| 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 "extensions/renderer/user_script_set_manager.h" | 5 #include "extensions/renderer/user_script_set_manager.h" |
| 6 | 6 |
| 7 #include "components/crx_file/id_util.h" | 7 #include "components/crx_file/id_util.h" |
| 8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
| 9 #include "extensions/common/extension_messages.h" | 9 #include "extensions/common/extension_messages.h" |
| 10 #include "extensions/renderer/dispatcher.h" | 10 #include "extensions/renderer/dispatcher.h" |
| 11 #include "extensions/renderer/script_injection.h" | 11 #include "extensions/renderer/script_injection.h" |
| 12 #include "extensions/renderer/user_script_set.h" | 12 #include "extensions/renderer/user_script_set.h" |
| 13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
| 14 #include "ipc/ipc_message_macros.h" | 14 #include "ipc/ipc_message_macros.h" |
| 15 | 15 |
| 16 namespace extensions { | 16 namespace extensions { |
| 17 | 17 |
| 18 UserScriptSetManager::UserScriptSetManager(const ExtensionSet* extensions) | 18 UserScriptSetManager::UserScriptSetManager() { |
| 19 : static_scripts_(extensions), | |
| 20 extensions_(extensions) { | |
| 21 content::RenderThread::Get()->AddObserver(this); | 19 content::RenderThread::Get()->AddObserver(this); |
| 22 } | 20 } |
| 23 | 21 |
| 24 UserScriptSetManager::~UserScriptSetManager() { | 22 UserScriptSetManager::~UserScriptSetManager() { |
| 25 } | 23 } |
| 26 | 24 |
| 27 void UserScriptSetManager::AddObserver(Observer* observer) { | 25 void UserScriptSetManager::AddObserver(Observer* observer) { |
| 28 observers_.AddObserver(observer); | 26 observers_.AddObserver(observer); |
| 29 } | 27 } |
| 30 | 28 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 108 } |
| 111 } | 109 } |
| 112 | 110 |
| 113 UserScriptSet* scripts = NULL; | 111 UserScriptSet* scripts = NULL; |
| 114 if (!host_id.id().empty()) { | 112 if (!host_id.id().empty()) { |
| 115 // The expectation when there is a host that "owns" this shared | 113 // The expectation when there is a host that "owns" this shared |
| 116 // memory region is that the |changed_hosts| is either the empty list | 114 // memory region is that the |changed_hosts| is either the empty list |
| 117 // or just the owner. | 115 // or just the owner. |
| 118 CHECK(changed_hosts.size() <= 1); | 116 CHECK(changed_hosts.size() <= 1); |
| 119 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) { | 117 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) { |
| 120 scripts = new UserScriptSet(extensions_); | 118 scripts = new UserScriptSet(); |
| 121 programmatic_scripts_[host_id] = make_linked_ptr(scripts); | 119 programmatic_scripts_[host_id] = make_linked_ptr(scripts); |
| 122 } else { | 120 } else { |
| 123 scripts = programmatic_scripts_[host_id].get(); | 121 scripts = programmatic_scripts_[host_id].get(); |
| 124 } | 122 } |
| 125 } else { | 123 } else { |
| 126 scripts = &static_scripts_; | 124 scripts = &static_scripts_; |
| 127 } | 125 } |
| 128 DCHECK(scripts); | 126 DCHECK(scripts); |
| 129 | 127 |
| 130 // If no hosts are included in the set, that indicates that all | 128 // If no hosts are included in the set, that indicates that all |
| 131 // hosts were updated. Add them all to the set so that observers and | 129 // hosts were updated. Add them all to the set so that observers and |
| 132 // individual UserScriptSets don't need to know this detail. | 130 // individual UserScriptSets don't need to know this detail. |
| 133 const std::set<HostID>* effective_hosts = &changed_hosts; | 131 const std::set<HostID>* effective_hosts = &changed_hosts; |
| 134 std::set<HostID> all_hosts; | 132 std::set<HostID> all_hosts; |
| 135 if (changed_hosts.empty()) { | 133 if (changed_hosts.empty()) { |
| 136 // The meaning of "all hosts(extensions)" varies, depending on whether some | 134 // The meaning of "all hosts(extensions)" varies, depending on whether some |
| 137 // host "owns" this shared memory region. | 135 // host "owns" this shared memory region. |
| 138 // No owner => all known hosts. | 136 // No owner => all known hosts. |
| 139 // Owner => just the owner host. | 137 // Owner => just the owner host. |
| 140 if (host_id.id().empty()) { | 138 if (host_id.id().empty()) { |
| 141 std::set<std::string> extension_ids = extensions_->GetIDs(); | 139 std::set<std::string> extension_ids = |
| 140 RendererExtensionRegistry::Get()->GetIDs(); |
| 142 for (const std::string& extension_id : extension_ids) | 141 for (const std::string& extension_id : extension_ids) |
| 143 all_hosts.insert(HostID(HostID::EXTENSIONS, extension_id)); | 142 all_hosts.insert(HostID(HostID::EXTENSIONS, extension_id)); |
| 144 } else { | 143 } else { |
| 145 all_hosts.insert(host_id); | 144 all_hosts.insert(host_id); |
| 146 } | 145 } |
| 147 effective_hosts = &all_hosts; | 146 effective_hosts = &all_hosts; |
| 148 } | 147 } |
| 149 | 148 |
| 150 if (scripts->UpdateUserScripts(shared_memory, | 149 if (scripts->UpdateUserScripts(shared_memory, |
| 151 *effective_hosts, | 150 *effective_hosts, |
| 152 whitelisted_only)) { | 151 whitelisted_only)) { |
| 153 FOR_EACH_OBSERVER( | 152 FOR_EACH_OBSERVER( |
| 154 Observer, | 153 Observer, |
| 155 observers_, | 154 observers_, |
| 156 OnUserScriptsUpdated(*effective_hosts, scripts->scripts())); | 155 OnUserScriptsUpdated(*effective_hosts, scripts->scripts())); |
| 157 } | 156 } |
| 158 } | 157 } |
| 159 | 158 |
| 160 } // namespace extensions | 159 } // namespace extensions |
| OLD | NEW |