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