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