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

Side by Side Diff: chrome/browser/extensions/crx_installer.cc

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month 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 (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 "chrome/browser/extensions/crx_installer.h" 5 #include "chrome/browser/extensions/crx_installer.h"
6 6
7 #include "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "base/task.h" 11 #include "base/task.h"
12 #include "chrome/browser/browser_process.h" 12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chrome_thread.h" 13 #include "chrome/browser/chrome_thread.h"
14 #include "chrome/browser/extensions/convert_user_script.h"
14 #include "chrome/browser/extensions/extension_file_util.h" 15 #include "chrome/browser/extensions/extension_file_util.h"
15 #include "chrome/common/extensions/extension_error_reporter.h" 16 #include "chrome/common/extensions/extension_error_reporter.h"
16 #include "chrome/common/notification_service.h" 17 #include "chrome/common/notification_service.h"
17 #include "chrome/common/notification_type.h" 18 #include "chrome/common/notification_type.h"
18 #include "grit/chromium_strings.h" 19 #include "grit/chromium_strings.h"
19 #include "third_party/skia/include/core/SkBitmap.h" 20 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "webkit/glue/image_decoder.h" 21 #include "webkit/glue/image_decoder.h"
21 22
22 namespace { 23 namespace {
23 // Helper function to delete files. This is used to avoid ugly casts which 24 // Helper function to delete files. This is used to avoid ugly casts which
24 // would be necessary with PostMessage since file_util::Delete is overloaded. 25 // would be necessary with PostMessage since file_util::Delete is overloaded.
25 static void DeleteFileHelper(const FilePath& path, bool recursive) { 26 static void DeleteFileHelper(const FilePath& path, bool recursive) {
26 file_util::Delete(path, recursive); 27 file_util::Delete(path, recursive);
27 } 28 }
28 } 29 }
29 30
30 void CrxInstaller::Start(const FilePath& crx_path, 31 void CrxInstaller::Start(const FilePath& crx_path,
31 const FilePath& install_directory, 32 const FilePath& install_directory,
32 Extension::Location install_source, 33 Extension::Location install_source,
33 const std::string& expected_id, 34 const std::string& expected_id,
34 bool delete_crx, 35 bool delete_source,
35 bool allow_privilege_increase, 36 bool allow_privilege_increase,
36 ExtensionsService* frontend, 37 ExtensionsService* frontend,
37 ExtensionInstallUI* client) { 38 ExtensionInstallUI* client) {
38 // Note: We don't keep a reference because this object manages its own 39 // Note: We don't keep a reference because this object manages its own
39 // lifetime. 40 // lifetime.
40 new CrxInstaller(crx_path, install_directory, install_source, expected_id, 41 CrxInstaller* installer = new CrxInstaller(crx_path, install_directory,
41 delete_crx, allow_privilege_increase, frontend, client); 42 delete_source, frontend,
42 } 43 client);
44 installer->install_source_ = install_source;
45 installer->expected_id_ = expected_id;
46 installer->allow_privilege_increase_ = allow_privilege_increase;
43 47
44 CrxInstaller::CrxInstaller(const FilePath& crx_path, 48 installer->unpacker_ = new SandboxedExtensionUnpacker(
45 const FilePath& install_directory, 49 installer->source_file_, g_browser_process->resource_dispatcher_host(),
46 Extension::Location install_source, 50 installer);
47 const std::string& expected_id,
48 bool delete_crx,
49 bool allow_privilege_increase,
50 ExtensionsService* frontend,
51 ExtensionInstallUI* client)
52 : crx_path_(crx_path),
53 install_directory_(install_directory),
54 install_source_(install_source),
55 expected_id_(expected_id),
56 delete_crx_(delete_crx),
57 allow_privilege_increase_(allow_privilege_increase),
58 frontend_(frontend),
59 client_(client) {
60
61 extensions_enabled_ = frontend_->extensions_enabled();
62
63 unpacker_ = new SandboxedExtensionUnpacker(
64 crx_path, g_browser_process->resource_dispatcher_host(), this);
65 51
66 ChromeThread::PostTask( 52 ChromeThread::PostTask(
67 ChromeThread::FILE, FROM_HERE, 53 ChromeThread::FILE, FROM_HERE,
68 NewRunnableMethod(unpacker_, &SandboxedExtensionUnpacker::Start)); 54 NewRunnableMethod(installer->unpacker_, &SandboxedExtensionUnpacker::Start ));
55 }
56
57 void CrxInstaller::InstallUserScript(const FilePath& source_file,
58 const GURL& original_url,
59 const FilePath& install_directory,
60 bool delete_source,
61 ExtensionsService* frontend,
62 ExtensionInstallUI* client) {
63 CrxInstaller* installer = new CrxInstaller(source_file, install_directory,
64 delete_source, frontend, client);
65 installer->original_url_ = original_url;
66
67 ChromeThread::PostTask(
68 ChromeThread::FILE, FROM_HERE,
69 NewRunnableMethod(installer, &CrxInstaller::ConvertUserScriptOnFileThread) );
70 }
71
72 CrxInstaller::CrxInstaller(const FilePath& source_file,
73 const FilePath& install_directory,
74 bool delete_source,
75 ExtensionsService* frontend,
76 ExtensionInstallUI* client)
77 : source_file_(source_file),
78 install_directory_(install_directory),
79 install_source_(Extension::INTERNAL),
80 delete_source_(delete_source),
81 allow_privilege_increase_(false),
82 frontend_(frontend),
83 client_(client) {
84 extensions_enabled_ = frontend_->extensions_enabled();
69 } 85 }
70 86
71 CrxInstaller::~CrxInstaller() { 87 CrxInstaller::~CrxInstaller() {
72 // Delete the temp directory and crx file as necessary. Note that the 88 // Delete the temp directory and crx file as necessary. Note that the
73 // destructor might be called on any thread, so we post a task to the file 89 // destructor might be called on any thread, so we post a task to the file
74 // thread to make sure the delete happens there. 90 // thread to make sure the delete happens there.
75 if (!temp_dir_.value().empty()) { 91 if (!temp_dir_.value().empty()) {
76 ChromeThread::PostTask( 92 ChromeThread::PostTask(
77 ChromeThread::FILE, FROM_HERE, 93 ChromeThread::FILE, FROM_HERE,
78 NewRunnableFunction(&DeleteFileHelper, temp_dir_, true)); 94 NewRunnableFunction(&DeleteFileHelper, temp_dir_, true));
79 } 95 }
80 96
81 if (delete_crx_) { 97 if (delete_source_) {
82 ChromeThread::PostTask( 98 ChromeThread::PostTask(
83 ChromeThread::FILE, FROM_HERE, 99 ChromeThread::FILE, FROM_HERE,
84 NewRunnableFunction(&DeleteFileHelper, crx_path_, false)); 100 NewRunnableFunction(&DeleteFileHelper, source_file_, false));
85 } 101 }
86 } 102 }
87 103
104 void CrxInstaller::ConvertUserScriptOnFileThread() {
105 std::string error;
106 Extension* extension = ConvertUserScriptToExtension(source_file_,
107 original_url_, &error);
108 if (!extension) {
109 ReportFailureFromFileThread(error);
110 return;
111 }
112
113 OnUnpackSuccess(extension->path(), extension->path(), extension);
114 }
115
88 void CrxInstaller::OnUnpackFailure(const std::string& error_message) { 116 void CrxInstaller::OnUnpackFailure(const std::string& error_message) {
89 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); 117 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
90 ReportFailureFromFileThread(error_message); 118 ReportFailureFromFileThread(error_message);
91 } 119 }
92 120
93 void CrxInstaller::OnUnpackSuccess(const FilePath& temp_dir, 121 void CrxInstaller::OnUnpackSuccess(const FilePath& temp_dir,
94 const FilePath& extension_dir, 122 const FilePath& extension_dir,
95 Extension* extension) { 123 Extension* extension) {
96 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); 124 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
97 125
98 // Note: We take ownership of |extension| and |temp_dir|. 126 // Note: We take ownership of |extension| and |temp_dir|.
99 extension_.reset(extension); 127 extension_.reset(extension);
100 temp_dir_ = temp_dir; 128 temp_dir_ = temp_dir;
101 129
102 // The unpack dir we don't have to delete explicity since it is a child of 130 // The unpack dir we don't have to delete explicity since it is a child of
103 // the temp dir. 131 // the temp dir.
104 unpacked_extension_root_ = extension_dir; 132 unpacked_extension_root_ = extension_dir;
105 DCHECK(file_util::ContainsPath(temp_dir_, unpacked_extension_root_));
106 133
107 // Determine whether to allow installation. We always allow themes and 134 // Determine whether to allow installation. We always allow themes and
108 // external installs. 135 // external installs.
109 if (!extensions_enabled_ && !extension->IsTheme() && 136 if (!extensions_enabled_ && !extension->IsTheme() &&
110 !Extension::IsExternalLocation(install_source_)) { 137 !Extension::IsExternalLocation(install_source_)) {
111 ReportFailureFromFileThread("Extensions are not enabled."); 138 ReportFailureFromFileThread("Extensions are not enabled.");
112 return; 139 return;
113 } 140 }
114 141
115 // Make sure the expected id matches. 142 // Make sure the expected id matches.
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 client_->OnInstallSuccess(extension_.get()); 342 client_->OnInstallSuccess(extension_.get());
316 343
317 // Tell the frontend about the installation and hand off ownership of 344 // Tell the frontend about the installation and hand off ownership of
318 // extension_ to it. 345 // extension_ to it.
319 frontend_->OnExtensionInstalled(extension_.release(), 346 frontend_->OnExtensionInstalled(extension_.release(),
320 allow_privilege_increase_); 347 allow_privilege_increase_);
321 348
322 // We're done. We don't post any more tasks to ourselves so we are deleted 349 // We're done. We don't post any more tasks to ourselves so we are deleted
323 // soon. 350 // soon.
324 } 351 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698