1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
#define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
#include "extensions/browser/api/execute_code_function.h"
#include "extensions/browser/component_extension_resource_manager.h"
#include "extensions/browser/extension_api_frame_id_map.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/file_reader.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/message_bundle.h"
#include "net/base/filename_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace {
// Error messages
const char kNoCodeOrFileToExecuteError[] = "No source code or file specified.";
const char kMoreThanOneValuesError[] =
"Code and file should not be specified "
"at the same time in the second argument.";
const char kBadFileEncodingError[] =
"Could not load file '*' for content script. It isn't UTF-8 encoded.";
const char kLoadFileError[] = "Failed to load file: \"*\". ";
}
namespace extensions {
using api::extension_types::InjectDetails;
ExecuteCodeFunction::ExecuteCodeFunction() {
}
ExecuteCodeFunction::~ExecuteCodeFunction() {
}
void ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) {
if (!success || !details_->file) {
DidLoadAndLocalizeFile(
resource_.relative_path().AsUTF8Unsafe(), success, data);
return;
}
ScriptExecutor::ScriptType script_type =
ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
std::string extension_id;
base::FilePath extension_path;
std::string extension_default_locale;
if (extension()) {
extension_id = extension()->id();
extension_path = extension()->path();
extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
&extension_default_locale);
}
content::BrowserThread::PostTask(
content::BrowserThread::FILE,
FROM_HERE,
base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS,
this,
script_type,
data,
extension_id,
extension_path,
extension_default_locale));
}
void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
ScriptExecutor::ScriptType script_type,
const std::string& data,
const std::string& extension_id,
const base::FilePath& extension_path,
const std::string& extension_default_locale) {
std::string localized_data = data;
// Check if the file is CSS and needs localization.
if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() &&
(data.find(MessageBundle::kMessageBegin) != std::string::npos)) {
scoped_ptr<SubstitutionMap> localization_messages(
file_util::LoadMessageBundleSubstitutionMap(
extension_path, extension_id, extension_default_locale));
// We need to do message replacement on the data, so it has to be mutable.
std::string error;
MessageBundle::ReplaceMessagesWithExternalDictionary(
*localization_messages, &localized_data, &error);
}
file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
// Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
// is always true, because if loading had failed, we wouldn't have had
// anything to localize.
content::BrowserThread::PostTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile,
this,
resource_.relative_path().AsUTF8Unsafe(),
true,
localized_data));
}
void ExecuteCodeFunction::DidLoadAndLocalizeFile(const std::string& file,
bool success,
const std::string& data) {
if (success) {
if (!base::IsStringUTF8(data)) {
error_ = ErrorUtils::FormatErrorMessage(kBadFileEncodingError, file);
SendResponse(false);
} else if (!Execute(data))
SendResponse(false);
} else {
// TODO(viettrungluu): bug: there's no particular reason the path should be
// UTF-8, in which case this may fail.
error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
SendResponse(false);
}
}
bool ExecuteCodeFunction::Execute(const std::string& code_string) {
ScriptExecutor* executor = GetScriptExecutor();
if (!executor)
return false;
if (!extension() && !IsWebView())
return false;
ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
if (ShouldInsertCSS())
script_type = ScriptExecutor::CSS;
ScriptExecutor::FrameScope frame_scope =
details_->all_frames.get() && *details_->all_frames
? ScriptExecutor::INCLUDE_SUB_FRAMES
: ScriptExecutor::SINGLE_FRAME;
int frame_id = details_->frame_id.get() ? *details_->frame_id
: ExtensionApiFrameIdMap::kTopFrameId;
ScriptExecutor::MatchAboutBlank match_about_blank =
details_->match_about_blank.get() && *details_->match_about_blank
? ScriptExecutor::MATCH_ABOUT_BLANK
: ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
UserScript::RunLocation run_at = UserScript::UNDEFINED;
switch (details_->run_at) {
case api::extension_types::RUN_AT_NONE:
case api::extension_types::RUN_AT_DOCUMENT_IDLE:
run_at = UserScript::DOCUMENT_IDLE;
break;
case api::extension_types::RUN_AT_DOCUMENT_START:
run_at = UserScript::DOCUMENT_START;
break;
case api::extension_types::RUN_AT_DOCUMENT_END:
run_at = UserScript::DOCUMENT_END;
break;
}
CHECK_NE(UserScript::UNDEFINED, run_at);
executor->ExecuteScript(
host_id_, script_type, code_string, frame_scope, frame_id,
match_about_blank, run_at, ScriptExecutor::ISOLATED_WORLD,
IsWebView() ? ScriptExecutor::WEB_VIEW_PROCESS
: ScriptExecutor::DEFAULT_PROCESS,
GetWebViewSrc(), file_url_, user_gesture_,
has_callback() ? ScriptExecutor::JSON_SERIALIZED_RESULT
: ScriptExecutor::NO_RESULT,
base::Bind(&ExecuteCodeFunction::OnExecuteCodeFinished, this));
return true;
}
bool ExecuteCodeFunction::HasPermission() {
return true;
}
bool ExecuteCodeFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(Init());
if (!details_->code.get() && !details_->file.get()) {
error_ = kNoCodeOrFileToExecuteError;
return false;
}
if (details_->code.get() && details_->file.get()) {
error_ = kMoreThanOneValuesError;
return false;
}
if (!CanExecuteScriptOnPage())
return false;
if (details_->code.get())
return Execute(*details_->code);
if (!details_->file.get())
return false;
return LoadFile(*details_->file);
}
bool ExecuteCodeFunction::LoadFile(const std::string& file) {
resource_ = extension()->GetResource(file);
if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
error_ = kNoCodeOrFileToExecuteError;
return false;
}
int resource_id;
const ComponentExtensionResourceManager*
component_extension_resource_manager =
ExtensionsBrowserClient::Get()
->GetComponentExtensionResourceManager();
if (component_extension_resource_manager &&
component_extension_resource_manager->IsComponentExtensionResource(
resource_.extension_root(),
resource_.relative_path(),
&resource_id)) {
const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string());
} else {
scoped_refptr<FileReader> file_reader(new FileReader(
resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
file_reader->Start();
}
return true;
}
void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error,
const GURL& on_url,
const base::ListValue& result) {
if (!error.empty())
SetError(error);
SendResponse(error.empty());
}
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
|