 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| 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_context.h" | 5 #include "content/browser/service_worker/service_worker_context.h" | 
| 6 | 6 | 
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" | 
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" | 
| 9 #include "base/strings/string_piece.h" | |
| 10 #include "content/browser/service_worker/service_worker_version.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 9 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" | 
| 13 #include "url/gurl.h" | |
| 10 #include "webkit/browser/quota/quota_manager.h" | 14 #include "webkit/browser/quota/quota_manager.h" | 
| 11 | 15 | 
| 16 using base::StringPiece; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 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.
 | |
| 21 static int64 worker_id = 0; | |
| 22 return worker_id++; | |
| 23 } | |
| 24 } | |
| 25 | |
| 12 namespace content { | 26 namespace content { | 
| 13 | 27 | 
| 14 const base::FilePath::CharType kServiceWorkerDirectory[] = | 28 const base::FilePath::CharType kServiceWorkerDirectory[] = | 
| 15 FILE_PATH_LITERAL("ServiceWorker"); | 29 FILE_PATH_LITERAL("ServiceWorker"); | 
| 16 | 30 | 
| 17 ServiceWorkerContext::ServiceWorkerContext( | 31 ServiceWorkerContext::ServiceWorkerContext( | 
| 18 const base::FilePath& path, | 32 const base::FilePath& path, | 
| 19 quota::QuotaManagerProxy* quota_manager_proxy) | 33 quota::QuotaManagerProxy* quota_manager_proxy) | 
| 20 : quota_manager_proxy_(quota_manager_proxy) { | 34 : quota_manager_proxy_(quota_manager_proxy) { | 
| 21 if (!path.empty()) | 35 if (!path.empty()) | 
| 22 path_ = path.Append(kServiceWorkerDirectory); | 36 path_ = path.Append(kServiceWorkerDirectory); | 
| 23 } | 37 } | 
| 24 | 38 | 
| 25 bool ServiceWorkerContext::IsEnabled() { | 39 bool ServiceWorkerContext::IsEnabled() { | 
| 26 return CommandLine::ForCurrentProcess()->HasSwitch( | 40 return CommandLine::ForCurrentProcess()->HasSwitch( | 
| 27 switches::kEnableServiceWorker); | 41 switches::kEnableServiceWorker); | 
| 28 } | 42 } | 
| 29 | 43 | 
| 30 ServiceWorkerContext::~ServiceWorkerContext() {} | 44 ServiceWorkerContext::~ServiceWorkerContext() {} | 
| 31 | 45 | 
| 
kinuko
2013/11/08 09:08:21
Please match the method order same as in header.
 
alecflett
2013/11/08 22:50:54
Done.
 | |
| 46 scoped_refptr<ServiceWorkerRegistration> | |
| 47 ServiceWorkerContext::RegistrationForDocument(const GURL& document_url) { | |
| 48 for (PatternToRegistrationMap::const_iterator iter = | |
| 49 registration_by_pattern_.begin(); | |
| 50 iter != registration_by_pattern_.end(); | |
| 51 ++iter) { | |
| 52 if (PatternMatches(iter->first, document_url)) | |
| 53 return iter->second; | |
| 54 } | |
| 55 return NULL; | |
| 56 } | |
| 57 | |
| 58 ServiceWorkerRegistration* ServiceWorkerContext::Register( | |
| 59 const GURL& pattern, | |
| 60 const GURL& script_url) { | |
| 61 scoped_refptr<ServiceWorkerRegistration> registration( | |
| 62 new ServiceWorkerRegistration(pattern, script_url, NextRegistrationId())); | |
| 63 | |
| 64 scoped_refptr<ServiceWorkerRegistration> previous_registration( | |
| 65 RegistrationForPattern(pattern)); | |
| 66 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)
 | |
| 67 previous_registration->Shutdown(); | |
| 68 previous_registration = NULL; | |
| 69 } | |
| 70 // This gets much more complicated if there is already a service | |
| 71 // worker here, especially if it is also running. For now we just | |
| 72 // clobber the one that is there, if any. | |
| 73 // TODO(alecflett): version upgrade path. | |
| 74 registration_by_pattern_[pattern] = registration; | |
| 75 | |
| 76 return registration; | |
| 77 } | |
| 78 | |
| 79 void ServiceWorkerContext::RegisterServiceWorker( | |
| 80 const GURL& pattern, | |
| 81 const GURL& script_url, | |
| 82 const ResponseCallback& callback) { | |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 84 | |
| 85 Unregister(pattern); | |
| 86 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
 | |
| 87 DCHECK(registration); | |
| 88 | |
| 89 BrowserThread::PostTask( | |
| 90 BrowserThread::IO, FROM_HERE, base::Bind(callback, registration->id())); | |
| 91 } | |
| 92 | |
| 93 void ServiceWorkerContext::UnregisterServiceWorker( | |
| 94 const GURL& pattern, | |
| 95 const base::Closure& callback) { | |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 97 | |
| 98 Unregister(pattern); | |
| 99 | |
| 100 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); | |
| 101 } | |
| 102 | |
| 103 void ServiceWorkerContext::Unregister(const GURL& pattern) { | |
| 104 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 105 RegistrationForPattern(pattern); | |
| 106 if (registration) { | |
| 107 registration_by_pattern_.erase(pattern); | |
| 108 registration->Shutdown(); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 scoped_refptr<ServiceWorkerRegistration> | |
| 113 ServiceWorkerContext::RegistrationForPattern(const GURL& pattern) { | |
| 114 PatternToRegistrationMap::const_iterator match = | |
| 115 registration_by_pattern_.find(pattern); | |
| 116 if (match == registration_by_pattern_.end()) | |
| 117 return NULL; | |
| 118 return match->second; | |
| 119 } | |
| 120 | |
| 121 bool ServiceWorkerContext::PatternMatches(const GURL& pattern, | |
| 122 const GURL& url) { | |
| 123 DCHECK(pattern.spec().length() > 0 && *pattern.spec().rbegin() == '*'); | |
| 124 StringPiece pattern_prefix(pattern.spec().begin(), pattern.spec().end()); | |
| 125 pattern_prefix.remove_suffix(1); | |
| 126 | |
| 127 return StringPiece(url.spec()).starts_with(pattern_prefix); | |
| 128 } | |
| 129 | |
| 32 } // namespace content | 130 } // namespace content | 
| OLD | NEW |