Index: content/browser/loader/offline_policy.cc |
diff --git a/content/browser/loader/offline_policy.cc b/content/browser/loader/offline_policy.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a97997d5ead78e9d30ea904c2ba5c7f771847705 |
--- /dev/null |
+++ b/content/browser/loader/offline_policy.cc |
@@ -0,0 +1,93 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/browser/loader/offline_policy.h" |
+ |
+#include "base/command_line.h" |
+#include "content/common/resource_messages.h" |
+#include "content/public/common/content_switches.h" |
+#include "net/base/load_flags.h" |
+#include "net/http/http_response_info.h" |
+#include "net/url_request/url_request.h" |
+ |
+namespace content { |
+ |
+OfflinePolicy::OfflinePolicy() {} |
+ |
+OfflinePolicy::~OfflinePolicy() {} |
+ |
+int OfflinePolicy::OfflineLoadFlags( |
+ int child_id, int route_id, int current_flags, |
+ ResourceType::Type resource_type) { |
+ |
+ // Don't do anything if offline mode is disabled. |
+ if (!CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableOfflineCacheAccess)) |
+ return 0; |
+ |
+ // If a consumer has requested something contradictory, it wins; we |
+ // don't modify the load flags. |
+ if (current_flags & |
+ (LOAD_BYPASS_CACHE | LOAD_PREFERRING_CACHE | LOAD_ONLY_FROM_CACHE | |
+ LOAD_FROM_CACHE_IF_OFFLINE | LOAD_DISABLE_CACHE)) { |
+ return; |
+ } |
+ |
+ std::pair<int, int> key(std::make_pair(child_id, route_id)); |
+ // std::map::insert just returns an existing element if it already exists. |
+ const RouteMap::iterator it( |
+ offline_state_map_.insert(std::make_pair(key, ROUTE_INIT)).first); |
+ |
+ if (resource_type == ResourceType::MAIN_FRAME) |
+ it->second = ROUTE_INIT; |
+ |
+ switch(it->second) { |
+ case ROUTE_INIT: |
+ return net::LOAD_FROM_CACHE_IF_OFFLINE; |
+ case ROUTE_ONLINE: |
+ return 0; |
+ case ROUTE_OFFLINE: |
+ return net::LOAD_ONLY_FROM_CACHE; |
+ } |
+ NOTREACHED(); |
+ return 0; |
+} |
+ |
+void OfflinePolicy::RequestCompleted( |
+ int child_id, int route_id, const net::HttpResponseInfo& response_info) { |
+ // Don't do anything if offline mode is disabled. |
+ if (!CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableOfflineCacheAccess)) |
+ return; |
+ |
+ const RouteMap::iterator it( |
+ offline_state_map_.find(std::make_pair(child_id, route_id))); |
+ |
+ // We should have seen this on the incoming side. |
+ DCHECK(offline_state_map_.end() != it); |
+ |
+ if (it->second == ROUTE_INIT) { |
+ if (response_info.server_data_unavailable) { |
+ it->second = ROUTE_OFFLINE; |
+ } else if (response_info.network_verified) { |
+ // If we got the response from the network or validated it as part |
+ // of this request, that means our connection to the host is working. |
+ it->second = ROUTE_ONLINE; |
+ } |
+ } |
+} |
+ |
+void OfflinePolicy::RouteRemoved(int child_id, int route_id) { |
+ // Don't do anything if offline mode is disabled. |
+ if (!CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableOfflineCacheAccess)) |
+ return; |
+ |
+ const RouteMap::iterator it( |
+ offline_state_map_.find(std::make_pair(child_id, route_id))); |
+ if (offline_state_map_.end() != it) |
+ offline_state_map_.erase(it); |
+} |
+ |
+} // namespace content |