| 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 13 matching lines...) Expand all Loading... |
| 24 } | 24 } |
| 25 | 25 |
| 26 void UserScriptSetManager::AddObserver(Observer* observer) { | 26 void UserScriptSetManager::AddObserver(Observer* observer) { |
| 27 observers_.AddObserver(observer); | 27 observers_.AddObserver(observer); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void UserScriptSetManager::RemoveObserver(Observer* observer) { | 30 void UserScriptSetManager::RemoveObserver(Observer* observer) { |
| 31 observers_.RemoveObserver(observer); | 31 observers_.RemoveObserver(observer); |
| 32 } | 32 } |
| 33 | 33 |
| 34 scoped_ptr<ScriptInjection> |
| 35 UserScriptSetManager::GetInjectionForDeclarativeScript( |
| 36 int64 script_id, |
| 37 blink::WebFrame* web_frame, |
| 38 int tab_id, |
| 39 const GURL& url, |
| 40 const Extension* extension) { |
| 41 UserScriptSet* user_script_set = |
| 42 GetProgrammaticScriptsByExtension(extension->id()); |
| 43 if (!user_script_set) |
| 44 return scoped_ptr<ScriptInjection>(); |
| 45 |
| 46 return user_script_set->GetInjectionByScriptID(script_id, |
| 47 web_frame, |
| 48 tab_id, |
| 49 UserScript::BROWSER_DRIVEN, |
| 50 url, |
| 51 extension); |
| 52 } |
| 53 |
| 34 bool UserScriptSetManager::OnControlMessageReceived( | 54 bool UserScriptSetManager::OnControlMessageReceived( |
| 35 const IPC::Message& message) { | 55 const IPC::Message& message) { |
| 36 bool handled = true; | 56 bool handled = true; |
| 37 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message) | 57 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message) |
| 38 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) | 58 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) |
| 39 IPC_MESSAGE_UNHANDLED(handled = false) | 59 IPC_MESSAGE_UNHANDLED(handled = false) |
| 40 IPC_END_MESSAGE_MAP() | 60 IPC_END_MESSAGE_MAP() |
| 41 return handled; | 61 return handled; |
| 42 } | 62 } |
| 43 | 63 |
| 44 const UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension( | |
| 45 const ExtensionId& extension_id) { | |
| 46 UserScriptSetMap::const_iterator it = | |
| 47 programmatic_scripts_.find(extension_id); | |
| 48 return it != programmatic_scripts_.end() ? it->second.get() : NULL; | |
| 49 } | |
| 50 | |
| 51 void UserScriptSetManager::GetAllInjections( | 64 void UserScriptSetManager::GetAllInjections( |
| 52 ScopedVector<ScriptInjection>* injections, | 65 ScopedVector<ScriptInjection>* injections, |
| 53 blink::WebFrame* web_frame, | 66 blink::WebFrame* web_frame, |
| 54 int tab_id, | 67 int tab_id, |
| 55 UserScript::RunLocation run_location) { | 68 UserScript::RunLocation run_location) { |
| 56 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location); | 69 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location); |
| 57 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin(); | 70 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin(); |
| 58 it != programmatic_scripts_.end(); | 71 it != programmatic_scripts_.end(); |
| 59 ++it) { | 72 ++it) { |
| 60 it->second->GetInjections(injections, web_frame, tab_id, run_location); | 73 it->second->GetInjections(injections, web_frame, tab_id, run_location); |
| 61 } | 74 } |
| 62 } | 75 } |
| 63 | 76 |
| 64 void UserScriptSetManager::GetAllActiveExtensionIds( | 77 void UserScriptSetManager::GetAllActiveExtensionIds( |
| 65 std::set<std::string>* ids) const { | 78 std::set<std::string>* ids) const { |
| 66 DCHECK(ids); | 79 DCHECK(ids); |
| 67 static_scripts_.GetActiveExtensionIds(ids); | 80 static_scripts_.GetActiveExtensionIds(ids); |
| 68 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); | 81 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); |
| 69 it != programmatic_scripts_.end(); | 82 it != programmatic_scripts_.end(); |
| 70 ++it) { | 83 ++it) { |
| 71 it->second->GetActiveExtensionIds(ids); | 84 it->second->GetActiveExtensionIds(ids); |
| 72 } | 85 } |
| 73 } | 86 } |
| 74 | 87 |
| 88 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension( |
| 89 const ExtensionId& extension_id) { |
| 90 UserScriptSetMap::const_iterator it = |
| 91 programmatic_scripts_.find(extension_id); |
| 92 return it != programmatic_scripts_.end() ? it->second.get() : NULL; |
| 93 } |
| 94 |
| 75 void UserScriptSetManager::OnUpdateUserScripts( | 95 void UserScriptSetManager::OnUpdateUserScripts( |
| 76 base::SharedMemoryHandle shared_memory, | 96 base::SharedMemoryHandle shared_memory, |
| 77 const ExtensionId& extension_id, | 97 const ExtensionId& extension_id, |
| 78 const std::set<std::string>& changed_extensions) { | 98 const std::set<std::string>& changed_extensions) { |
| 79 if (!base::SharedMemory::IsHandleValid(shared_memory)) { | 99 if (!base::SharedMemory::IsHandleValid(shared_memory)) { |
| 80 NOTREACHED() << "Bad scripts handle"; | 100 NOTREACHED() << "Bad scripts handle"; |
| 81 return; | 101 return; |
| 82 } | 102 } |
| 83 | 103 |
| 84 for (std::set<std::string>::const_iterator iter = changed_extensions.begin(); | 104 for (std::set<std::string>::const_iterator iter = changed_extensions.begin(); |
| 85 iter != changed_extensions.end(); | 105 iter != changed_extensions.end(); |
| 86 ++iter) { | 106 ++iter) { |
| 87 if (!crx_file::id_util::IdIsValid(*iter)) { | 107 if (!crx_file::id_util::IdIsValid(*iter)) { |
| 88 NOTREACHED() << "Invalid extension id: " << *iter; | 108 NOTREACHED() << "Invalid extension id: " << *iter; |
| 89 return; | 109 return; |
| 90 } | 110 } |
| 91 } | 111 } |
| 92 | 112 |
| 93 UserScriptSet* scripts = NULL; | 113 UserScriptSet* scripts = NULL; |
| 94 if (!extension_id.empty()) { | 114 if (!extension_id.empty()) { |
| 95 // The expectation when there is an extensions that "owns" this shared | 115 // The expectation when there is an extension that "owns" this shared |
| 96 // memory region is that it will list itself as the only changed extension. | 116 // memory region is that the |changed_extensions| is either the empty list |
| 97 CHECK(changed_extensions.size() == 1 && | 117 // or just the owner. |
| 98 changed_extensions.find(extension_id) != changed_extensions.end()); | 118 CHECK(changed_extensions.size() <= 1); |
| 99 if (programmatic_scripts_.find(extension_id) == | 119 if (programmatic_scripts_.find(extension_id) == |
| 100 programmatic_scripts_.end()) { | 120 programmatic_scripts_.end()) { |
| 101 scripts = new UserScriptSet(extensions_); | 121 scripts = new UserScriptSet(extensions_); |
| 102 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); | 122 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); |
| 103 } else { | 123 } else { |
| 104 scripts = programmatic_scripts_[extension_id].get(); | 124 scripts = programmatic_scripts_[extension_id].get(); |
| 105 } | 125 } |
| 106 } else { | 126 } else { |
| 107 scripts = &static_scripts_; | 127 scripts = &static_scripts_; |
| 108 } | 128 } |
| 109 DCHECK(scripts); | 129 DCHECK(scripts); |
| 110 | 130 |
| 111 // If no extensions are included in the set, that indicates that all | 131 // If no extensions are included in the set, that indicates that all |
| 112 // extensions were updated. Add them all to the set so that observers and | 132 // extensions were updated. Add them all to the set so that observers and |
| 113 // individual UserScriptSets don't need to know this detail. | 133 // individual UserScriptSets don't need to know this detail. |
| 114 const std::set<std::string>* effective_extensions = &changed_extensions; | 134 const std::set<std::string>* effective_extensions = &changed_extensions; |
| 115 std::set<std::string> all_extensions; | 135 std::set<std::string> all_extensions; |
| 116 if (changed_extensions.empty()) { | 136 if (changed_extensions.empty()) { |
| 117 all_extensions = extensions_->GetIDs(); | 137 // The meaning of "all extensions" varies, depending on whether some |
| 138 // extension "owns" this shared memory region. |
| 139 // No owner => all known extensions. |
| 140 // Owner => just the owner extension. |
| 141 if (extension_id.empty()) |
| 142 all_extensions = extensions_->GetIDs(); |
| 143 else |
| 144 all_extensions.insert(extension_id); |
| 118 effective_extensions = &all_extensions; | 145 effective_extensions = &all_extensions; |
| 119 } | 146 } |
| 120 | 147 |
| 121 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { | 148 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { |
| 122 FOR_EACH_OBSERVER( | 149 FOR_EACH_OBSERVER( |
| 123 Observer, | 150 Observer, |
| 124 observers_, | 151 observers_, |
| 125 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); | 152 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); |
| 126 } | 153 } |
| 127 } | 154 } |
| 128 | 155 |
| 129 } // namespace extensions | 156 } // namespace extensions |
| OLD | NEW |