Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/extensions/shared_user_script_master.h" | 5 #include "chrome/browser/extensions/shared_user_script_master.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | |
| 7 #include "chrome/browser/extensions/extension_util.h" | 8 #include "chrome/browser/extensions/extension_util.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" | 10 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
| 10 #include "extensions/browser/extension_registry.h" | 11 #include "extensions/browser/extension_registry.h" |
| 11 #include "extensions/common/host_id.h" | 12 #include "extensions/common/host_id.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 15 SharedUserScriptMaster::SharedUserScriptMaster(Profile* profile) | 16 SharedUserScriptMaster::SharedUserScriptMaster(Profile* profile) |
| 16 : loader_(profile, | 17 : loader_(profile, |
| 17 HostID(), | 18 HostID(), |
| 18 true /* listen_for_extension_system_loaded */), | 19 true /* listen_for_extension_system_loaded */), |
| 19 profile_(profile), | 20 profile_(profile), |
| 20 extension_registry_observer_(this) { | 21 extension_registry_observer_(this) { |
| 21 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | 22 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 22 } | 23 } |
| 23 | 24 |
| 24 SharedUserScriptMaster::~SharedUserScriptMaster() { | 25 SharedUserScriptMaster::~SharedUserScriptMaster() { |
| 25 } | 26 } |
| 26 | 27 |
| 27 void SharedUserScriptMaster::OnExtensionLoaded( | 28 void SharedUserScriptMaster::OnExtensionLoaded( |
| 28 content::BrowserContext* browser_context, | 29 content::BrowserContext* browser_context, |
| 29 const Extension* extension) { | 30 const Extension* extension) { |
| 30 loader_.AddScripts(GetScriptsMetadata(extension)); | 31 UserScriptList list = GetScriptsMetadata(extension); |
| 32 loader_.AddScripts(list); | |
|
Devlin
2016/08/17 16:39:30
nit: inline list?
lazyboy
2016/08/17 18:55:52
We need an l-value here since AddScripts takes a (
| |
| 31 } | 33 } |
| 32 | 34 |
| 33 void SharedUserScriptMaster::OnExtensionUnloaded( | 35 void SharedUserScriptMaster::OnExtensionUnloaded( |
| 34 content::BrowserContext* browser_context, | 36 content::BrowserContext* browser_context, |
| 35 const Extension* extension, | 37 const Extension* extension, |
| 36 UnloadedExtensionInfo::Reason reason) { | 38 UnloadedExtensionInfo::Reason reason) { |
| 37 const UserScriptList& script_list = | 39 const UserScriptList& script_list = |
| 38 ContentScriptsInfo::GetContentScripts(extension); | 40 ContentScriptsInfo::GetContentScripts(extension); |
| 39 std::set<UserScriptIDPair> scripts_to_remove; | 41 std::set<UserScriptIDPair> scripts_to_remove; |
| 40 for (const UserScript& script : script_list) | 42 for (const std::unique_ptr<UserScript>& script : script_list) |
| 41 scripts_to_remove.insert(UserScriptIDPair(script.id(), script.host_id())); | 43 scripts_to_remove.insert(UserScriptIDPair(script->id(), script->host_id())); |
| 42 loader_.RemoveScripts(scripts_to_remove); | 44 loader_.RemoveScripts(scripts_to_remove); |
| 43 } | 45 } |
| 44 | 46 |
| 45 const std::vector<UserScript> SharedUserScriptMaster::GetScriptsMetadata( | 47 UserScriptList SharedUserScriptMaster::GetScriptsMetadata( |
| 46 const Extension* extension) { | 48 const Extension* extension) { |
| 47 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_); | 49 bool incognito_enabled = util::IsIncognitoEnabled(extension->id(), profile_); |
| 48 const UserScriptList& script_list = | 50 const UserScriptList& script_list = |
| 49 ContentScriptsInfo::GetContentScripts(extension); | 51 ContentScriptsInfo::GetContentScripts(extension); |
| 50 std::vector<UserScript> scripts; | 52 std::vector<std::unique_ptr<UserScript>> script_vector; |
|
Devlin
2016/08/17 16:39:30
nit: UserScriptList?
lazyboy
2016/08/17 18:55:52
Done.
| |
| 51 scripts.reserve(script_list.size()); | 53 script_vector.reserve(script_list.size()); |
| 52 for (const UserScript& script : script_list) { | 54 for (const std::unique_ptr<UserScript>& meta : script_list) { |
| 53 scripts.push_back(UserScript(script)); | 55 std::unique_ptr<UserScript> data = UserScript::CopyFrom(*meta); |
| 54 scripts.back().set_incognito_enabled(incognito_enabled); | 56 data->set_incognito_enabled(incognito_enabled); |
|
Devlin
2016/08/17 16:39:30
(Not related to this patch set, but...)
This seems
lazyboy
2016/08/17 18:55:52
Added TODO in user_script.h, will follow up.
| |
| 57 script_vector.push_back(std::move(data)); | |
| 55 } | 58 } |
| 56 | 59 return script_vector; |
| 57 return scripts; | |
| 58 } | 60 } |
| 59 | 61 |
| 60 } // namespace extensions | 62 } // namespace extensions |
| OLD | NEW |