1 // Copyright 2014 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/declarative_user_script_manager.h"
7 #include "content/public/browser/browser_context.h"
8 #include "extensions/browser/declarative_user_script_master.h"
9 #include "extensions/browser/extension_registry.h"
11 namespace extensions
{
13 DeclarativeUserScriptManager::DeclarativeUserScriptManager(
14 content::BrowserContext
* browser_context
)
15 : browser_context_(browser_context
), extension_registry_observer_(this) {
16 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context
));
19 DeclarativeUserScriptManager::~DeclarativeUserScriptManager() {
22 DeclarativeUserScriptMaster
*
23 DeclarativeUserScriptManager::GetDeclarativeUserScriptMasterByID(
24 const HostID
& host_id
) {
25 UserScriptMasterMap::iterator it
=
26 declarative_user_script_masters_
.find(host_id
);
28 if (it
!= declarative_user_script_masters_
.end())
29 return it
->second
.get();
31 return CreateDeclarativeUserScriptMaster(host_id
);
34 DeclarativeUserScriptMaster
*
35 DeclarativeUserScriptManager::CreateDeclarativeUserScriptMaster(
36 const HostID
& host_id
) {
37 linked_ptr
<DeclarativeUserScriptMaster
> master(
38 new DeclarativeUserScriptMaster(browser_context_
, host_id
));
39 declarative_user_script_masters_
[host_id
] = master
;
43 void DeclarativeUserScriptManager::OnExtensionUnloaded(
44 content::BrowserContext
* browser_context
,
45 const Extension
* extension
,
46 UnloadedExtensionInfo::Reason reason
) {
47 for (const auto& val
: declarative_user_script_masters_
) {
48 DeclarativeUserScriptMaster
* master
= val
.second
.get();
49 if (master
->host_id().id() == extension
->id())
50 master
->ClearScripts();
54 } // namespace extensions