Chromium Code Reviews| Index: content/browser/service_worker/service_worker_context.cc |
| diff --git a/content/browser/service_worker/service_worker_context.cc b/content/browser/service_worker/service_worker_context.cc |
| index f61744f36bc148d828cb075f2ff72059cad5f073..9cc7ecfea6fed111ace0fd7552d709cd5b90ed30 100644 |
| --- a/content/browser/service_worker/service_worker_context.cc |
| +++ b/content/browser/service_worker/service_worker_context.cc |
| @@ -6,9 +6,23 @@ |
| #include "base/command_line.h" |
| #include "base/files/file_path.h" |
| +#include "base/strings/string_piece.h" |
| +#include "content/browser/service_worker/service_worker_version.h" |
| +#include "content/public/browser/browser_thread.h" |
| #include "content/public/common/content_switches.h" |
| +#include "url/gurl.h" |
| #include "webkit/browser/quota/quota_manager.h" |
| +using base::StringPiece; |
| + |
| +namespace { |
| + |
| +int64 NextRegistrationId() { |
| + static int64 worker_id = 0; |
| + return worker_id++; |
| +} |
| +} |
| + |
| namespace content { |
| const base::FilePath::CharType kServiceWorkerDirectory[] = |
| @@ -22,11 +36,94 @@ ServiceWorkerContext::ServiceWorkerContext( |
| path_ = path.Append(kServiceWorkerDirectory); |
| } |
| +ServiceWorkerContext::~ServiceWorkerContext() {} |
| + |
| bool ServiceWorkerContext::IsEnabled() { |
| return CommandLine::ForCurrentProcess()->HasSwitch( |
| switches::kEnableServiceWorker); |
| } |
| -ServiceWorkerContext::~ServiceWorkerContext() {} |
| +void ServiceWorkerContext::RegisterServiceWorker( |
| + const GURL& pattern, |
| + const GURL& script_url, |
| + const ResponseCallback& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + scoped_refptr<ServiceWorkerRegistration> registration( |
| + RegistrationForPattern(pattern)); |
| + if (!registration || registration->script_url() != script_url) { |
| + Unregister(pattern); |
| + registration = Register(pattern, script_url); |
| + DCHECK(registration); |
| + } |
| + |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, base::Bind(callback, registration->id())); |
| +} |
| + |
| +void ServiceWorkerContext::UnregisterServiceWorker( |
| + const GURL& pattern, |
| + const base::Closure& callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + Unregister(pattern); |
| + |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); |
| +} |
| + |
| +scoped_refptr<ServiceWorkerRegistration> |
| +ServiceWorkerContext::RegistrationForDocument(const GURL& document_url) { |
| + for (PatternToRegistrationMap::const_iterator iter = |
| + registration_by_pattern_.begin(); |
| + iter != registration_by_pattern_.end(); |
| + ++iter) { |
| + if (PatternMatches(iter->first, document_url)) |
| + return iter->second; |
| + } |
| + return NULL; |
| +} |
| + |
| +scoped_refptr<ServiceWorkerRegistration> |
| +ServiceWorkerContext::RegistrationForPattern(const GURL& pattern) { |
| + PatternToRegistrationMap::const_iterator match = |
| + registration_by_pattern_.find(pattern); |
| + if (match == registration_by_pattern_.end()) |
| + return NULL; |
| + return match->second; |
| +} |
| + |
| +scoped_refptr<ServiceWorkerRegistration> ServiceWorkerContext::Register( |
| + const GURL& pattern, |
| + const GURL& script_url) { |
| + scoped_refptr<ServiceWorkerRegistration> registration( |
| + new ServiceWorkerRegistration(pattern, script_url, NextRegistrationId())); |
| + |
| + DCHECK(registration_by_pattern_.find(pattern) == |
| + registration_by_pattern_.end()); |
| + // TODO(alecflett): version upgrade path. |
| + registration_by_pattern_[pattern] = registration; |
| + |
| + return registration; |
| +} |
| + |
| +void ServiceWorkerContext::Unregister(const GURL& pattern) { |
| + scoped_refptr<ServiceWorkerRegistration> registration = |
| + RegistrationForPattern(pattern); |
| + if (registration) { |
| + registration_by_pattern_.erase(pattern); |
| + registration->Shutdown(); |
| + } |
| +} |
| + |
| +bool ServiceWorkerContext::PatternMatches(const GURL& pattern, |
|
michaeln
2013/11/11 23:48:24
Maybe we can get some use out of the MatchPattern
|
| + const GURL& url) { |
| + // This is a really basic, naive |
| + // TODO(alecflett): Formalize what pattern matches mean. |
| + DCHECK(pattern.spec().length() > 0 && *pattern.spec().rbegin() == '*'); |
| + StringPiece pattern_prefix(pattern.spec().begin(), pattern.spec().end()); |
| + pattern_prefix.remove_suffix(1); |
| + |
| + return StringPiece(url.spec()).starts_with(pattern_prefix); |
| +} |
| } // namespace content |