1 // Copyright 2015 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.
5 #include "extensions/browser/service_worker_manager.h"
7 #include "content/public/browser/browser_context.h"
8 #include "content/public/browser/service_worker_context.h"
9 #include "content/public/browser/storage_partition.h"
10 #include "extensions/browser/extension_registry.h"
12 namespace extensions
{
15 void EmptySuccessCallback(bool success
) {}
18 ServiceWorkerManager::ServiceWorkerManager(
19 content::BrowserContext
* browser_context
)
20 : browser_context_(browser_context
), registry_observer_(this) {
21 registry_observer_
.Add(ExtensionRegistry::Get(browser_context_
));
24 ServiceWorkerManager::~ServiceWorkerManager() {}
26 void ServiceWorkerManager::OnExtensionUnloaded(
27 content::BrowserContext
* browser_context
,
28 const Extension
* extension
,
29 UnloadedExtensionInfo::Reason reason
) {
30 content::BrowserContext::GetStoragePartitionForSite(browser_context_
,
32 ->GetServiceWorkerContext()
33 ->StopAllServiceWorkersForOrigin(extension
->url());
36 void ServiceWorkerManager::OnExtensionUninstalled(
37 content::BrowserContext
* browser_context
,
38 const Extension
* extension
,
39 extensions::UninstallReason reason
) {
40 // TODO(devlin): Technically, this can fail. We should ideally:
41 // a) Keep track of extensions with registered service workers.
42 // b) Add a callback to the (Un)SuspendServiceWorkersOnOrigin() method.
43 // c) Check for any orphaned workers.
44 content::BrowserContext::GetStoragePartitionForSite(browser_context_
,
46 ->GetServiceWorkerContext()
47 ->DeleteForOrigin(extension
->url(), base::Bind(&EmptySuccessCallback
));
50 } // namespace extensions