Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: extensions/renderer/user_script_set_manager.cc

Issue 492133002: Renderer changes for wiring up shared memory with declarative injection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add browsertests Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
96 // memory region is that it will list itself as the only changed extension.
97 CHECK(changed_extensions.size() == 1 &&
Devlin 2014/08/25 22:31:06 We can still have a check that the size is <= 1, r
Mark Dittmer 2014/08/26 17:53:58 Sure.
98 changed_extensions.find(extension_id) != changed_extensions.end());
99 if (programmatic_scripts_.find(extension_id) == 115 if (programmatic_scripts_.find(extension_id) ==
100 programmatic_scripts_.end()) { 116 programmatic_scripts_.end()) {
101 scripts = new UserScriptSet(extensions_); 117 scripts = new UserScriptSet(extensions_);
102 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); 118 programmatic_scripts_[extension_id] = make_linked_ptr(scripts);
103 } else { 119 } else {
104 scripts = programmatic_scripts_[extension_id].get(); 120 scripts = programmatic_scripts_[extension_id].get();
105 } 121 }
106 } else { 122 } else {
107 scripts = &static_scripts_; 123 scripts = &static_scripts_;
108 } 124 }
109 DCHECK(scripts); 125 DCHECK(scripts);
110 126
111 // If no extensions are included in the set, that indicates that all 127 // 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 128 // extensions were updated. Add them all to the set so that observers and
113 // individual UserScriptSets don't need to know this detail. 129 // individual UserScriptSets don't need to know this detail.
114 const std::set<std::string>* effective_extensions = &changed_extensions; 130 const std::set<std::string>* effective_extensions = &changed_extensions;
115 std::set<std::string> all_extensions; 131 std::set<std::string> all_extensions;
116 if (changed_extensions.empty()) { 132 if (changed_extensions.empty()) {
117 all_extensions = extensions_->GetIDs(); 133 // The meaning of "all extensions" varies, depending on whether some
134 // extension "owns" this shared memory region.
135 // No owner => all known extensions.
136 // Owner => just the owner extension.
137 if (extension_id.empty())
138 all_extensions = extensions_->GetIDs();
139 else
140 all_extensions.insert(extension_id);
118 effective_extensions = &all_extensions; 141 effective_extensions = &all_extensions;
119 } 142 }
120 143
121 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { 144 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) {
122 FOR_EACH_OBSERVER( 145 FOR_EACH_OBSERVER(
123 Observer, 146 Observer,
124 observers_, 147 observers_,
125 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); 148 OnUserScriptsUpdated(*effective_extensions, scripts->scripts()));
126 } 149 }
127 } 150 }
128 151
129 } // namespace extensions 152 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698