 Chromium Code Reviews
 Chromium Code Reviews Issue 62203007:
  Implement memory-persistent registration  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 62203007:
  Implement memory-persistent registration  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| 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..4a19d245733b0047c8e35cdebaf954ca550329ce 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 { | 
| + | 
| +static int64 NextRegistrationId() { | 
| 
kinuko
2013/11/08 09:08:21
nit: no need of static in anonymous namespace
 
alecflett
2013/11/08 22:50:54
Done.
 | 
| + static int64 worker_id = 0; | 
| + return worker_id++; | 
| +} | 
| +} | 
| + | 
| namespace content { | 
| const base::FilePath::CharType kServiceWorkerDirectory[] = | 
| @@ -29,4 +43,88 @@ bool ServiceWorkerContext::IsEnabled() { | 
| ServiceWorkerContext::~ServiceWorkerContext() {} | 
| 
kinuko
2013/11/08 09:08:21
Please match the method order same as in header.
 
alecflett
2013/11/08 22:50:54
Done.
 | 
| +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; | 
| +} | 
| + | 
| +ServiceWorkerRegistration* ServiceWorkerContext::Register( | 
| + const GURL& pattern, | 
| + const GURL& script_url) { | 
| + scoped_refptr<ServiceWorkerRegistration> registration( | 
| + new ServiceWorkerRegistration(pattern, script_url, NextRegistrationId())); | 
| + | 
| + scoped_refptr<ServiceWorkerRegistration> previous_registration( | 
| + RegistrationForPattern(pattern)); | 
| + if (previous_registration) { | 
| 
kinuko
2013/11/08 09:08:21
We first unregister in the RegisterServiceWorker c
 
alecflett
2013/11/08 22:50:54
Done. (oops, leftover from early versions of this)
 | 
| + previous_registration->Shutdown(); | 
| + previous_registration = NULL; | 
| + } | 
| + // This gets much more complicated if there is already a service | 
| + // worker here, especially if it is also running. For now we just | 
| + // clobber the one that is there, if any. | 
| + // TODO(alecflett): version upgrade path. | 
| + registration_by_pattern_[pattern] = registration; | 
| + | 
| + return registration; | 
| +} | 
| + | 
| +void ServiceWorkerContext::RegisterServiceWorker( | 
| + const GURL& pattern, | 
| + const GURL& script_url, | 
| + const ResponseCallback& callback) { | 
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 
| + | 
| + Unregister(pattern); | 
| + ServiceWorkerRegistration* registration = Register(pattern, script_url); | 
| 
kinuko
2013/11/08 09:08:21
Do we always unregister and register a new one?
 
alecflett
2013/11/08 22:50:54
Yes. unregister is a no-op if there was no previou
 | 
| + 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); | 
| +} | 
| + | 
| +void ServiceWorkerContext::Unregister(const GURL& pattern) { | 
| + scoped_refptr<ServiceWorkerRegistration> registration = | 
| + RegistrationForPattern(pattern); | 
| + if (registration) { | 
| + registration_by_pattern_.erase(pattern); | 
| + registration->Shutdown(); | 
| + } | 
| +} | 
| + | 
| +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; | 
| +} | 
| + | 
| +bool ServiceWorkerContext::PatternMatches(const GURL& pattern, | 
| + const GURL& url) { | 
| + 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 |