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

Side by Side Diff: content/child/appcache/web_application_cache_host_impl.cc

Issue 344493002: Move all remaining appcache-related code to content namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/appcache/web_application_cache_host_impl.h" 5 #include "content/child/appcache/web_application_cache_host_impl.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/id_map.h" 8 #include "base/id_map.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "third_party/WebKit/public/platform/WebString.h" 11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/platform/WebURL.h" 12 #include "third_party/WebKit/public/platform/WebURL.h"
13 #include "third_party/WebKit/public/platform/WebURLRequest.h" 13 #include "third_party/WebKit/public/platform/WebURLRequest.h"
14 #include "third_party/WebKit/public/platform/WebURLResponse.h" 14 #include "third_party/WebKit/public/platform/WebURLResponse.h"
15 15
16 using blink::WebApplicationCacheHost; 16 using blink::WebApplicationCacheHost;
17 using blink::WebApplicationCacheHostClient; 17 using blink::WebApplicationCacheHostClient;
18 using blink::WebString; 18 using blink::WebString;
19 using blink::WebURLRequest; 19 using blink::WebURLRequest;
20 using blink::WebURL; 20 using blink::WebURL;
21 using blink::WebURLResponse; 21 using blink::WebURLResponse;
22 using blink::WebVector; 22 using blink::WebVector;
23 using appcache::AppCacheBackend;
24 using appcache::AppCacheResourceInfo;
25 23
26 namespace content { 24 namespace content {
27 25
28 namespace { 26 namespace {
29 27
30 // Note: the order of the elements in this array must match those 28 // Note: the order of the elements in this array must match those
31 // of the EventID enum in appcache_interfaces.h. 29 // of the EventID enum in appcache_interfaces.h.
32 const char* kEventNames[] = { 30 const char* kEventNames[] = {
33 "Checking", "Error", "NoUpdate", "Downloading", "Progress", 31 "Checking", "Error", "NoUpdate", "Downloading", "Progress",
34 "UpdateReady", "Cached", "Obsolete" 32 "UpdateReady", "Cached", "Obsolete"
(...skipping 19 matching lines...) Expand all
54 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromId(int id) { 52 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromId(int id) {
55 return all_hosts()->Lookup(id); 53 return all_hosts()->Lookup(id);
56 } 54 }
57 55
58 WebApplicationCacheHostImpl::WebApplicationCacheHostImpl( 56 WebApplicationCacheHostImpl::WebApplicationCacheHostImpl(
59 WebApplicationCacheHostClient* client, 57 WebApplicationCacheHostClient* client,
60 AppCacheBackend* backend) 58 AppCacheBackend* backend)
61 : client_(client), 59 : client_(client),
62 backend_(backend), 60 backend_(backend),
63 host_id_(all_hosts()->Add(this)), 61 host_id_(all_hosts()->Add(this)),
64 status_(appcache::APPCACHE_STATUS_UNCACHED), 62 status_(APPCACHE_STATUS_UNCACHED),
65 is_scheme_supported_(false), 63 is_scheme_supported_(false),
66 is_get_method_(false), 64 is_get_method_(false),
67 is_new_master_entry_(MAYBE), 65 is_new_master_entry_(MAYBE),
68 was_select_cache_called_(false) { 66 was_select_cache_called_(false) {
69 DCHECK(client && backend && (host_id_ != appcache::kAppCacheNoHostId)); 67 DCHECK(client && backend && (host_id_ != kAppCacheNoHostId));
70 68
71 backend_->RegisterHost(host_id_); 69 backend_->RegisterHost(host_id_);
72 } 70 }
73 71
74 WebApplicationCacheHostImpl::~WebApplicationCacheHostImpl() { 72 WebApplicationCacheHostImpl::~WebApplicationCacheHostImpl() {
75 backend_->UnregisterHost(host_id_); 73 backend_->UnregisterHost(host_id_);
76 all_hosts()->Remove(host_id_); 74 all_hosts()->Remove(host_id_);
77 } 75 }
78 76
79 void WebApplicationCacheHostImpl::OnCacheSelected( 77 void WebApplicationCacheHostImpl::OnCacheSelected(
80 const appcache::AppCacheInfo& info) { 78 const AppCacheInfo& info) {
81 cache_info_ = info; 79 cache_info_ = info;
82 client_->didChangeCacheAssociation(); 80 client_->didChangeCacheAssociation();
83 } 81 }
84 82
85 void WebApplicationCacheHostImpl::OnStatusChanged( 83 void WebApplicationCacheHostImpl::OnStatusChanged(
86 appcache::AppCacheStatus status) { 84 AppCacheStatus status) {
87 // TODO(michaeln): delete me, not used 85 // TODO(michaeln): delete me, not used
88 } 86 }
89 87
90 void WebApplicationCacheHostImpl::OnEventRaised( 88 void WebApplicationCacheHostImpl::OnEventRaised(
91 appcache::AppCacheEventID event_id) { 89 AppCacheEventID event_id) {
92 DCHECK(event_id != 90 DCHECK(event_id !=
93 appcache::APPCACHE_PROGRESS_EVENT); // See OnProgressEventRaised. 91 APPCACHE_PROGRESS_EVENT); // See OnProgressEventRaised.
94 DCHECK(event_id != appcache::APPCACHE_ERROR_EVENT); // See OnErrorEventRaised. 92 DCHECK(event_id != APPCACHE_ERROR_EVENT); // See OnErrorEventRaised.
95 93
96 // Emit logging output prior to calling out to script as we can get 94 // Emit logging output prior to calling out to script as we can get
97 // deleted within the script event handler. 95 // deleted within the script event handler.
98 const char* kFormatString = "Application Cache %s event"; 96 const char* kFormatString = "Application Cache %s event";
99 std::string message = base::StringPrintf(kFormatString, 97 std::string message = base::StringPrintf(kFormatString,
100 kEventNames[event_id]); 98 kEventNames[event_id]);
101 OnLogMessage(appcache::APPCACHE_LOG_INFO, message); 99 OnLogMessage(APPCACHE_LOG_INFO, message);
102 100
103 switch (event_id) { 101 switch (event_id) {
104 case appcache::APPCACHE_CHECKING_EVENT: 102 case APPCACHE_CHECKING_EVENT:
105 status_ = appcache::APPCACHE_STATUS_CHECKING; 103 status_ = APPCACHE_STATUS_CHECKING;
106 break; 104 break;
107 case appcache::APPCACHE_DOWNLOADING_EVENT: 105 case APPCACHE_DOWNLOADING_EVENT:
108 status_ = appcache::APPCACHE_STATUS_DOWNLOADING; 106 status_ = APPCACHE_STATUS_DOWNLOADING;
109 break; 107 break;
110 case appcache::APPCACHE_UPDATE_READY_EVENT: 108 case APPCACHE_UPDATE_READY_EVENT:
111 status_ = appcache::APPCACHE_STATUS_UPDATE_READY; 109 status_ = APPCACHE_STATUS_UPDATE_READY;
112 break; 110 break;
113 case appcache::APPCACHE_CACHED_EVENT: 111 case APPCACHE_CACHED_EVENT:
114 case appcache::APPCACHE_NO_UPDATE_EVENT: 112 case APPCACHE_NO_UPDATE_EVENT:
115 status_ = appcache::APPCACHE_STATUS_IDLE; 113 status_ = APPCACHE_STATUS_IDLE;
116 break; 114 break;
117 case appcache::APPCACHE_OBSOLETE_EVENT: 115 case APPCACHE_OBSOLETE_EVENT:
118 status_ = appcache::APPCACHE_STATUS_OBSOLETE; 116 status_ = APPCACHE_STATUS_OBSOLETE;
119 break; 117 break;
120 default: 118 default:
121 NOTREACHED(); 119 NOTREACHED();
122 break; 120 break;
123 } 121 }
124 122
125 client_->notifyEventListener(static_cast<EventID>(event_id)); 123 client_->notifyEventListener(static_cast<EventID>(event_id));
126 } 124 }
127 125
128 void WebApplicationCacheHostImpl::OnProgressEventRaised( 126 void WebApplicationCacheHostImpl::OnProgressEventRaised(
129 const GURL& url, int num_total, int num_complete) { 127 const GURL& url, int num_total, int num_complete) {
130 // Emit logging output prior to calling out to script as we can get 128 // Emit logging output prior to calling out to script as we can get
131 // deleted within the script event handler. 129 // deleted within the script event handler.
132 const char* kFormatString = "Application Cache Progress event (%d of %d) %s"; 130 const char* kFormatString = "Application Cache Progress event (%d of %d) %s";
133 std::string message = base::StringPrintf(kFormatString, num_complete, 131 std::string message = base::StringPrintf(kFormatString, num_complete,
134 num_total, url.spec().c_str()); 132 num_total, url.spec().c_str());
135 OnLogMessage(appcache::APPCACHE_LOG_INFO, message); 133 OnLogMessage(APPCACHE_LOG_INFO, message);
136 status_ = appcache::APPCACHE_STATUS_DOWNLOADING; 134 status_ = APPCACHE_STATUS_DOWNLOADING;
137 client_->notifyProgressEventListener(url, num_total, num_complete); 135 client_->notifyProgressEventListener(url, num_total, num_complete);
138 } 136 }
139 137
140 void WebApplicationCacheHostImpl::OnErrorEventRaised( 138 void WebApplicationCacheHostImpl::OnErrorEventRaised(
141 const appcache::AppCacheErrorDetails& details) { 139 const AppCacheErrorDetails& details) {
142 // Emit logging output prior to calling out to script as we can get 140 // Emit logging output prior to calling out to script as we can get
143 // deleted within the script event handler. 141 // deleted within the script event handler.
144 const char* kFormatString = "Application Cache Error event: %s"; 142 const char* kFormatString = "Application Cache Error event: %s";
145 std::string full_message = 143 std::string full_message =
146 base::StringPrintf(kFormatString, details.message.c_str()); 144 base::StringPrintf(kFormatString, details.message.c_str());
147 OnLogMessage(appcache::APPCACHE_LOG_ERROR, full_message); 145 OnLogMessage(APPCACHE_LOG_ERROR, full_message);
148 146
149 status_ = cache_info_.is_complete ? appcache::APPCACHE_STATUS_IDLE : 147 status_ = cache_info_.is_complete ? APPCACHE_STATUS_IDLE :
150 appcache::APPCACHE_STATUS_UNCACHED; 148 APPCACHE_STATUS_UNCACHED;
151 if (details.is_cross_origin) { 149 if (details.is_cross_origin) {
152 // Don't leak detailed information to script for cross-origin resources. 150 // Don't leak detailed information to script for cross-origin resources.
153 DCHECK_EQ(appcache::APPCACHE_RESOURCE_ERROR, details.reason); 151 DCHECK_EQ(APPCACHE_RESOURCE_ERROR, details.reason);
154 client_->notifyErrorEventListener( 152 client_->notifyErrorEventListener(
155 static_cast<ErrorReason>(details.reason), details.url, 0, WebString()); 153 static_cast<ErrorReason>(details.reason), details.url, 0, WebString());
156 } else { 154 } else {
157 client_->notifyErrorEventListener(static_cast<ErrorReason>(details.reason), 155 client_->notifyErrorEventListener(static_cast<ErrorReason>(details.reason),
158 details.url, 156 details.url,
159 details.status, 157 details.status,
160 WebString::fromUTF8(details.message)); 158 WebString::fromUTF8(details.message));
161 } 159 }
162 } 160 }
163 161
164 void WebApplicationCacheHostImpl::willStartMainResourceRequest( 162 void WebApplicationCacheHostImpl::willStartMainResourceRequest(
165 WebURLRequest& request, const WebApplicationCacheHost* spawning_host) { 163 WebURLRequest& request, const WebApplicationCacheHost* spawning_host) {
166 request.setAppCacheHostID(host_id_); 164 request.setAppCacheHostID(host_id_);
167 165
168 original_main_resource_url_ = ClearUrlRef(request.url()); 166 original_main_resource_url_ = ClearUrlRef(request.url());
169 167
170 std::string method = request.httpMethod().utf8(); 168 std::string method = request.httpMethod().utf8();
171 is_get_method_ = (method == appcache::kHttpGETMethod); 169 is_get_method_ = (method == kHttpGETMethod);
172 DCHECK(method == StringToUpperASCII(method)); 170 DCHECK(method == StringToUpperASCII(method));
173 171
174 const WebApplicationCacheHostImpl* spawning_host_impl = 172 const WebApplicationCacheHostImpl* spawning_host_impl =
175 static_cast<const WebApplicationCacheHostImpl*>(spawning_host); 173 static_cast<const WebApplicationCacheHostImpl*>(spawning_host);
176 if (spawning_host_impl && (spawning_host_impl != this) && 174 if (spawning_host_impl && (spawning_host_impl != this) &&
177 (spawning_host_impl->status_ != appcache::APPCACHE_STATUS_UNCACHED)) { 175 (spawning_host_impl->status_ != APPCACHE_STATUS_UNCACHED)) {
178 backend_->SetSpawningHostId(host_id_, spawning_host_impl->host_id()); 176 backend_->SetSpawningHostId(host_id_, spawning_host_impl->host_id());
179 } 177 }
180 } 178 }
181 179
182 void WebApplicationCacheHostImpl::willStartSubResourceRequest( 180 void WebApplicationCacheHostImpl::willStartSubResourceRequest(
183 WebURLRequest& request) { 181 WebURLRequest& request) {
184 request.setAppCacheHostID(host_id_); 182 request.setAppCacheHostID(host_id_);
185 } 183 }
186 184
187 void WebApplicationCacheHostImpl::selectCacheWithoutManifest() { 185 void WebApplicationCacheHostImpl::selectCacheWithoutManifest() {
188 if (was_select_cache_called_) 186 if (was_select_cache_called_)
189 return; 187 return;
190 was_select_cache_called_ = true; 188 was_select_cache_called_ = true;
191 189
192 status_ = (document_response_.appCacheID() == appcache::kAppCacheNoCacheId) ? 190 status_ = (document_response_.appCacheID() == kAppCacheNoCacheId) ?
193 appcache::APPCACHE_STATUS_UNCACHED : appcache::APPCACHE_STATUS_CHECKING; 191 APPCACHE_STATUS_UNCACHED : APPCACHE_STATUS_CHECKING;
194 is_new_master_entry_ = NO; 192 is_new_master_entry_ = NO;
195 backend_->SelectCache(host_id_, document_url_, 193 backend_->SelectCache(host_id_, document_url_,
196 document_response_.appCacheID(), 194 document_response_.appCacheID(),
197 GURL()); 195 GURL());
198 } 196 }
199 197
200 bool WebApplicationCacheHostImpl::selectCacheWithManifest( 198 bool WebApplicationCacheHostImpl::selectCacheWithManifest(
201 const WebURL& manifest_url) { 199 const WebURL& manifest_url) {
202 if (was_select_cache_called_) 200 if (was_select_cache_called_)
203 return true; 201 return true;
204 was_select_cache_called_ = true; 202 was_select_cache_called_ = true;
205 203
206 GURL manifest_gurl(ClearUrlRef(manifest_url)); 204 GURL manifest_gurl(ClearUrlRef(manifest_url));
207 205
208 // 6.9.6 The application cache selection algorithm 206 // 6.9.6 The application cache selection algorithm
209 // Check for new 'master' entries. 207 // Check for new 'master' entries.
210 if (document_response_.appCacheID() == appcache::kAppCacheNoCacheId) { 208 if (document_response_.appCacheID() == kAppCacheNoCacheId) {
211 if (is_scheme_supported_ && is_get_method_ && 209 if (is_scheme_supported_ && is_get_method_ &&
212 (manifest_gurl.GetOrigin() == document_url_.GetOrigin())) { 210 (manifest_gurl.GetOrigin() == document_url_.GetOrigin())) {
213 status_ = appcache::APPCACHE_STATUS_CHECKING; 211 status_ = APPCACHE_STATUS_CHECKING;
214 is_new_master_entry_ = YES; 212 is_new_master_entry_ = YES;
215 } else { 213 } else {
216 status_ = appcache::APPCACHE_STATUS_UNCACHED; 214 status_ = APPCACHE_STATUS_UNCACHED;
217 is_new_master_entry_ = NO; 215 is_new_master_entry_ = NO;
218 manifest_gurl = GURL(); 216 manifest_gurl = GURL();
219 } 217 }
220 backend_->SelectCache( 218 backend_->SelectCache(
221 host_id_, document_url_, appcache::kAppCacheNoCacheId, manifest_gurl); 219 host_id_, document_url_, kAppCacheNoCacheId, manifest_gurl);
222 return true; 220 return true;
223 } 221 }
224 222
225 DCHECK_EQ(NO, is_new_master_entry_); 223 DCHECK_EQ(NO, is_new_master_entry_);
226 224
227 // 6.9.6 The application cache selection algorithm 225 // 6.9.6 The application cache selection algorithm
228 // Check for 'foreign' entries. 226 // Check for 'foreign' entries.
229 GURL document_manifest_gurl(document_response_.appCacheManifestURL()); 227 GURL document_manifest_gurl(document_response_.appCacheManifestURL());
230 if (document_manifest_gurl != manifest_gurl) { 228 if (document_manifest_gurl != manifest_gurl) {
231 backend_->MarkAsForeignEntry(host_id_, document_url_, 229 backend_->MarkAsForeignEntry(host_id_, document_url_,
232 document_response_.appCacheID()); 230 document_response_.appCacheID());
233 status_ = appcache::APPCACHE_STATUS_UNCACHED; 231 status_ = APPCACHE_STATUS_UNCACHED;
234 return false; // the navigation will be restarted 232 return false; // the navigation will be restarted
235 } 233 }
236 234
237 status_ = appcache::APPCACHE_STATUS_CHECKING; 235 status_ = APPCACHE_STATUS_CHECKING;
238 236
239 // Its a 'master' entry thats already in the cache. 237 // Its a 'master' entry thats already in the cache.
240 backend_->SelectCache(host_id_, document_url_, 238 backend_->SelectCache(host_id_, document_url_,
241 document_response_.appCacheID(), 239 document_response_.appCacheID(),
242 manifest_gurl); 240 manifest_gurl);
243 return true; 241 return true;
244 } 242 }
245 243
246 void WebApplicationCacheHostImpl::didReceiveResponseForMainResource( 244 void WebApplicationCacheHostImpl::didReceiveResponseForMainResource(
247 const WebURLResponse& response) { 245 const WebURLResponse& response) {
248 document_response_ = response; 246 document_response_ = response;
249 document_url_ = ClearUrlRef(document_response_.url()); 247 document_url_ = ClearUrlRef(document_response_.url());
250 if (document_url_ != original_main_resource_url_) 248 if (document_url_ != original_main_resource_url_)
251 is_get_method_ = true; // A redirect was involved. 249 is_get_method_ = true; // A redirect was involved.
252 original_main_resource_url_ = GURL(); 250 original_main_resource_url_ = GURL();
253 251
254 is_scheme_supported_ = appcache::IsSchemeSupported(document_url_); 252 is_scheme_supported_ = IsSchemeSupportedForAppCache(document_url_);
255 if ((document_response_.appCacheID() != appcache::kAppCacheNoCacheId) || 253 if ((document_response_.appCacheID() != kAppCacheNoCacheId) ||
256 !is_scheme_supported_ || !is_get_method_) 254 !is_scheme_supported_ || !is_get_method_)
257 is_new_master_entry_ = NO; 255 is_new_master_entry_ = NO;
258 } 256 }
259 257
260 void WebApplicationCacheHostImpl::didReceiveDataForMainResource( 258 void WebApplicationCacheHostImpl::didReceiveDataForMainResource(
261 const char* data, int len) { 259 const char* data, int len) {
262 if (is_new_master_entry_ == NO) 260 if (is_new_master_entry_ == NO)
263 return; 261 return;
264 // TODO(michaeln): write me 262 // TODO(michaeln): write me
265 } 263 }
266 264
267 void WebApplicationCacheHostImpl::didFinishLoadingMainResource(bool success) { 265 void WebApplicationCacheHostImpl::didFinishLoadingMainResource(bool success) {
268 if (is_new_master_entry_ == NO) 266 if (is_new_master_entry_ == NO)
269 return; 267 return;
270 // TODO(michaeln): write me 268 // TODO(michaeln): write me
271 } 269 }
272 270
273 WebApplicationCacheHost::Status WebApplicationCacheHostImpl::status() { 271 WebApplicationCacheHost::Status WebApplicationCacheHostImpl::status() {
274 return static_cast<WebApplicationCacheHost::Status>(status_); 272 return static_cast<WebApplicationCacheHost::Status>(status_);
275 } 273 }
276 274
277 bool WebApplicationCacheHostImpl::startUpdate() { 275 bool WebApplicationCacheHostImpl::startUpdate() {
278 if (!backend_->StartUpdate(host_id_)) 276 if (!backend_->StartUpdate(host_id_))
279 return false; 277 return false;
280 if (status_ == appcache::APPCACHE_STATUS_IDLE || 278 if (status_ == APPCACHE_STATUS_IDLE ||
281 status_ == appcache::APPCACHE_STATUS_UPDATE_READY) 279 status_ == APPCACHE_STATUS_UPDATE_READY)
282 status_ = appcache::APPCACHE_STATUS_CHECKING; 280 status_ = APPCACHE_STATUS_CHECKING;
283 else 281 else
284 status_ = backend_->GetStatus(host_id_); 282 status_ = backend_->GetStatus(host_id_);
285 return true; 283 return true;
286 } 284 }
287 285
288 bool WebApplicationCacheHostImpl::swapCache() { 286 bool WebApplicationCacheHostImpl::swapCache() {
289 if (!backend_->SwapCache(host_id_)) 287 if (!backend_->SwapCache(host_id_))
290 return false; 288 return false;
291 status_ = backend_->GetStatus(host_id_); 289 status_ = backend_->GetStatus(host_id_);
292 return true; 290 return true;
(...skipping 23 matching lines...) Expand all
316 web_resources[i].isExplicit = resource_infos[i].is_explicit; 314 web_resources[i].isExplicit = resource_infos[i].is_explicit;
317 web_resources[i].isManifest = resource_infos[i].is_manifest; 315 web_resources[i].isManifest = resource_infos[i].is_manifest;
318 web_resources[i].isForeign = resource_infos[i].is_foreign; 316 web_resources[i].isForeign = resource_infos[i].is_foreign;
319 web_resources[i].isFallback = resource_infos[i].is_fallback; 317 web_resources[i].isFallback = resource_infos[i].is_fallback;
320 web_resources[i].url = resource_infos[i].url; 318 web_resources[i].url = resource_infos[i].url;
321 } 319 }
322 resources->swap(web_resources); 320 resources->swap(web_resources);
323 } 321 }
324 322
325 } // namespace content 323 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698