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