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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
// Copyright (c) 2009 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.
#include "chrome/browser/extensions/crx_installer.h"
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/file_util.h"
#include "base/scoped_temp_dir.h"
#include "base/string_util.h"
#include "base/task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/convert_user_script.h"
#include "chrome/browser/extensions/extension_file_util.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/common/extensions/extension_error_reporter.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "grit/browser_resources.h"
#include "grit/chromium_strings.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace {
// Helper function to delete files. This is used to avoid ugly casts which
// would be necessary with PostMessage since file_util::Delete is overloaded.
static void DeleteFileHelper(const FilePath& path, bool recursive) {
file_util::Delete(path, recursive);
}
}
void CrxInstaller::Start(const FilePath& crx_path,
const FilePath& install_directory,
Extension::Location install_source,
const std::string& expected_id,
bool delete_source,
bool allow_privilege_increase,
ExtensionsService* frontend,
ExtensionInstallUI* client) {
// Note: This object manages its own lifetime.
scoped_refptr<CrxInstaller> installer(
new CrxInstaller(crx_path, install_directory, delete_source, frontend,
client));
installer->install_source_ = install_source;
installer->expected_id_ = expected_id;
installer->allow_privilege_increase_ = allow_privilege_increase;
scoped_refptr<SandboxedExtensionUnpacker> unpacker(
new SandboxedExtensionUnpacker(
installer->source_file_,
g_browser_process->resource_dispatcher_host(),
installer.get()));
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(
unpacker.get(), &SandboxedExtensionUnpacker::Start));
}
void CrxInstaller::InstallUserScript(const FilePath& source_file,
const GURL& original_url,
const FilePath& install_directory,
bool delete_source,
ExtensionsService* frontend,
ExtensionInstallUI* client) {
scoped_refptr<CrxInstaller> installer(
new CrxInstaller(source_file, install_directory, delete_source, frontend,
client));
installer->original_url_ = original_url;
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(installer.get(),
&CrxInstaller::ConvertUserScriptOnFileThread));
}
CrxInstaller::CrxInstaller(const FilePath& source_file,
const FilePath& install_directory,
bool delete_source,
ExtensionsService* frontend,
ExtensionInstallUI* client)
: source_file_(source_file),
install_directory_(install_directory),
install_source_(Extension::INTERNAL),
delete_source_(delete_source),
allow_privilege_increase_(false),
create_app_shortcut_(false),
frontend_(frontend),
client_(client) {
extensions_enabled_ = frontend_->extensions_enabled();
}
CrxInstaller::~CrxInstaller() {
// Delete the temp directory and crx file as necessary. Note that the
// destructor might be called on any thread, so we post a task to the file
// thread to make sure the delete happens there.
if (!temp_dir_.value().empty()) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableFunction(&DeleteFileHelper, temp_dir_, true));
}
if (delete_source_) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableFunction(&DeleteFileHelper, source_file_, false));
}
}
void CrxInstaller::ConvertUserScriptOnFileThread() {
std::string error;
Extension* extension = ConvertUserScriptToExtension(source_file_,
original_url_, &error);
if (!extension) {
ReportFailureFromFileThread(error);
return;
}
OnUnpackSuccess(extension->path(), extension->path(), extension);
}
void CrxInstaller::OnUnpackFailure(const std::string& error_message) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
ReportFailureFromFileThread(error_message);
}
void CrxInstaller::OnUnpackSuccess(const FilePath& temp_dir,
const FilePath& extension_dir,
Extension* extension) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
// Note: We take ownership of |extension| and |temp_dir|.
extension_.reset(extension);
temp_dir_ = temp_dir;
// The unpack dir we don't have to delete explicity since it is a child of
// the temp dir.
unpacked_extension_root_ = extension_dir;
// Determine whether to allow installation. We always allow themes and
// external installs.
if (!extensions_enabled_ && !extension->IsTheme() &&
!Extension::IsExternalLocation(install_source_)) {
ReportFailureFromFileThread("Extensions are not enabled.");
return;
}
// Make sure the expected id matches.
// TODO(aa): Also support expected version?
if (!expected_id_.empty() && expected_id_ != extension->id()) {
ReportFailureFromFileThread(StringPrintf(
"ID in new extension manifest (%s) does not match expected id (%s)",
extension->id().c_str(),
expected_id_.c_str()));
return;
}
if (client_.get() || extension_->IsApp()) {
Extension::DecodeIcon(extension_.get(), Extension::EXTENSION_ICON_LARGE,
&install_icon_);
}
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::ConfirmInstall));
}
void CrxInstaller::ConfirmInstall() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
if (frontend_->extension_prefs()->IsExtensionBlacklisted(extension_->id())) {
LOG(INFO) << "This extension: " << extension_->id()
<< " is blacklisted. Install failed.";
ReportFailureFromUIThread("This extension is blacklisted.");
return;
}
current_version_ =
frontend_->extension_prefs()->GetVersionString(extension_->id());
if (client_.get()) {
AddRef(); // Balanced in Proceed() and Abort().
client_->ConfirmInstall(this, extension_.get(), install_icon_.get());
} else {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::CompleteInstall));
}
return;
}
void CrxInstaller::InstallUIProceed(bool create_app_shortcut) {
if (create_app_shortcut) {
DCHECK(extension_->IsApp());
create_app_shortcut_ = true;
}
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::CompleteInstall));
Release(); // balanced in ConfirmInstall().
}
void CrxInstaller::InstallUIAbort() {
// Kill the theme loading bubble.
NotificationService* service = NotificationService::current();
service->Notify(NotificationType::NO_THEME_DETECTED,
Source<CrxInstaller>(this),
NotificationService::NoDetails());
Release(); // balanced in ConfirmInstall().
// We're done. Since we don't post any more tasks to ourself, our ref count
// should go to zero and we die. The destructor will clean up the temp dir.
}
void CrxInstaller::CompleteInstall() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
FilePath version_dir;
Extension::InstallType install_type =
extension_file_util::CompareToInstalledVersion(
install_directory_, extension_->id(), current_version_,
extension_->VersionString(), &version_dir);
if (install_type == Extension::DOWNGRADE) {
ReportFailureFromFileThread("Attempted to downgrade extension.");
return;
}
if (install_type == Extension::REINSTALL) {
// We use this as a signal to switch themes.
ReportOverinstallFromFileThread();
return;
}
std::string error_msg;
if (!extension_file_util::InstallExtension(unpacked_extension_root_,
version_dir, &error_msg)) {
ReportFailureFromFileThread(error_msg);
return;
}
if (create_app_shortcut_) {
SkBitmap icon = install_icon_.get() ? *install_icon_ :
*ResourceBundle::GetSharedInstance().GetBitmapNamed(
IDR_EXTENSION_DEFAULT_ICON);
ShellIntegration::ShortcutInfo shortcut_info;
shortcut_info.url = extension_->app_launch_url();
shortcut_info.title = UTF8ToUTF16(extension_->name());
shortcut_info.description = UTF8ToUTF16(extension_->description());
shortcut_info.favicon = icon;
shortcut_info.create_on_desktop = true;
// TODO(aa): Seems nasty to be reusing the old webapps code this way. What
// baggage am I inheriting?
web_app::CreateShortcut(frontend_->profile()->GetPath(), shortcut_info,
NULL);
}
// This is lame, but we must reload the extension because absolute paths
// inside the content scripts are established inside InitFromValue() and we
// just moved the extension.
// TODO(aa): All paths to resources inside extensions should be created
// lazily and based on the Extension's root path at that moment.
std::string error;
extension_.reset(extension_file_util::LoadExtension(version_dir, true,
&error));
DCHECK(error.empty());
extension_->set_location(install_source_);
ReportSuccessFromFileThread();
}
void CrxInstaller::ReportFailureFromFileThread(const std::string& error) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::ReportFailureFromUIThread, error));
}
void CrxInstaller::ReportFailureFromUIThread(const std::string& error) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
NotificationService* service = NotificationService::current();
service->Notify(NotificationType::EXTENSION_INSTALL_ERROR,
Source<CrxInstaller>(this),
Details<const std::string>(&error));
// This isn't really necessary, it is only used because unit tests expect to
// see errors get reported via this interface.
//
// TODO(aa): Need to go through unit tests and clean them up too, probably get
// rid of this line.
ExtensionErrorReporter::GetInstance()->ReportError(error, false); // quiet
if (client_.get())
client_->OnInstallFailure(error);
}
void CrxInstaller::ReportOverinstallFromFileThread() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::ReportOverinstallFromUIThread));
}
void CrxInstaller::ReportOverinstallFromUIThread() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
NotificationService* service = NotificationService::current();
service->Notify(NotificationType::EXTENSION_OVERINSTALL_ERROR,
Source<CrxInstaller>(this),
Details<const FilePath>(&extension_->path()));
if (client_.get())
client_->OnOverinstallAttempted(extension_.get());
frontend_->OnExtensionOverinstallAttempted(extension_->id());
}
void CrxInstaller::ReportSuccessFromFileThread() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &CrxInstaller::ReportSuccessFromUIThread));
}
void CrxInstaller::ReportSuccessFromUIThread() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
// If there is a client, tell the client about installation.
if (client_.get())
client_->OnInstallSuccess(extension_.get());
// Tell the frontend about the installation and hand off ownership of
// extension_ to it.
frontend_->OnExtensionInstalled(extension_.release(),
allow_privilege_increase_);
// We're done. We don't post any more tasks to ourselves so we are deleted
// soon.
}
|