Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "content/public/common/content_switches.h" | |
| 9 #include "net/base/load_flags.h" | |
| 10 #include "net/http/http_response_info.h" | |
| 11 #include "net/url_request/url_request.h" | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 OfflinePolicy::OfflinePolicy() | |
| 16 : offline_state_(INIT) {} | |
| 17 | |
| 18 OfflinePolicy::~OfflinePolicy() {} | |
| 19 | |
| 20 int OfflinePolicy::GetAdditionalLoadFlags( | |
| 21 int current_flags, ResourceType::Type resource_type) { | |
| 22 | |
| 23 // Don't do anything if offline mode is disabled. | |
| 24 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
|
darin (slow to review)
2013/04/11 20:01:23
it seems like this switch could just be checked on
Randy Smith (Not in Mondays)
2013/04/12 14:06:44
Done.
| |
| 25 switches::kEnableOfflineCacheAccess)) | |
| 26 return 0; | |
| 27 | |
| 28 if (resource_type == ResourceType::MAIN_FRAME) | |
| 29 offline_state_ = INIT; | |
| 30 | |
| 31 // If a consumer has requested something contradictory, it wins; we | |
| 32 // don't modify the load flags. | |
| 33 if (current_flags & | |
| 34 (net::LOAD_BYPASS_CACHE | net::LOAD_PREFERRING_CACHE | | |
| 35 net::LOAD_ONLY_FROM_CACHE | net::LOAD_FROM_CACHE_IF_OFFLINE | | |
| 36 net::LOAD_DISABLE_CACHE)) { | |
| 37 return 0; | |
| 38 } | |
| 39 | |
| 40 switch(offline_state_) { | |
| 41 case INIT: | |
| 42 return net::LOAD_FROM_CACHE_IF_OFFLINE; | |
| 43 case ONLINE: | |
| 44 return 0; | |
| 45 case OFFLINE: | |
| 46 return net::LOAD_ONLY_FROM_CACHE; | |
| 47 } | |
| 48 NOTREACHED(); | |
| 49 return 0; | |
| 50 } | |
| 51 | |
| 52 void OfflinePolicy::RequestCompleted( | |
| 53 const net::HttpResponseInfo& response_info) { | |
| 54 // Don't do anything if offline mode is disabled. | |
| 55 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 56 switches::kEnableOfflineCacheAccess)) | |
| 57 return; | |
| 58 | |
| 59 if (offline_state_ != INIT) | |
| 60 // We've already made the decision for the rest of this set | |
| 61 // of navigations. | |
| 62 return; | |
| 63 | |
| 64 if (response_info.server_data_unavailable) { | |
| 65 offline_state_ = OFFLINE; | |
| 66 } else if (response_info.network_accessed) { | |
| 67 // If we got the response from the network or validated it as part | |
| 68 // of this request, that means our connection to the host is working. | |
| 69 offline_state_ = ONLINE; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 } // namespace content | |
| OLD | NEW |