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

Side by Side Diff: extensions/browser/script_executor.cc

Issue 942533003: Enable <webview>.executeScript outside of Apps and Extensions [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@decouple_brower_isolated_world_routingid_user_script_UserScriptSet_non_hostset_2
Patch Set: Add a test. Created 5 years, 9 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/browser/script_executor.h" 5 #include "extensions/browser/script_executor.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/pickle.h" 10 #include "base/pickle.h"
(...skipping 20 matching lines...) Expand all
31 // injection request to the renderer, and it will be destroyed after either the 31 // injection request to the renderer, and it will be destroyed after either the
32 // corresponding response comes from the renderer, or the renderer is destroyed. 32 // corresponding response comes from the renderer, or the renderer is destroyed.
33 class Handler : public content::WebContentsObserver { 33 class Handler : public content::WebContentsObserver {
34 public: 34 public:
35 Handler(ObserverList<ScriptExecutionObserver>* script_observers, 35 Handler(ObserverList<ScriptExecutionObserver>* script_observers,
36 content::WebContents* web_contents, 36 content::WebContents* web_contents,
37 const ExtensionMsg_ExecuteCode_Params& params, 37 const ExtensionMsg_ExecuteCode_Params& params,
38 const ScriptExecutor::ExecuteScriptCallback& callback) 38 const ScriptExecutor::ExecuteScriptCallback& callback)
39 : content::WebContentsObserver(web_contents), 39 : content::WebContentsObserver(web_contents),
40 script_observers_(AsWeakPtr(script_observers)), 40 script_observers_(AsWeakPtr(script_observers)),
41 extension_id_(params.extension_id), 41 host_id_(params.host_id),
42 request_id_(params.request_id), 42 request_id_(params.request_id),
43 callback_(callback) { 43 callback_(callback) {
44 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); 44 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
45 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params)); 45 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params));
46 } 46 }
47 47
48 ~Handler() override {} 48 ~Handler() override {}
49 49
50 bool OnMessageReceived(const IPC::Message& message) override { 50 bool OnMessageReceived(const IPC::Message& message) override {
51 // Unpack by hand to check the request_id, since there may be multiple 51 // Unpack by hand to check the request_id, since there may be multiple
(...skipping 19 matching lines...) Expand all
71 base::ListValue val; 71 base::ListValue val;
72 callback_.Run(kRendererDestroyed, GURL(std::string()), val); 72 callback_.Run(kRendererDestroyed, GURL(std::string()), val);
73 delete this; 73 delete this;
74 } 74 }
75 75
76 private: 76 private:
77 void OnExecuteCodeFinished(int request_id, 77 void OnExecuteCodeFinished(int request_id,
78 const std::string& error, 78 const std::string& error,
79 const GURL& on_url, 79 const GURL& on_url,
80 const base::ListValue& script_result) { 80 const base::ListValue& script_result) {
81 if (script_observers_.get() && error.empty()) { 81 if (script_observers_.get() && error.empty() &&
82 host_id_.type() == HostID::EXTENSIONS) {
82 ScriptExecutionObserver::ExecutingScriptsMap id_map; 83 ScriptExecutionObserver::ExecutingScriptsMap id_map;
83 id_map[extension_id_] = std::set<std::string>(); 84 id_map[host_id_.id()] = std::set<std::string>();
84 FOR_EACH_OBSERVER(ScriptExecutionObserver, 85 FOR_EACH_OBSERVER(ScriptExecutionObserver,
85 *script_observers_, 86 *script_observers_,
86 OnScriptsExecuted(web_contents(), id_map, on_url)); 87 OnScriptsExecuted(web_contents(), id_map, on_url));
87 } 88 }
88 89
89 callback_.Run(error, on_url, script_result); 90 callback_.Run(error, on_url, script_result);
90 delete this; 91 delete this;
91 } 92 }
92 93
93 base::WeakPtr<ObserverList<ScriptExecutionObserver> > script_observers_; 94 base::WeakPtr<ObserverList<ScriptExecutionObserver> > script_observers_;
94 std::string extension_id_; 95 HostID host_id_;
95 int request_id_; 96 int request_id_;
96 ScriptExecutor::ExecuteScriptCallback callback_; 97 ScriptExecutor::ExecuteScriptCallback callback_;
97 }; 98 };
98 99
99 } // namespace 100 } // namespace
100 101
101 ScriptExecutionObserver::~ScriptExecutionObserver() { 102 ScriptExecutionObserver::~ScriptExecutionObserver() {
102 } 103 }
103 104
104 ScriptExecutor::ScriptExecutor( 105 ScriptExecutor::ScriptExecutor(
105 content::WebContents* web_contents, 106 content::WebContents* web_contents,
106 ObserverList<ScriptExecutionObserver>* script_observers) 107 ObserverList<ScriptExecutionObserver>* script_observers)
107 : next_request_id_(0), 108 : next_request_id_(0),
108 web_contents_(web_contents), 109 web_contents_(web_contents),
109 script_observers_(script_observers) { 110 script_observers_(script_observers) {
110 CHECK(web_contents_); 111 CHECK(web_contents_);
111 } 112 }
112 113
113 ScriptExecutor::~ScriptExecutor() { 114 ScriptExecutor::~ScriptExecutor() {
114 } 115 }
115 116
116 void ScriptExecutor::ExecuteScript(const std::string& extension_id, 117 void ScriptExecutor::ExecuteScript(const HostID& host_id,
117 ScriptExecutor::ScriptType script_type, 118 ScriptExecutor::ScriptType script_type,
118 const std::string& code, 119 const std::string& code,
119 ScriptExecutor::FrameScope frame_scope, 120 ScriptExecutor::FrameScope frame_scope,
120 ScriptExecutor::MatchAboutBlank about_blank, 121 ScriptExecutor::MatchAboutBlank about_blank,
121 UserScript::RunLocation run_at, 122 UserScript::RunLocation run_at,
122 ScriptExecutor::WorldType world_type, 123 ScriptExecutor::WorldType world_type,
123 ScriptExecutor::ProcessType process_type, 124 ScriptExecutor::ProcessType process_type,
124 const GURL& webview_src, 125 const GURL& webview_src,
125 const GURL& file_url, 126 const GURL& file_url,
126 bool user_gesture, 127 bool user_gesture,
127 ScriptExecutor::ResultType result_type, 128 ScriptExecutor::ResultType result_type,
128 const ExecuteScriptCallback& callback) { 129 const ExecuteScriptCallback& callback) {
129 // Don't execute if the extension has been unloaded. 130 if (host_id.type() == HostID::EXTENSIONS) {
130 const Extension* extension = 131 // Don't execute if the extension has been unloaded.
131 ExtensionRegistry::Get(web_contents_->GetBrowserContext()) 132 const Extension* extension =
132 ->enabled_extensions().GetByID(extension_id); 133 ExtensionRegistry::Get(web_contents_->GetBrowserContext())
133 if (!extension) 134 ->enabled_extensions().GetByID(host_id.id());
134 return; 135 if (!extension)
136 return;
137 } else {
138 CHECK(process_type == WEB_VIEW_PROCESS);
139 }
135 140
136 ExtensionMsg_ExecuteCode_Params params; 141 ExtensionMsg_ExecuteCode_Params params;
137 params.request_id = next_request_id_++; 142 params.request_id = next_request_id_++;
138 params.extension_id = extension_id; 143 params.host_id = host_id;
139 params.is_javascript = (script_type == JAVASCRIPT); 144 params.is_javascript = (script_type == JAVASCRIPT);
140 params.code = code; 145 params.code = code;
141 params.all_frames = (frame_scope == ALL_FRAMES); 146 params.all_frames = (frame_scope == ALL_FRAMES);
142 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK); 147 params.match_about_blank = (about_blank == MATCH_ABOUT_BLANK);
143 params.run_at = static_cast<int>(run_at); 148 params.run_at = static_cast<int>(run_at);
144 params.in_main_world = (world_type == MAIN_WORLD); 149 params.in_main_world = (world_type == MAIN_WORLD);
145 params.is_web_view = (process_type == WEB_VIEW_PROCESS); 150 params.is_web_view = (process_type == WEB_VIEW_PROCESS);
146 params.webview_src = webview_src; 151 params.webview_src = webview_src;
147 params.file_url = file_url; 152 params.file_url = file_url;
148 params.wants_result = (result_type == JSON_SERIALIZED_RESULT); 153 params.wants_result = (result_type == JSON_SERIALIZED_RESULT);
149 params.user_gesture = user_gesture; 154 params.user_gesture = user_gesture;
150 155
151 // Handler handles IPCs and deletes itself on completion. 156 // Handler handles IPCs and deletes itself on completion.
152 new Handler(script_observers_, web_contents_, params, callback); 157 new Handler(script_observers_, web_contents_, params, callback);
153 } 158 }
154 159
155 } // namespace extensions 160 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698