• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "content/browser/service_worker/service_worker_context_core.h"
6 
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/strings/string_util.h"
10 #include "content/browser/service_worker/embedded_worker_registry.h"
11 #include "content/browser/service_worker/service_worker_provider_host.h"
12 #include "content/browser/service_worker/service_worker_register_job.h"
13 #include "content/browser/service_worker/service_worker_registration.h"
14 #include "content/browser/service_worker/service_worker_storage.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/common/content_switches.h"
17 #include "url/gurl.h"
18 
19 namespace content {
20 
ServiceWorkerContextCore(const base::FilePath & path,quota::QuotaManagerProxy * quota_manager_proxy)21 ServiceWorkerContextCore::ServiceWorkerContextCore(
22     const base::FilePath& path,
23     quota::QuotaManagerProxy* quota_manager_proxy)
24     : storage_(new ServiceWorkerStorage(path, quota_manager_proxy)),
25       embedded_worker_registry_(new EmbeddedWorkerRegistry(AsWeakPtr())) {}
26 
~ServiceWorkerContextCore()27 ServiceWorkerContextCore::~ServiceWorkerContextCore() {}
28 
GetProviderHost(int process_id,int provider_id)29 ServiceWorkerProviderHost* ServiceWorkerContextCore::GetProviderHost(
30     int process_id, int provider_id) {
31   ProviderMap* map = GetProviderMapForProcess(process_id);
32   if (!map)
33     return NULL;
34   return map->Lookup(provider_id);
35 }
36 
AddProviderHost(scoped_ptr<ServiceWorkerProviderHost> host)37 void ServiceWorkerContextCore::AddProviderHost(
38     scoped_ptr<ServiceWorkerProviderHost> host) {
39   ServiceWorkerProviderHost* host_ptr = host.release();   // we take ownership
40   ProviderMap* map = GetProviderMapForProcess(host_ptr->process_id());
41   if (!map) {
42     map = new ProviderMap;
43     providers_.AddWithID(map, host_ptr->process_id());
44   }
45   map->AddWithID(host_ptr, host_ptr->provider_id());
46 }
47 
RemoveProviderHost(int process_id,int provider_id)48 void ServiceWorkerContextCore::RemoveProviderHost(
49     int process_id, int provider_id) {
50   ProviderMap* map = GetProviderMapForProcess(process_id);
51   DCHECK(map);
52   map->Remove(provider_id);
53 }
54 
RemoveAllProviderHostsForProcess(int process_id)55 void ServiceWorkerContextCore::RemoveAllProviderHostsForProcess(
56     int process_id) {
57   if (providers_.Lookup(process_id))
58     providers_.Remove(process_id);
59 }
60 
IsEnabled()61 bool ServiceWorkerContextCore::IsEnabled() {
62   return CommandLine::ForCurrentProcess()->HasSwitch(
63       switches::kEnableServiceWorker);
64 }
65 
RegisterServiceWorker(const GURL & pattern,const GURL & script_url,const RegistrationCallback & callback)66 void ServiceWorkerContextCore::RegisterServiceWorker(
67     const GURL& pattern,
68     const GURL& script_url,
69     const RegistrationCallback& callback) {
70   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
71 
72   storage_->Register(pattern,
73                      script_url,
74                      base::Bind(&ServiceWorkerContextCore::RegistrationComplete,
75                                 AsWeakPtr(),
76                                 callback));
77 }
78 
UnregisterServiceWorker(const GURL & pattern,const UnregistrationCallback & callback)79 void ServiceWorkerContextCore::UnregisterServiceWorker(
80     const GURL& pattern,
81     const UnregistrationCallback& callback) {
82   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
83 
84   storage_->Unregister(pattern, callback);
85 }
86 
RegistrationComplete(const ServiceWorkerContextCore::RegistrationCallback & callback,ServiceWorkerRegistrationStatus status,const scoped_refptr<ServiceWorkerRegistration> & registration)87 void ServiceWorkerContextCore::RegistrationComplete(
88     const ServiceWorkerContextCore::RegistrationCallback& callback,
89     ServiceWorkerRegistrationStatus status,
90     const scoped_refptr<ServiceWorkerRegistration>& registration) {
91   if (status != REGISTRATION_OK) {
92     DCHECK(!registration);
93     callback.Run(status, -1L);
94   }
95 
96   callback.Run(status, registration->id());
97 }
98 
99 }  // namespace content
100