Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1122)

Unified Diff: content/browser/loader/offline_policy.cc

Issue 12886022: Implement offline mode behind a flag (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Incorporated Ricardo's comments. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..10003fd9db817cdbab057b1981dbd4e4ab670ab3
--- /dev/null
+++ b/content/browser/loader/offline_policy.cc
@@ -0,0 +1,94 @@
+// 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"
droger_google 2013/04/05 10:55:24 Why do you need this?
Randy Smith (Not in Mondays) 2013/04/05 17:56:38 I think it was a a leftover from when I had a diff
+#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::GetAdditionalLoadFlags(
+ 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;
+
+ 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;
+
+ // If a consumer has requested something contradictory, it wins; we
+ // don't modify the load flags.
+ if (current_flags &
+ (net::LOAD_BYPASS_CACHE | net::LOAD_PREFERRING_CACHE |
+ net::LOAD_ONLY_FROM_CACHE | net::LOAD_FROM_CACHE_IF_OFFLINE |
+ net::LOAD_DISABLE_CACHE)) {
+ return 0;
+ }
+
+ 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_accessed) {
+ // 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

Powered by Google App Engine
This is Rietveld 408576698