Home | History | Annotate | Download | only in loader
      1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/browser/loader/offline_policy.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/metrics/histogram.h"
      9 #include "content/public/common/content_switches.h"
     10 #include "net/base/load_flags.h"
     11 #include "net/http/http_response_info.h"
     12 #include "net/url_request/url_request.h"
     13 
     14 namespace content {
     15 
     16 OfflinePolicy::OfflinePolicy()
     17     : enabled_(CommandLine::ForCurrentProcess()->HasSwitch(
     18           switches::kEnableOfflineCacheAccess)),
     19       state_(INIT),
     20       resource_loads_initiated_(0),
     21       resource_loads_successfully_started_(0) {}
     22 
     23 OfflinePolicy::~OfflinePolicy() {
     24   RecordAndResetStats();
     25 }
     26 
     27 void OfflinePolicy::RecordAndResetStats() {
     28   if (enabled_ && OFFLINE == state_ && 0 != resource_loads_initiated_) {
     29     UMA_HISTOGRAM_PERCENTAGE(
     30         "OfflinePolicy.SuccessfulResourceLoadPercentage",
     31         (resource_loads_successfully_started_ * 100 /
     32          resource_loads_initiated_));
     33   }
     34   resource_loads_initiated_ = 0;
     35   resource_loads_successfully_started_ = 0;
     36 }
     37 
     38 int OfflinePolicy::GetAdditionalLoadFlags(int current_flags,
     39                                           bool reset_state) {
     40   // Don't do anything if offline mode is disabled.
     41   if (!enabled_)
     42     return 0;
     43 
     44   if (reset_state) {
     45     RecordAndResetStats();
     46     state_ = INIT;
     47   }
     48 
     49   ++resource_loads_initiated_;
     50 
     51   // If a consumer has requested something contradictory, it wins; we
     52   // don't modify the load flags.
     53   if (current_flags &
     54       (net::LOAD_BYPASS_CACHE | net::LOAD_PREFERRING_CACHE |
     55        net::LOAD_ONLY_FROM_CACHE | net::LOAD_FROM_CACHE_IF_OFFLINE |
     56        net::LOAD_DISABLE_CACHE)) {
     57     return 0;
     58   }
     59 
     60   switch(state_) {
     61     case INIT:
     62       return net::LOAD_FROM_CACHE_IF_OFFLINE;
     63     case ONLINE:
     64       return 0;
     65     case OFFLINE:
     66       return net::LOAD_ONLY_FROM_CACHE;
     67   }
     68   NOTREACHED();
     69   return 0;
     70 }
     71 
     72 void OfflinePolicy::UpdateStateForSuccessfullyStartedRequest(
     73     const net::HttpResponseInfo& response_info) {
     74   // Don't do anything if offline mode is disabled.
     75   if (!enabled_)
     76     return;
     77 
     78   // If we get here, we're going to be providing some amount of information
     79   // to the renderer.
     80   ++resource_loads_successfully_started_;
     81 
     82   if (state_ != INIT)
     83     // We've already made the decision for the rest of this set
     84     // of navigations.
     85     return;
     86 
     87   if (response_info.server_data_unavailable) {
     88     state_ = OFFLINE;
     89   } else if (response_info.network_accessed) {
     90     // If we got the response from the network or validated it as part
     91     // of this request, that means our connection to the host is working.
     92     state_ = ONLINE;
     93   }
     94 }
     95 
     96 }  // namespace content
     97