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.
5 #include "content/browser/loader/offline_policy.h"
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"
16 OfflinePolicy::OfflinePolicy()
17 : enabled_(CommandLine::ForCurrentProcess()->HasSwitch(
18 switches::kEnableOfflineCacheAccess
)),
20 resource_loads_initiated_(0),
21 resource_loads_successfully_started_(0) {}
23 OfflinePolicy::~OfflinePolicy() {
24 RecordAndResetStats();
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_
));
34 resource_loads_initiated_
= 0;
35 resource_loads_successfully_started_
= 0;
38 int OfflinePolicy::GetAdditionalLoadFlags(int current_flags
,
40 // Don't do anything if offline mode is disabled.
45 RecordAndResetStats();
49 ++resource_loads_initiated_
;
51 // If a consumer has requested something contradictory, it wins; we
52 // don't modify the load 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
)) {
62 return net::LOAD_FROM_CACHE_IF_OFFLINE
;
66 return net::LOAD_ONLY_FROM_CACHE
;
72 void OfflinePolicy::UpdateStateForSuccessfullyStartedRequest(
73 const net::HttpResponseInfo
& response_info
) {
74 // Don't do anything if offline mode is disabled.
78 // If we get here, we're going to be providing some amount of information
80 ++resource_loads_successfully_started_
;
83 // We've already made the decision for the rest of this set
87 if (response_info
.server_data_unavailable
) {
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.
96 } // namespace content