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 "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" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 } | 33 } |
| 34 | 34 |
| 35 scoped_ptr<ScriptInjection> | 35 scoped_ptr<ScriptInjection> |
| 36 UserScriptSetManager::GetInjectionForDeclarativeScript( | 36 UserScriptSetManager::GetInjectionForDeclarativeScript( |
| 37 int script_id, | 37 int script_id, |
| 38 blink::WebFrame* web_frame, | 38 blink::WebFrame* web_frame, |
| 39 int tab_id, | 39 int tab_id, |
| 40 const GURL& url, | 40 const GURL& url, |
| 41 const std::string& extension_id) { | 41 const std::string& extension_id) { |
| 42 UserScriptSet* user_script_set = | 42 UserScriptSet* user_script_set = |
| 43 GetProgrammaticScriptsByExtension(extension_id); | 43 GetProgrammaticScriptsByHostID(HostID(HostID::EXTENSIONS, extension_id)); |
| 44 if (!user_script_set) | 44 if (!user_script_set) |
| 45 return scoped_ptr<ScriptInjection>(); | 45 return scoped_ptr<ScriptInjection>(); |
| 46 | 46 |
| 47 return user_script_set->GetDeclarativeScriptInjection( | 47 return user_script_set->GetDeclarativeScriptInjection( |
| 48 script_id, | 48 script_id, |
| 49 web_frame, | 49 web_frame, |
| 50 tab_id, | 50 tab_id, |
| 51 UserScript::BROWSER_DRIVEN, | 51 UserScript::BROWSER_DRIVEN, |
| 52 url); | 52 url); |
| 53 } | 53 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 79 std::set<std::string>* ids) const { | 79 std::set<std::string>* ids) const { |
| 80 DCHECK(ids); | 80 DCHECK(ids); |
| 81 static_scripts_.GetActiveExtensionIds(ids); | 81 static_scripts_.GetActiveExtensionIds(ids); |
| 82 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); | 82 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); |
| 83 it != programmatic_scripts_.end(); | 83 it != programmatic_scripts_.end(); |
| 84 ++it) { | 84 ++it) { |
| 85 it->second->GetActiveExtensionIds(ids); | 85 it->second->GetActiveExtensionIds(ids); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension( | 89 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByHostID( |
| 90 const ExtensionId& extension_id) { | 90 const HostID& host_id) { |
| 91 UserScriptSetMap::const_iterator it = | 91 UserScriptSetMap::const_iterator it = programmatic_scripts_.find(host_id); |
| 92 programmatic_scripts_.find(extension_id); | |
| 93 return it != programmatic_scripts_.end() ? it->second.get() : NULL; | 92 return it != programmatic_scripts_.end() ? it->second.get() : NULL; |
| 94 } | 93 } |
| 95 | 94 |
| 96 void UserScriptSetManager::OnUpdateUserScripts( | 95 void UserScriptSetManager::OnUpdateUserScripts( |
| 97 base::SharedMemoryHandle shared_memory, | 96 base::SharedMemoryHandle shared_memory, |
| 98 const ExtensionId& extension_id, | 97 const HostID& host_id, |
| 99 const std::set<std::string>& changed_extensions) { | 98 const std::set<HostID>& changed_hosts) { |
| 100 if (!base::SharedMemory::IsHandleValid(shared_memory)) { | 99 if (!base::SharedMemory::IsHandleValid(shared_memory)) { |
| 101 NOTREACHED() << "Bad scripts handle"; | 100 NOTREACHED() << "Bad scripts handle"; |
| 102 return; | 101 return; |
| 103 } | 102 } |
| 104 | 103 |
| 105 for (std::set<std::string>::const_iterator iter = changed_extensions.begin(); | 104 for (std::set<HostID>::const_iterator iter = changed_hosts.begin(); |
|
Devlin
2015/03/09 16:00:05
range-based for-loop?
Xi Han
2015/03/09 19:12:36
Done.
| |
| 106 iter != changed_extensions.end(); | 105 iter != changed_hosts.end(); ++iter) { |
| 107 ++iter) { | 106 if (iter->type() == HostID::EXTENSIONS && |
| 108 if (!crx_file::id_util::IdIsValid(*iter)) { | 107 !crx_file::id_util::IdIsValid(iter->id())) { |
| 109 NOTREACHED() << "Invalid extension id: " << *iter; | 108 NOTREACHED() << "Invalid extension id: " << iter->id(); |
| 110 return; | 109 return; |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 UserScriptSet* scripts = NULL; | 113 UserScriptSet* scripts = NULL; |
| 115 if (!extension_id.empty()) { | 114 if (!host_id.id().empty()) { |
| 116 // The expectation when there is an extension that "owns" this shared | 115 // The expectation when there is an host that "owns" this shared |
|
Devlin
2015/03/09 16:00:05
a host, not an host.
Xi Han
2015/03/09 19:12:36
Done.
| |
| 117 // memory region is that the |changed_extensions| is either the empty list | 116 // memory region is that the |changed_hosts| is either the empty list |
| 118 // or just the owner. | 117 // or just the owner. |
| 119 CHECK(changed_extensions.size() <= 1); | 118 CHECK(changed_hosts.size() <= 1); |
| 120 if (programmatic_scripts_.find(extension_id) == | 119 if (programmatic_scripts_.find(host_id) == programmatic_scripts_.end()) { |
| 121 programmatic_scripts_.end()) { | |
| 122 scripts = new UserScriptSet(extensions_); | 120 scripts = new UserScriptSet(extensions_); |
| 123 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); | 121 programmatic_scripts_[host_id] = make_linked_ptr(scripts); |
| 124 } else { | 122 } else { |
| 125 scripts = programmatic_scripts_[extension_id].get(); | 123 scripts = programmatic_scripts_[host_id].get(); |
| 126 } | 124 } |
| 127 } else { | 125 } else { |
| 128 scripts = &static_scripts_; | 126 scripts = &static_scripts_; |
| 129 } | 127 } |
| 130 DCHECK(scripts); | 128 DCHECK(scripts); |
| 131 | 129 |
| 132 // If no extensions are included in the set, that indicates that all | 130 // If no hosts are included in the set, that indicates that all |
| 133 // extensions were updated. Add them all to the set so that observers and | 131 // hosts were updated. Add them all to the set so that observers and |
| 134 // individual UserScriptSets don't need to know this detail. | 132 // individual UserScriptSets don't need to know this detail. |
| 135 const std::set<std::string>* effective_extensions = &changed_extensions; | 133 const std::set<HostID>* effective_hosts = &changed_hosts; |
| 136 std::set<std::string> all_extensions; | 134 std::set<HostID> all_hosts; |
| 137 if (changed_extensions.empty()) { | 135 if (changed_hosts.empty()) { |
| 138 // The meaning of "all extensions" varies, depending on whether some | 136 // The meaning of "all hosts(extensions)" varies, depending on whether some |
| 139 // extension "owns" this shared memory region. | 137 // host "owns" this shared memory region. |
| 140 // No owner => all known extensions. | 138 // No owner => all known hosts. |
| 141 // Owner => just the owner extension. | 139 // Owner => just the owner host. |
| 142 if (extension_id.empty()) | 140 if (host_id.id().empty()) |
| 143 all_extensions = extensions_->GetIDs(); | 141 all_hosts = extensions_->GetHostIDs(); |
| 144 else | 142 else |
| 145 all_extensions.insert(extension_id); | 143 all_hosts.insert(host_id); |
| 146 effective_extensions = &all_extensions; | 144 effective_hosts = &all_hosts; |
| 147 } | 145 } |
| 148 | 146 |
| 149 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { | 147 if (scripts->UpdateUserScripts(shared_memory, *effective_hosts)) { |
| 150 FOR_EACH_OBSERVER( | 148 FOR_EACH_OBSERVER( |
| 151 Observer, | 149 Observer, |
| 152 observers_, | 150 observers_, |
| 153 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); | 151 OnUserScriptsUpdated(*effective_hosts, scripts->scripts())); |
| 154 } | 152 } |
| 155 } | 153 } |
| 156 | 154 |
| 157 } // namespace extensions | 155 } // namespace extensions |
| OLD | NEW |