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

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

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: uplaod with base Created 4 years, 4 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.h" 5 #include "extensions/renderer/user_script_set.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 void UserScriptSet::AddObserver(Observer* observer) { 51 void UserScriptSet::AddObserver(Observer* observer) {
52 observers_.AddObserver(observer); 52 observers_.AddObserver(observer);
53 } 53 }
54 54
55 void UserScriptSet::RemoveObserver(Observer* observer) { 55 void UserScriptSet::RemoveObserver(Observer* observer) {
56 observers_.RemoveObserver(observer); 56 observers_.RemoveObserver(observer);
57 } 57 }
58 58
59 void UserScriptSet::GetActiveExtensionIds( 59 void UserScriptSet::GetActiveExtensionIds(
60 std::set<std::string>* ids) const { 60 std::set<std::string>* ids) const {
61 for (const std::unique_ptr<UserScript>& script : scripts_) { 61 for (const std::unique_ptr<RendererUserScript>& script : scripts_) {
62 if (script->host_id().type() != HostID::EXTENSIONS) 62 if (script->host_id().type() != HostID::EXTENSIONS)
63 continue; 63 continue;
64 DCHECK(!script->extension_id().empty()); 64 DCHECK(!script->extension_id().empty());
65 ids->insert(script->extension_id()); 65 ids->insert(script->extension_id());
66 } 66 }
67 } 67 }
68 68
69 void UserScriptSet::GetInjections( 69 void UserScriptSet::GetInjections(
70 std::vector<std::unique_ptr<ScriptInjection>>* injections, 70 std::vector<std::unique_ptr<ScriptInjection>>* injections,
71 content::RenderFrame* render_frame, 71 content::RenderFrame* render_frame,
72 int tab_id, 72 int tab_id,
73 UserScript::RunLocation run_location, 73 UserScript::RunLocation run_location,
74 bool log_activity) { 74 bool log_activity) {
75 GURL document_url = GetDocumentUrlForFrame(render_frame->GetWebFrame()); 75 GURL document_url = GetDocumentUrlForFrame(render_frame->GetWebFrame());
76 for (const std::unique_ptr<UserScript>& script : scripts_) { 76 for (const std::unique_ptr<RendererUserScript>& script : scripts_) {
77 std::unique_ptr<ScriptInjection> injection = GetInjectionForScript( 77 std::unique_ptr<ScriptInjection> injection = GetInjectionForScript(
78 script.get(), render_frame, tab_id, run_location, document_url, 78 script.get(), render_frame, tab_id, run_location, document_url,
79 false /* is_declarative */, log_activity); 79 false /* is_declarative */, log_activity);
80 if (injection.get()) 80 if (injection.get())
81 injections->push_back(std::move(injection)); 81 injections->push_back(std::move(injection));
82 } 82 }
83 } 83 }
84 84
85 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory, 85 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory,
86 const std::set<HostID>& changed_hosts, 86 const std::set<HostID>& changed_hosts,
(...skipping 21 matching lines...) Expand all
108 // Unpickle scripts. 108 // Unpickle scripts.
109 uint32_t num_scripts = 0; 109 uint32_t num_scripts = 0;
110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), 110 base::Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()),
111 pickle_size); 111 pickle_size);
112 base::PickleIterator iter(pickle); 112 base::PickleIterator iter(pickle);
113 CHECK(iter.ReadUInt32(&num_scripts)); 113 CHECK(iter.ReadUInt32(&num_scripts));
114 114
115 scripts_.clear(); 115 scripts_.clear();
116 scripts_.reserve(num_scripts); 116 scripts_.reserve(num_scripts);
117 for (uint32_t i = 0; i < num_scripts; ++i) { 117 for (uint32_t i = 0; i < num_scripts; ++i) {
118 std::unique_ptr<UserScript> script(new UserScript()); 118 std::unique_ptr<RendererUserScript> script(new RendererUserScript());
119 script->Unpickle(pickle, &iter); 119 script->Unpickle(pickle, &iter);
120 120
121 // Note that this is a pointer into shared memory. We don't own it. It gets 121 // Note that this is a pointer into shared memory. We don't own it. It gets
122 // cleared up when the last renderer or browser process drops their 122 // cleared up when the last renderer or browser process drops their
123 // reference to the shared memory. 123 // reference to the shared memory.
124 for (size_t j = 0; j < script->js_scripts().size(); ++j) { 124 for (size_t j = 0; j < script->js_scripts().size(); ++j) {
125 const char* body = NULL; 125 const char* body = NULL;
126 int body_length = 0; 126 int body_length = 0;
127 CHECK(iter.ReadData(&body, &body_length)); 127 CHECK(iter.ReadData(&body, &body_length));
128 script->js_scripts()[j].set_external_content( 128 script->js_scripts()[j]->set_external_content(
129 base::StringPiece(body, body_length)); 129 base::StringPiece(body, body_length));
130 } 130 }
131 for (size_t j = 0; j < script->css_scripts().size(); ++j) { 131 for (size_t j = 0; j < script->css_scripts().size(); ++j) {
132 const char* body = NULL; 132 const char* body = NULL;
133 int body_length = 0; 133 int body_length = 0;
134 CHECK(iter.ReadData(&body, &body_length)); 134 CHECK(iter.ReadData(&body, &body_length));
135 script->css_scripts()[j].set_external_content( 135 script->css_scripts()[j]->set_external_content(
136 base::StringPiece(body, body_length)); 136 base::StringPiece(body, body_length));
137 } 137 }
138 138
139 if (only_inject_incognito && !script->is_incognito_enabled()) 139 if (only_inject_incognito && !script->is_incognito_enabled())
140 continue; // This script shouldn't run in an incognito tab. 140 continue; // This script shouldn't run in an incognito tab.
141 141
142 const Extension* extension = 142 const Extension* extension =
143 RendererExtensionRegistry::Get()->GetByID(script->extension_id()); 143 RendererExtensionRegistry::Get()->GetByID(script->extension_id());
144 if (whitelisted_only && 144 if (whitelisted_only &&
145 (!extension || 145 (!extension ||
146 !PermissionsData::CanExecuteScriptEverywhere(extension))) { 146 !PermissionsData::CanExecuteScriptEverywhere(extension))) {
147 continue; 147 continue;
148 } 148 }
149 149
150 scripts_.push_back(std::move(script)); 150 scripts_.push_back(std::move(script));
151 } 151 }
152 152
153 FOR_EACH_OBSERVER(Observer, observers_, 153 FOR_EACH_OBSERVER(Observer, observers_,
154 OnUserScriptsUpdated(changed_hosts, scripts_)); 154 OnUserScriptsUpdated(changed_hosts, scripts_));
155 return true; 155 return true;
156 } 156 }
157 157
158 std::unique_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( 158 std::unique_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection(
159 int script_id, 159 int script_id,
160 content::RenderFrame* render_frame, 160 content::RenderFrame* render_frame,
161 int tab_id, 161 int tab_id,
162 UserScript::RunLocation run_location, 162 UserScript::RunLocation run_location,
163 const GURL& document_url, 163 const GURL& document_url,
164 bool log_activity) { 164 bool log_activity) {
165 for (const std::unique_ptr<UserScript>& script : scripts_) { 165 for (const std::unique_ptr<RendererUserScript>& script : scripts_) {
166 if (script->id() == script_id) { 166 if (script->id() == script_id) {
167 return GetInjectionForScript(script.get(), render_frame, tab_id, 167 return GetInjectionForScript(script.get(), render_frame, tab_id,
168 run_location, document_url, 168 run_location, document_url,
169 true /* is_declarative */, log_activity); 169 true /* is_declarative */, log_activity);
170 } 170 }
171 } 171 }
172 return std::unique_ptr<ScriptInjection>(); 172 return std::unique_ptr<ScriptInjection>();
173 } 173 }
174 174
175 std::unique_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( 175 std::unique_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript(
176 const UserScript* script, 176 const RendererUserScript* script,
177 content::RenderFrame* render_frame, 177 content::RenderFrame* render_frame,
178 int tab_id, 178 int tab_id,
179 UserScript::RunLocation run_location, 179 UserScript::RunLocation run_location,
180 const GURL& document_url, 180 const GURL& document_url,
181 bool is_declarative, 181 bool is_declarative,
182 bool log_activity) { 182 bool log_activity) {
183 std::unique_ptr<ScriptInjection> injection; 183 std::unique_ptr<ScriptInjection> injection;
184 std::unique_ptr<const InjectionHost> injection_host; 184 std::unique_ptr<const InjectionHost> injection_host;
185 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame(); 185 blink::WebLocalFrame* web_frame = render_frame->GetWebFrame();
186 186
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 !script->js_scripts().empty() && script->run_location() == run_location; 220 !script->js_scripts().empty() && script->run_location() == run_location;
221 if (inject_css || inject_js) { 221 if (inject_css || inject_js) {
222 injection.reset(new ScriptInjection(std::move(injector), render_frame, 222 injection.reset(new ScriptInjection(std::move(injector), render_frame,
223 std::move(injection_host), run_location, 223 std::move(injection_host), run_location,
224 log_activity)); 224 log_activity));
225 } 225 }
226 return injection; 226 return injection;
227 } 227 }
228 228
229 } // namespace extensions 229 } // namespace extensions
OLDNEW
« extensions/common/user_script.h ('K') | « extensions/renderer/user_script_set.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698