OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "webkit/appcache/web_application_cache_host_impl.h" | 5 #include "webkit/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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/singleton.h" | 10 #include "base/singleton.h" |
11 #include "third_party/WebKit/WebKit/chromium/public/WebDataSource.h" | 11 #include "third_party/WebKit/WebKit/chromium/public/WebDataSource.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
16 | 16 |
17 using WebKit::WebApplicationCacheHost; | 17 using WebKit::WebApplicationCacheHost; |
18 using WebKit::WebApplicationCacheHostClient; | 18 using WebKit::WebApplicationCacheHostClient; |
19 using WebKit::WebDataSource; | 19 using WebKit::WebDataSource; |
20 using WebKit::WebFrame; | 20 using WebKit::WebFrame; |
21 using WebKit::WebURLRequest; | 21 using WebKit::WebURLRequest; |
22 using WebKit::WebURL; | 22 using WebKit::WebURL; |
23 using WebKit::WebURLResponse; | 23 using WebKit::WebURLResponse; |
24 | 24 |
25 namespace appcache { | 25 namespace appcache { |
26 | 26 |
27 typedef IDMap<WebApplicationCacheHostImpl> HostsMap; | 27 typedef IDMap<WebApplicationCacheHostImpl> HostsMap; |
28 | 28 |
| 29 // Note: the order of the elements in this array must match those |
| 30 // of the EventID enum in appcache_interfaces.h. |
| 31 static const char* kEventNames[] = { |
| 32 "Checking", "Error", "NoUpdate", "Downloading", "Progress", |
| 33 "UpdateReady", "Cached", "Obsolete" |
| 34 }; |
| 35 |
29 static HostsMap* all_hosts() { | 36 static HostsMap* all_hosts() { |
30 return Singleton<HostsMap>::get(); | 37 return Singleton<HostsMap>::get(); |
31 } | 38 } |
32 | 39 |
33 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromId(int id) { | 40 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromId(int id) { |
34 return all_hosts()->Lookup(id); | 41 return all_hosts()->Lookup(id); |
35 } | 42 } |
36 | 43 |
37 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromFrame( | 44 WebApplicationCacheHostImpl* WebApplicationCacheHostImpl::FromFrame( |
38 WebFrame* frame) { | 45 WebFrame* frame) { |
(...skipping 35 matching lines...) Loading... |
74 has_status_ = true; | 81 has_status_ = true; |
75 } | 82 } |
76 | 83 |
77 void WebApplicationCacheHostImpl::OnStatusChanged(appcache::Status status) { | 84 void WebApplicationCacheHostImpl::OnStatusChanged(appcache::Status status) { |
78 if (has_status_) | 85 if (has_status_) |
79 status_ = status; | 86 status_ = status; |
80 } | 87 } |
81 | 88 |
82 void WebApplicationCacheHostImpl::OnEventRaised(appcache::EventID event_id) { | 89 void WebApplicationCacheHostImpl::OnEventRaised(appcache::EventID event_id) { |
83 DCHECK(event_id != PROGRESS_EVENT); // See OnProgressEventRaised. | 90 DCHECK(event_id != PROGRESS_EVENT); // See OnProgressEventRaised. |
| 91 DCHECK(event_id != ERROR_EVENT); // See OnErrorEventRaised. |
| 92 |
| 93 // Emit logging output prior to calling out to script as we can get |
| 94 // deleted withing the script event handler. |
| 95 const char* kFormatString = "Application Cache %s event"; |
| 96 std::string message = StringPrintf(kFormatString, kEventNames[event_id]); |
| 97 OnLogMessage(LOG_INFO, message); |
| 98 |
84 // Most events change the status. Clear out what we know so that the latest | 99 // Most events change the status. Clear out what we know so that the latest |
85 // status will be obtained from the backend. | 100 // status will be obtained from the backend. |
86 has_status_ = false; | 101 has_status_ = false; |
87 has_cached_status_ = false; | 102 has_cached_status_ = false; |
88 client_->notifyEventListener(static_cast<EventID>(event_id)); | 103 client_->notifyEventListener(static_cast<EventID>(event_id)); |
89 } | 104 } |
90 | 105 |
91 void WebApplicationCacheHostImpl::OnProgressEventRaised( | 106 void WebApplicationCacheHostImpl::OnProgressEventRaised( |
92 const GURL& url, int num_total, int num_complete) { | 107 const GURL& url, int num_total, int num_complete) { |
| 108 // Emit logging output prior to calling out to script as we can get |
| 109 // deleted withing the script event handler. |
| 110 const char* kFormatString = "Application Cache Progress event (%d of %d) %s"; |
| 111 std::string message = StringPrintf(kFormatString, num_complete, |
| 112 num_total, url.spec().c_str()); |
| 113 OnLogMessage(LOG_INFO, message); |
| 114 |
93 client_->notifyProgressEventListener(url, num_total, num_complete); | 115 client_->notifyProgressEventListener(url, num_total, num_complete); |
94 } | 116 } |
95 | 117 |
| 118 void WebApplicationCacheHostImpl::OnErrorEventRaised( |
| 119 const std::string& message) { |
| 120 // Emit logging output prior to calling out to script as we can get |
| 121 // deleted withing the script event handler. |
| 122 const char* kFormatString = "Application Cache Error event: %s"; |
| 123 std::string full_message = StringPrintf(kFormatString, message.c_str()); |
| 124 OnLogMessage(LOG_ERROR, full_message); |
| 125 |
| 126 // Most events change the status. Clear out what we know so that the latest |
| 127 // status will be obtained from the backend. |
| 128 has_status_ = false; |
| 129 has_cached_status_ = false; |
| 130 client_->notifyEventListener(static_cast<EventID>(ERROR_EVENT)); |
| 131 } |
| 132 |
96 void WebApplicationCacheHostImpl::willStartMainResourceRequest( | 133 void WebApplicationCacheHostImpl::willStartMainResourceRequest( |
97 WebURLRequest& request) { | 134 WebURLRequest& request) { |
98 request.setAppCacheHostID(host_id_); | 135 request.setAppCacheHostID(host_id_); |
99 std::string method = request.httpMethod().utf8(); | 136 std::string method = request.httpMethod().utf8(); |
100 is_get_method_ = (method == kHttpGETMethod); | 137 is_get_method_ = (method == kHttpGETMethod); |
101 DCHECK(method == StringToUpperASCII(method)); | 138 DCHECK(method == StringToUpperASCII(method)); |
102 } | 139 } |
103 | 140 |
104 void WebApplicationCacheHostImpl::willStartSubResourceRequest( | 141 void WebApplicationCacheHostImpl::willStartSubResourceRequest( |
105 WebURLRequest& request) { | 142 WebURLRequest& request) { |
(...skipping 120 matching lines...) Loading... |
226 | 263 |
227 bool WebApplicationCacheHostImpl::swapCache() { | 264 bool WebApplicationCacheHostImpl::swapCache() { |
228 // Cache status will change when cache is swapped. Clear out any saved idea | 265 // Cache status will change when cache is swapped. Clear out any saved idea |
229 // of status so that backend will be queried for actual status. | 266 // of status so that backend will be queried for actual status. |
230 has_status_ = false; | 267 has_status_ = false; |
231 has_cached_status_ = false; | 268 has_cached_status_ = false; |
232 return backend_->SwapCache(host_id_); | 269 return backend_->SwapCache(host_id_); |
233 } | 270 } |
234 | 271 |
235 } // appcache namespace | 272 } // appcache namespace |
OLD | NEW |