 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 int64 NextRegistrationId() { | |
| 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 | 
| 39 ServiceWorkerContext::~ServiceWorkerContext() {} | |
| 40 | |
| 25 bool ServiceWorkerContext::IsEnabled() { | 41 bool ServiceWorkerContext::IsEnabled() { | 
| 26 return CommandLine::ForCurrentProcess()->HasSwitch( | 42 return CommandLine::ForCurrentProcess()->HasSwitch( | 
| 27 switches::kEnableServiceWorker); | 43 switches::kEnableServiceWorker); | 
| 28 } | 44 } | 
| 29 | 45 | 
| 30 ServiceWorkerContext::~ServiceWorkerContext() {} | 46 void ServiceWorkerContext::RegisterServiceWorker( | 
| 47 const GURL& pattern, | |
| 48 const GURL& script_url, | |
| 49 const ResponseCallback& callback) { | |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 51 | |
| 52 scoped_refptr<ServiceWorkerRegistration> registration( | |
| 53 RegistrationForPattern(pattern)); | |
| 54 if (!registration || registration->script_url() != script_url) { | |
| 55 Unregister(pattern); | |
| 56 registration = Register(pattern, script_url); | |
| 57 DCHECK(registration); | |
| 58 } | |
| 59 | |
| 60 BrowserThread::PostTask( | |
| 61 BrowserThread::IO, FROM_HERE, base::Bind(callback, registration->id())); | |
| 62 } | |
| 63 | |
| 64 void ServiceWorkerContext::UnregisterServiceWorker( | |
| 65 const GURL& pattern, | |
| 66 const base::Closure& callback) { | |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 68 | |
| 69 Unregister(pattern); | |
| 70 | |
| 71 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, callback); | |
| 72 } | |
| 73 | |
| 74 scoped_refptr<ServiceWorkerRegistration> | |
| 75 ServiceWorkerContext::RegistrationForDocument(const GURL& document_url) { | |
| 76 for (PatternToRegistrationMap::const_iterator iter = | |
| 77 registration_by_pattern_.begin(); | |
| 78 iter != registration_by_pattern_.end(); | |
| 79 ++iter) { | |
| 80 if (PatternMatches(iter->first, document_url)) | |
| 81 return iter->second; | |
| 82 } | |
| 83 return NULL; | |
| 84 } | |
| 85 | |
| 86 scoped_refptr<ServiceWorkerRegistration> | |
| 87 ServiceWorkerContext::RegistrationForPattern(const GURL& pattern) { | |
| 88 PatternToRegistrationMap::const_iterator match = | |
| 89 registration_by_pattern_.find(pattern); | |
| 90 if (match == registration_by_pattern_.end()) | |
| 91 return NULL; | |
| 92 return match->second; | |
| 93 } | |
| 94 | |
| 95 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerContext::Register( | |
| 96 const GURL& pattern, | |
| 97 const GURL& script_url) { | |
| 98 scoped_refptr<ServiceWorkerRegistration> registration( | |
| 99 new ServiceWorkerRegistration(pattern, script_url, NextRegistrationId())); | |
| 100 | |
| 101 DCHECK(registration_by_pattern_.find(pattern) == | |
| 102 registration_by_pattern_.end()); | |
| 103 // TODO(alecflett): version upgrade path. | |
| 104 registration_by_pattern_[pattern] = registration; | |
| 105 | |
| 106 return registration; | |
| 107 } | |
| 108 | |
| 109 void ServiceWorkerContext::Unregister(const GURL& pattern) { | |
| 110 scoped_refptr<ServiceWorkerRegistration> registration = | |
| 111 RegistrationForPattern(pattern); | |
| 112 if (registration) { | |
| 113 registration_by_pattern_.erase(pattern); | |
| 114 registration->Shutdown(); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 bool ServiceWorkerContext::PatternMatches(const GURL& pattern, | |
| 
michaeln
2013/11/11 23:48:24
Maybe we can get some use out of the MatchPattern
 | |
| 119 const GURL& url) { | |
| 120 // This is a really basic, naive | |
| 121 // TODO(alecflett): Formalize what pattern matches mean. | |
| 122 DCHECK(pattern.spec().length() > 0 && *pattern.spec().rbegin() == '*'); | |
| 123 StringPiece pattern_prefix(pattern.spec().begin(), pattern.spec().end()); | |
| 124 pattern_prefix.remove_suffix(1); | |
| 125 | |
| 126 return StringPiece(url.spec()).starts_with(pattern_prefix); | |
| 127 } | |
| 31 | 128 | 
| 32 } // namespace content | 129 } // namespace content | 
| OLD | NEW |