1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/startup_helper.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/sandboxed_unpacker.h"
18 #include "chrome/browser/extensions/webstore_startup_installer.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/extensions/chrome_extensions_client.h"
22 #include "components/crx_file/id_util.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/web_contents.h"
25 #include "extensions/common/extension.h"
26 #include "ipc/ipc_message.h"
29 #include "extensions/browser/app_window/app_window.h"
30 #include "extensions/browser/app_window/app_window_registry.h"
31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_util.h"
35 using content::BrowserThread;
37 namespace extensions {
41 void PrintPackExtensionMessage(const std::string& message) {
45 // On Windows, the jumplist action for installing an ephemeral app has to use
46 // the --install-ephemeral-app-from-webstore command line arg to initiate an
48 scoped_refptr<WebstoreStandaloneInstaller> CreateEphemeralAppInstaller(
50 const std::string& app_id,
51 WebstoreStandaloneInstaller::Callback callback) {
52 scoped_refptr<WebstoreStandaloneInstaller> installer;
55 ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
57 if (!registry->GetExtensionById(app_id, ExtensionRegistry::EVERYTHING) ||
58 !util::IsEphemeralApp(app_id, profile)) {
62 AppWindowRegistry* app_window_registry = AppWindowRegistry::Get(profile);
63 DCHECK(app_window_registry);
64 AppWindow* app_window =
65 app_window_registry->GetCurrentAppWindowForApp(app_id);
69 installer = new WebstoreInstallWithPrompt(
70 app_id, profile, app_window->GetNativeWindow(), callback);
78 StartupHelper::StartupHelper() : pack_job_succeeded_(false) {
79 ExtensionsClient::Set(ChromeExtensionsClient::GetInstance());
82 void StartupHelper::OnPackSuccess(
83 const base::FilePath& crx_path,
84 const base::FilePath& output_private_key_path) {
85 pack_job_succeeded_ = true;
86 PrintPackExtensionMessage(
88 PackExtensionJob::StandardSuccessMessage(crx_path,
89 output_private_key_path)));
92 void StartupHelper::OnPackFailure(const std::string& error_message,
93 ExtensionCreator::ErrorType type) {
94 PrintPackExtensionMessage(error_message);
97 bool StartupHelper::PackExtension(const CommandLine& cmd_line) {
98 if (!cmd_line.HasSwitch(switches::kPackExtension))
102 base::FilePath src_dir =
103 cmd_line.GetSwitchValuePath(switches::kPackExtension);
104 base::FilePath private_key_path;
105 if (cmd_line.HasSwitch(switches::kPackExtensionKey)) {
106 private_key_path = cmd_line.GetSwitchValuePath(switches::kPackExtensionKey);
109 // Launch a job to perform the packing on the file thread. Ignore warnings
110 // from the packing process. (e.g. Overwrite any existing crx file.)
111 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path,
112 ExtensionCreator::kOverwriteCRX);
113 pack_job_->set_asynchronous(false);
116 return pack_job_succeeded_;
121 class ValidateCrxHelper : public SandboxedUnpackerClient {
123 ValidateCrxHelper(const base::FilePath& crx_file,
124 const base::FilePath& temp_dir,
125 base::RunLoop* run_loop)
126 : crx_file_(crx_file), temp_dir_(temp_dir), run_loop_(run_loop),
127 finished_(false), success_(false) {}
129 bool finished() { return finished_; }
130 bool success() { return success_; }
131 const base::string16& error() { return error_; }
134 BrowserThread::PostTask(BrowserThread::FILE,
136 base::Bind(&ValidateCrxHelper::StartOnFileThread,
141 virtual ~ValidateCrxHelper() {}
143 virtual void OnUnpackSuccess(const base::FilePath& temp_dir,
144 const base::FilePath& extension_root,
145 const base::DictionaryValue* original_manifest,
146 const Extension* extension,
147 const SkBitmap& install_icon) OVERRIDE {
150 BrowserThread::PostTask(BrowserThread::UI,
152 base::Bind(&ValidateCrxHelper::FinishOnUIThread,
156 virtual void OnUnpackFailure(const base::string16& error) OVERRIDE {
160 BrowserThread::PostTask(BrowserThread::UI,
162 base::Bind(&ValidateCrxHelper::FinishOnUIThread,
166 void FinishOnUIThread() {
167 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
168 if (run_loop_->running())
172 void StartOnFileThread() {
173 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
174 scoped_refptr<base::MessageLoopProxy> file_thread_proxy =
175 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
177 scoped_refptr<SandboxedUnpacker> unpacker(
178 new SandboxedUnpacker(crx_file_,
180 0, /* no special creation flags */
182 file_thread_proxy.get(),
187 // The file being validated.
188 const base::FilePath& crx_file_;
190 // The temporary directory where the sandboxed unpacker will do work.
191 const base::FilePath& temp_dir_;
193 // Unowned pointer to a runloop, so our consumer can wait for us to finish.
194 base::RunLoop* run_loop_;
196 // Whether we're finished unpacking;
199 // Whether the unpacking was successful.
202 // If the unpacking wasn't successful, this contains an error message.
203 base::string16 error_;
208 bool StartupHelper::ValidateCrx(const CommandLine& cmd_line,
209 std::string* error) {
211 base::FilePath path = cmd_line.GetSwitchValuePath(switches::kValidateCrx);
213 *error = base::StringPrintf("Empty path passed for %s",
214 switches::kValidateCrx);
217 base::ScopedTempDir temp_dir;
219 if (!temp_dir.CreateUniqueTempDir()) {
220 *error = std::string("Failed to create temp dir");
224 base::RunLoop run_loop;
225 scoped_refptr<ValidateCrxHelper> helper(
226 new ValidateCrxHelper(path, temp_dir.path(), &run_loop));
228 if (!helper->finished())
231 bool success = helper->success();
233 *error = base::UTF16ToUTF8(helper->error());
239 class AppInstallHelper {
241 // A callback for when the install process is done.
242 typedef base::Callback<void()> DoneCallback;
245 virtual ~AppInstallHelper();
246 bool success() { return success_; }
247 const std::string& error() { return error_; }
248 void BeginInstall(Profile* profile,
249 const std::string& id,
251 DoneCallback callback);
254 WebstoreStandaloneInstaller::Callback Callback();
255 void OnAppInstallComplete(bool success,
256 const std::string& error,
257 webstore_install::Result result);
259 DoneCallback done_callback_;
261 // These hold on to the result of the app install when it is complete.
265 scoped_refptr<WebstoreStandaloneInstaller> installer_;
268 AppInstallHelper::AppInstallHelper() : success_(false) {}
270 AppInstallHelper::~AppInstallHelper() {}
272 WebstoreStandaloneInstaller::Callback AppInstallHelper::Callback() {
273 return base::Bind(&AppInstallHelper::OnAppInstallComplete,
274 base::Unretained(this));
277 void AppInstallHelper::BeginInstall(
279 const std::string& id,
281 DoneCallback done_callback) {
282 done_callback_ = done_callback;
284 WebstoreStandaloneInstaller::Callback callback =
285 base::Bind(&AppInstallHelper::OnAppInstallComplete,
286 base::Unretained(this));
288 installer_ = CreateEphemeralAppInstaller(profile, id, callback);
289 if (installer_.get()) {
290 installer_->BeginInstall();
292 error_ = "Not a supported ephemeral app installation.";
293 done_callback_.Run();
297 void AppInstallHelper::OnAppInstallComplete(bool success,
298 const std::string& error,
299 webstore_install::Result result) {
302 done_callback_.Run();
307 bool StartupHelper::InstallEphemeralApp(const CommandLine& cmd_line,
310 cmd_line.GetSwitchValueASCII(switches::kInstallEphemeralAppFromWebstore);
311 if (!crx_file::id_util::IdIsValid(id)) {
312 LOG(ERROR) << "Invalid id for "
313 << switches::kInstallEphemeralAppFromWebstore << " : '" << id << "'";
317 AppInstallHelper helper;
318 base::RunLoop run_loop;
319 helper.BeginInstall(profile, id, true, run_loop.QuitClosure());
322 if (!helper.success())
323 LOG(ERROR) << "InstallFromWebstore failed with error: " << helper.error();
324 return helper.success();
327 StartupHelper::~StartupHelper() {
329 pack_job_->ClearClient();
332 } // namespace extensions