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/common/resource_messages.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 |
| 18 OfflinePolicy::~OfflinePolicy() {} |
| 19 |
| 20 int OfflinePolicy::OfflineLoadFlags( |
| 21 int child_id, int route_id, int current_flags, |
| 22 ResourceType::Type resource_type) { |
| 23 |
| 24 // Don't do anything if offline mode is disabled. |
| 25 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 26 switches::kEnableOfflineCacheAccess)) |
| 27 return 0; |
| 28 |
| 29 // If a consumer has requested something contradictory, it wins; we |
| 30 // don't modify the load flags. |
| 31 if (current_flags & |
| 32 (LOAD_BYPASS_CACHE | LOAD_PREFERRING_CACHE | LOAD_ONLY_FROM_CACHE | |
| 33 LOAD_FROM_CACHE_IF_OFFLINE | LOAD_DISABLE_CACHE)) { |
| 34 return; |
| 35 } |
| 36 |
| 37 std::pair<int, int> key(std::make_pair(child_id, route_id)); |
| 38 // std::map::insert just returns an existing element if it already exists. |
| 39 const RouteMap::iterator it( |
| 40 offline_state_map_.insert(std::make_pair(key, ROUTE_INIT)).first); |
| 41 |
| 42 if (resource_type == ResourceType::MAIN_FRAME) |
| 43 it->second = ROUTE_INIT; |
| 44 |
| 45 switch(it->second) { |
| 46 case ROUTE_INIT: |
| 47 return net::LOAD_FROM_CACHE_IF_OFFLINE; |
| 48 case ROUTE_ONLINE: |
| 49 return 0; |
| 50 case ROUTE_OFFLINE: |
| 51 return net::LOAD_ONLY_FROM_CACHE; |
| 52 } |
| 53 NOTREACHED(); |
| 54 return 0; |
| 55 } |
| 56 |
| 57 void OfflinePolicy::RequestCompleted( |
| 58 int child_id, int route_id, const net::HttpResponseInfo& response_info) { |
| 59 // Don't do anything if offline mode is disabled. |
| 60 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 61 switches::kEnableOfflineCacheAccess)) |
| 62 return; |
| 63 |
| 64 const RouteMap::iterator it( |
| 65 offline_state_map_.find(std::make_pair(child_id, route_id))); |
| 66 |
| 67 // We should have seen this on the incoming side. |
| 68 DCHECK(offline_state_map_.end() != it); |
| 69 |
| 70 if (it->second == ROUTE_INIT) { |
| 71 if (response_info.server_data_unavailable) { |
| 72 it->second = ROUTE_OFFLINE; |
| 73 } else if (response_info.network_verified) { |
| 74 // If we got the response from the network or validated it as part |
| 75 // of this request, that means our connection to the host is working. |
| 76 it->second = ROUTE_ONLINE; |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 void OfflinePolicy::RouteRemoved(int child_id, int route_id) { |
| 82 // Don't do anything if offline mode is disabled. |
| 83 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 84 switches::kEnableOfflineCacheAccess)) |
| 85 return; |
| 86 |
| 87 const RouteMap::iterator it( |
| 88 offline_state_map_.find(std::make_pair(child_id, route_id))); |
| 89 if (offline_state_map_.end() != it) |
| 90 offline_state_map_.erase(it); |
| 91 } |
| 92 |
| 93 } // namespace content |
OLD | NEW |