OLD | NEW |
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/browser/service_worker/service_worker_storage.h" | 5 #include "content/browser/service_worker/service_worker_storage.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "content/browser/service_worker/service_worker_info.h" | 10 #include "content/browser/service_worker/service_worker_info.h" |
11 #include "content/browser/service_worker/service_worker_registration.h" | 11 #include "content/browser/service_worker/service_worker_registration.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "webkit/browser/quota/quota_manager_proxy.h" | 13 #include "webkit/browser/quota/quota_manager_proxy.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 void RunSoon(const base::Closure& closure) { | 19 void RunSoon(const tracked_objects::Location& from_here, |
20 base::MessageLoop::current()->PostTask(FROM_HERE, closure); | 20 const base::Closure& closure) { |
| 21 base::MessageLoop::current()->PostTask(from_here, closure); |
21 } | 22 } |
22 | 23 |
23 const base::FilePath::CharType kServiceWorkerDirectory[] = | 24 const base::FilePath::CharType kServiceWorkerDirectory[] = |
24 FILE_PATH_LITERAL("ServiceWorker"); | 25 FILE_PATH_LITERAL("ServiceWorker"); |
25 | 26 |
26 } // namespace | 27 } // namespace |
27 | 28 |
28 ServiceWorkerStorage::ServiceWorkerStorage( | 29 ServiceWorkerStorage::ServiceWorkerStorage( |
29 const base::FilePath& path, | 30 const base::FilePath& path, |
30 quota::QuotaManagerProxy* quota_manager_proxy) | 31 quota::QuotaManagerProxy* quota_manager_proxy) |
(...skipping 19 matching lines...) Expand all Loading... |
50 const FindRegistrationCallback& callback) { | 51 const FindRegistrationCallback& callback) { |
51 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NOT_FOUND; | 52 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NOT_FOUND; |
52 scoped_refptr<ServiceWorkerRegistration> found; | 53 scoped_refptr<ServiceWorkerRegistration> found; |
53 PatternToRegistrationMap::const_iterator match = | 54 PatternToRegistrationMap::const_iterator match = |
54 registration_by_pattern_.find(pattern); | 55 registration_by_pattern_.find(pattern); |
55 if (match != registration_by_pattern_.end()) { | 56 if (match != registration_by_pattern_.end()) { |
56 status = SERVICE_WORKER_OK; | 57 status = SERVICE_WORKER_OK; |
57 found = match->second; | 58 found = match->second; |
58 } | 59 } |
59 // Always simulate asynchronous call for now. | 60 // Always simulate asynchronous call for now. |
60 RunSoon(base::Bind(callback, status, found)); | 61 RunSoon(FROM_HERE, base::Bind(callback, status, found)); |
61 } | 62 } |
62 | 63 |
63 void ServiceWorkerStorage::FindRegistrationForDocument( | 64 void ServiceWorkerStorage::FindRegistrationForDocument( |
64 const GURL& document_url, | 65 const GURL& document_url, |
65 const FindRegistrationCallback& callback) { | 66 const FindRegistrationCallback& callback) { |
66 // TODO(alecflett): This needs to be synchronous in the fast path, | 67 // TODO(alecflett): This needs to be synchronous in the fast path, |
67 // but asynchronous in the slow path (when the patterns have to be | 68 // but asynchronous in the slow path (when the patterns have to be |
68 // loaded from disk). For now it is always pessimistically async. | 69 // loaded from disk). For now it is always pessimistically async. |
69 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NOT_FOUND; | 70 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NOT_FOUND; |
70 scoped_refptr<ServiceWorkerRegistration> found; | 71 scoped_refptr<ServiceWorkerRegistration> found; |
71 for (PatternToRegistrationMap::const_iterator it = | 72 for (PatternToRegistrationMap::const_iterator it = |
72 registration_by_pattern_.begin(); | 73 registration_by_pattern_.begin(); |
73 it != registration_by_pattern_.end(); | 74 it != registration_by_pattern_.end(); |
74 ++it) { | 75 ++it) { |
75 if (PatternMatches(it->first, document_url)) { | 76 if (PatternMatches(it->first, document_url)) { |
76 status = SERVICE_WORKER_OK; | 77 status = SERVICE_WORKER_OK; |
77 found = it->second; | 78 found = it->second; |
78 break; | 79 break; |
79 } | 80 } |
80 } | 81 } |
81 // Always simulate asynchronous call for now. | 82 // Always simulate asynchronous call for now. |
82 RunSoon(base::Bind(callback, status, found)); | 83 RunSoon(FROM_HERE, base::Bind(callback, status, found)); |
83 } | 84 } |
84 | 85 |
85 void ServiceWorkerStorage::GetAllRegistrations( | 86 void ServiceWorkerStorage::GetAllRegistrations( |
86 const GetAllRegistrationInfosCallback& callback) { | 87 const GetAllRegistrationInfosCallback& callback) { |
87 std::vector<ServiceWorkerRegistrationInfo> registrations; | 88 std::vector<ServiceWorkerRegistrationInfo> registrations; |
88 for (PatternToRegistrationMap::const_iterator it = | 89 for (PatternToRegistrationMap::const_iterator it = |
89 registration_by_pattern_.begin(); | 90 registration_by_pattern_.begin(); |
90 it != registration_by_pattern_.end(); | 91 it != registration_by_pattern_.end(); |
91 ++it) { | 92 ++it) { |
92 ServiceWorkerRegistration* registration(it->second.get()); | 93 ServiceWorkerRegistration* registration(it->second.get()); |
(...skipping 12 matching lines...) Expand all Loading... |
105 for (PatternToRegistrationMap::const_iterator it = | 106 for (PatternToRegistrationMap::const_iterator it = |
106 registration_by_pattern_.begin(); | 107 registration_by_pattern_.begin(); |
107 it != registration_by_pattern_.end(); | 108 it != registration_by_pattern_.end(); |
108 ++it) { | 109 ++it) { |
109 if (registration_id == it->second->id()) { | 110 if (registration_id == it->second->id()) { |
110 status = SERVICE_WORKER_OK; | 111 status = SERVICE_WORKER_OK; |
111 found = it->second; | 112 found = it->second; |
112 break; | 113 break; |
113 } | 114 } |
114 } | 115 } |
115 RunSoon(base::Bind(callback, status, found)); | 116 RunSoon(FROM_HERE, base::Bind(callback, status, found)); |
116 } | 117 } |
117 | 118 |
118 void ServiceWorkerStorage::StoreRegistration( | 119 void ServiceWorkerStorage::StoreRegistration( |
119 ServiceWorkerRegistration* registration, | 120 ServiceWorkerRegistration* registration, |
120 const StatusCallback& callback) { | 121 const StatusCallback& callback) { |
121 DCHECK(registration); | 122 DCHECK(registration); |
122 | 123 |
123 PatternToRegistrationMap::const_iterator current( | 124 PatternToRegistrationMap::const_iterator current( |
124 registration_by_pattern_.find(registration->pattern())); | 125 registration_by_pattern_.find(registration->pattern())); |
125 if (current != registration_by_pattern_.end() && | 126 if (current != registration_by_pattern_.end() && |
126 current->second->script_url() != registration->script_url()) { | 127 current->second->script_url() != registration->script_url()) { |
127 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_EXISTS)); | 128 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_EXISTS)); |
128 return; | 129 return; |
129 } | 130 } |
130 | 131 |
131 // This may update the existing registration information. | 132 // This may update the existing registration information. |
132 registration_by_pattern_[registration->pattern()] = registration; | 133 registration_by_pattern_[registration->pattern()] = registration; |
133 | 134 |
134 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 135 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_OK)); |
135 } | 136 } |
136 | 137 |
137 void ServiceWorkerStorage::DeleteRegistration( | 138 void ServiceWorkerStorage::DeleteRegistration( |
138 const GURL& pattern, | 139 const GURL& pattern, |
139 const StatusCallback& callback) { | 140 const StatusCallback& callback) { |
140 PatternToRegistrationMap::iterator match = | 141 PatternToRegistrationMap::iterator match = |
141 registration_by_pattern_.find(pattern); | 142 registration_by_pattern_.find(pattern); |
142 if (match == registration_by_pattern_.end()) { | 143 if (match == registration_by_pattern_.end()) { |
143 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_NOT_FOUND)); | 144 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_NOT_FOUND)); |
144 return; | 145 return; |
145 } | 146 } |
146 registration_by_pattern_.erase(match); | 147 registration_by_pattern_.erase(match); |
147 RunSoon(base::Bind(callback, SERVICE_WORKER_OK)); | 148 RunSoon(FROM_HERE, base::Bind(callback, SERVICE_WORKER_OK)); |
148 } | 149 } |
149 | 150 |
150 int64 ServiceWorkerStorage::NewRegistrationId() { | 151 int64 ServiceWorkerStorage::NewRegistrationId() { |
151 return ++last_registration_id_; | 152 return ++last_registration_id_; |
152 } | 153 } |
153 | 154 |
154 int64 ServiceWorkerStorage::NewVersionId() { | 155 int64 ServiceWorkerStorage::NewVersionId() { |
155 return ++last_version_id_; | 156 return ++last_version_id_; |
156 } | 157 } |
157 | 158 |
158 bool ServiceWorkerStorage::PatternMatches(const GURL& pattern, | 159 bool ServiceWorkerStorage::PatternMatches(const GURL& pattern, |
159 const GURL& url) { | 160 const GURL& url) { |
160 // This is a really basic, naive | 161 // This is a really basic, naive |
161 // TODO(alecflett): Formalize what pattern matches mean. | 162 // TODO(alecflett): Formalize what pattern matches mean. |
162 // Temporarily borrowed directly from appcache::Namespace::IsMatch(). | 163 // Temporarily borrowed directly from appcache::Namespace::IsMatch(). |
163 // We have to escape '?' characters since MatchPattern also treats those | 164 // We have to escape '?' characters since MatchPattern also treats those |
164 // as wildcards which we don't want here, we only do '*'s. | 165 // as wildcards which we don't want here, we only do '*'s. |
165 std::string pattern_spec(pattern.spec()); | 166 std::string pattern_spec(pattern.spec()); |
166 if (pattern.has_query()) | 167 if (pattern.has_query()) |
167 ReplaceSubstringsAfterOffset(&pattern_spec, 0, "?", "\\?"); | 168 ReplaceSubstringsAfterOffset(&pattern_spec, 0, "?", "\\?"); |
168 return MatchPattern(url.spec(), pattern_spec); | 169 return MatchPattern(url.spec(), pattern_spec); |
169 } | 170 } |
170 | 171 |
171 } // namespace content | 172 } // namespace content |
OLD | NEW |