| 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 | 8 |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "content/browser/service_worker/service_worker_register_job.h" | |
| 11 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 13 #include "webkit/browser/quota/quota_manager.h" | 12 #include "webkit/browser/quota/quota_manager.h" |
| 14 | 13 |
| 15 namespace { | 14 namespace { |
| 16 // This is temporary until we figure out how registration ids will be | 15 // This is temporary until we figure out how registration ids will be |
| 17 // calculated. | 16 // calculated. |
| 18 int64 NextRegistrationId() { | 17 int64 NextRegistrationId() { |
| 19 static int64 worker_id = 0; | 18 static int64 worker_id = 0; |
| 20 return worker_id++; | 19 return worker_id++; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 87 } |
| 89 BrowserThread::PostTask( | 88 BrowserThread::PostTask( |
| 90 BrowserThread::IO, | 89 BrowserThread::IO, |
| 91 FROM_HERE, | 90 FROM_HERE, |
| 92 base::Bind(callback, | 91 base::Bind(callback, |
| 93 false /* found */, | 92 false /* found */, |
| 94 REGISTRATION_OK, | 93 REGISTRATION_OK, |
| 95 scoped_refptr<ServiceWorkerRegistration>())); | 94 scoped_refptr<ServiceWorkerRegistration>())); |
| 96 } | 95 } |
| 97 | 96 |
| 98 void ServiceWorkerStorage::Register(const GURL& pattern, | |
| 99 const GURL& script_url, | |
| 100 const RegistrationCallback& callback) { | |
| 101 scoped_ptr<ServiceWorkerRegisterJob> job(new ServiceWorkerRegisterJob( | |
| 102 weak_factory_.GetWeakPtr(), | |
| 103 base::Bind(&ServiceWorkerStorage::RegisterComplete, | |
| 104 weak_factory_.GetWeakPtr(), | |
| 105 callback))); | |
| 106 job->StartRegister(pattern, script_url); | |
| 107 registration_jobs_.push_back(job.release()); | |
| 108 } | |
| 109 | |
| 110 void ServiceWorkerStorage::Unregister(const GURL& pattern, | |
| 111 const UnregistrationCallback& callback) { | |
| 112 scoped_ptr<ServiceWorkerRegisterJob> job(new ServiceWorkerRegisterJob( | |
| 113 weak_factory_.GetWeakPtr(), | |
| 114 base::Bind(&ServiceWorkerStorage::UnregisterComplete, | |
| 115 weak_factory_.GetWeakPtr(), | |
| 116 callback))); | |
| 117 job->StartUnregister(pattern); | |
| 118 registration_jobs_.push_back(job.release()); | |
| 119 } | |
| 120 | |
| 121 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerStorage::RegisterInternal( | 97 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerStorage::RegisterInternal( |
| 122 const GURL& pattern, | 98 const GURL& pattern, |
| 123 const GURL& script_url) { | 99 const GURL& script_url) { |
| 124 | 100 |
| 125 PatternToRegistrationMap::const_iterator current( | 101 PatternToRegistrationMap::const_iterator current( |
| 126 registration_by_pattern_.find(pattern)); | 102 registration_by_pattern_.find(pattern)); |
| 127 DCHECK(current == registration_by_pattern_.end() || | 103 DCHECK(current == registration_by_pattern_.end() || |
| 128 current->second->script_url() == script_url); | 104 current->second->script_url() == script_url); |
| 129 | 105 |
| 130 if (current == registration_by_pattern_.end()) { | 106 if (current == registration_by_pattern_.end()) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 154 // TODO(alecflett): Formalize what pattern matches mean. | 130 // TODO(alecflett): Formalize what pattern matches mean. |
| 155 // Temporarily borrowed directly from appcache::Namespace::IsMatch(). | 131 // Temporarily borrowed directly from appcache::Namespace::IsMatch(). |
| 156 // We have to escape '?' characters since MatchPattern also treats those | 132 // We have to escape '?' characters since MatchPattern also treats those |
| 157 // as wildcards which we don't want here, we only do '*'s. | 133 // as wildcards which we don't want here, we only do '*'s. |
| 158 std::string pattern_spec(pattern.spec()); | 134 std::string pattern_spec(pattern.spec()); |
| 159 if (pattern.has_query()) | 135 if (pattern.has_query()) |
| 160 ReplaceSubstringsAfterOffset(&pattern_spec, 0, "?", "\\?"); | 136 ReplaceSubstringsAfterOffset(&pattern_spec, 0, "?", "\\?"); |
| 161 return MatchPattern(url.spec(), pattern_spec); | 137 return MatchPattern(url.spec(), pattern_spec); |
| 162 } | 138 } |
| 163 | 139 |
| 164 void ServiceWorkerStorage::EraseJob(ServiceWorkerRegisterJob* job) { | |
| 165 ScopedVector<ServiceWorkerRegisterJob>::iterator job_position = | |
| 166 registration_jobs_.begin(); | |
| 167 for (; job_position != registration_jobs_.end(); ++job_position) { | |
| 168 if (*job_position == job) { | |
| 169 registration_jobs_.erase(job_position); | |
| 170 return; | |
| 171 } | |
| 172 } | |
| 173 NOTREACHED() << "Deleting non-existent job. "; | |
| 174 } | |
| 175 | |
| 176 void ServiceWorkerStorage::UnregisterComplete( | |
| 177 const UnregistrationCallback& callback, | |
| 178 ServiceWorkerRegisterJob* job, | |
| 179 ServiceWorkerRegistrationStatus status, | |
| 180 ServiceWorkerRegistration* previous_registration) { | |
| 181 callback.Run(status); | |
| 182 EraseJob(job); | |
| 183 } | |
| 184 | |
| 185 void ServiceWorkerStorage::RegisterComplete( | |
| 186 const RegistrationCallback& callback, | |
| 187 ServiceWorkerRegisterJob* job, | |
| 188 ServiceWorkerRegistrationStatus status, | |
| 189 ServiceWorkerRegistration* registration) { | |
| 190 callback.Run(status, registration); | |
| 191 EraseJob(job); | |
| 192 } | |
| 193 | |
| 194 } // namespace content | 140 } // namespace content |
| OLD | NEW |