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

Side by Side Diff: chrome/browser/extensions/api/execute_code_function.cc

Issue 538063003: Move execute_code_function to extensions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: gn build 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
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/execute_code_function.h"
6
7 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
8 #include "chrome/common/extensions/api/i18n/default_locale_handler.h"
9 #include "extensions/browser/component_extension_resource_manager.h"
10 #include "extensions/browser/extensions_browser_client.h"
11 #include "extensions/browser/file_reader.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/file_util.h"
15 #include "extensions/common/message_bundle.h"
16 #include "net/base/filename_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18
19 namespace extensions {
20
21 namespace keys = tabs_constants;
22 using api::tabs::InjectDetails;
23
24 ExecuteCodeFunction::ExecuteCodeFunction() {
25 }
26
27 ExecuteCodeFunction::~ExecuteCodeFunction() {
28 }
29
30 void ExecuteCodeFunction::DidLoadFile(bool success,
31 const std::string& data) {
32
33 if (!success || !details_->file) {
34 DidLoadAndLocalizeFile(success, data);
35 return;
36 }
37
38 ScriptExecutor::ScriptType script_type =
39 ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
40
41 std::string extension_id;
42 base::FilePath extension_path;
43 std::string extension_default_locale;
44 if (extension()) {
45 extension_id = extension()->id();
46 extension_path = extension()->path();
47 extension_default_locale = LocaleInfo::GetDefaultLocale(extension());
48 }
49
50 content::BrowserThread::PostTask(
51 content::BrowserThread::FILE, FROM_HERE,
52 base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this,
53 script_type,
54 data,
55 extension_id,
56 extension_path,
57 extension_default_locale));
58 }
59
60 void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
61 ScriptExecutor::ScriptType script_type,
62 const std::string& data,
63 const std::string& extension_id,
64 const base::FilePath& extension_path,
65 const std::string& extension_default_locale) {
66
67 std::string localized_data = data;
68 // Check if the file is CSS and needs localization.
69 if ((script_type == ScriptExecutor::CSS) &&
70 !extension_id.empty() &&
71 (data.find(MessageBundle::kMessageBegin) != std::string::npos)) {
72 scoped_ptr<SubstitutionMap> localization_messages(
73 file_util::LoadMessageBundleSubstitutionMap(
74 extension_path, extension_id, extension_default_locale));
75
76 // We need to do message replacement on the data, so it has to be mutable.
77 std::string error;
78 MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
79 &localized_data,
80 &error);
81 }
82
83 file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
84
85 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
86 // is always true, because if loading had failed, we wouldn't have had
87 // anything to localize.
88 content::BrowserThread::PostTask(
89 content::BrowserThread::UI, FROM_HERE,
90 base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
91 true, localized_data));
92 }
93
94 void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success,
95 const std::string& data) {
96 if (success) {
97 if (!Execute(data))
98 SendResponse(false);
99 } else {
100 // TODO(viettrungluu): bug: there's no particular reason the path should be
101 // UTF-8, in which case this may fail.
102 error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError,
103 resource_.relative_path().AsUTF8Unsafe());
104 SendResponse(false);
105 }
106 }
107
108 bool ExecuteCodeFunction::Execute(const std::string& code_string) {
109 ScriptExecutor* executor = GetScriptExecutor();
110 if (!executor)
111 return false;
112
113 if (!extension())
114 return false;
115
116 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
117 if (ShouldInsertCSS())
118 script_type = ScriptExecutor::CSS;
119
120 ScriptExecutor::FrameScope frame_scope =
121 details_->all_frames.get() && *details_->all_frames ?
122 ScriptExecutor::ALL_FRAMES :
123 ScriptExecutor::TOP_FRAME;
124
125 ScriptExecutor::MatchAboutBlank match_about_blank =
126 details_->match_about_blank.get() && *details_->match_about_blank ?
127 ScriptExecutor::MATCH_ABOUT_BLANK :
128 ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
129
130 UserScript::RunLocation run_at =
131 UserScript::UNDEFINED;
132 switch (details_->run_at) {
133 case InjectDetails::RUN_AT_NONE:
134 case InjectDetails::RUN_AT_DOCUMENT_IDLE:
135 run_at = UserScript::DOCUMENT_IDLE;
136 break;
137 case InjectDetails::RUN_AT_DOCUMENT_START:
138 run_at = UserScript::DOCUMENT_START;
139 break;
140 case InjectDetails::RUN_AT_DOCUMENT_END:
141 run_at = UserScript::DOCUMENT_END;
142 break;
143 }
144 CHECK_NE(UserScript::UNDEFINED, run_at);
145
146 executor->ExecuteScript(
147 extension()->id(),
148 script_type,
149 code_string,
150 frame_scope,
151 match_about_blank,
152 run_at,
153 ScriptExecutor::ISOLATED_WORLD,
154 IsWebView() ? ScriptExecutor::WEB_VIEW_PROCESS
155 : ScriptExecutor::DEFAULT_PROCESS,
156 GetWebViewSrc(),
157 file_url_,
158 user_gesture_,
159 has_callback() ? ScriptExecutor::JSON_SERIALIZED_RESULT
160 : ScriptExecutor::NO_RESULT,
161 base::Bind(&ExecuteCodeFunction::OnExecuteCodeFinished, this));
162 return true;
163 }
164
165 bool ExecuteCodeFunction::HasPermission() {
166 return true;
167 }
168
169 bool ExecuteCodeFunction::RunAsync() {
170 EXTENSION_FUNCTION_VALIDATE(Init());
171
172 if (!details_->code.get() && !details_->file.get()) {
173 error_ = keys::kNoCodeOrFileToExecuteError;
174 return false;
175 }
176 if (details_->code.get() && details_->file.get()) {
177 error_ = keys::kMoreThanOneValuesError;
178 return false;
179 }
180
181 if (!CanExecuteScriptOnPage())
182 return false;
183
184 if (details_->code.get())
185 return Execute(*details_->code);
186
187 if (!details_->file.get())
188 return false;
189 resource_ = extension()->GetResource(*details_->file);
190
191 if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
192 error_ = keys::kNoCodeOrFileToExecuteError;
193 return false;
194 }
195
196 int resource_id;
197 if (ExtensionsBrowserClient::Get()->GetComponentExtensionResourceManager()->
198 IsComponentExtensionResource(
199 resource_.extension_root(), resource_.relative_path(),
200 &resource_id)) {
201 const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
202 DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string());
203 } else {
204 scoped_refptr<FileReader> file_reader(new FileReader(
205 resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
206 file_reader->Start();
207 }
208
209 return true;
210 }
211
212 void ExecuteCodeFunction::OnExecuteCodeFinished(
213 const std::string& error,
214 const GURL& on_url,
215 const base::ListValue& result) {
216 if (!error.empty())
217 SetError(error);
218
219 SendResponse(error.empty());
220 }
221
222 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/execute_code_function.h ('k') | chrome/browser/extensions/api/tabs/tabs_api.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698