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/webstore_startup_installer.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/chrome_extensions_client.h"
21 #include "components/crx_file/id_util.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/web_contents.h"
24 #include "extensions/browser/sandboxed_unpacker.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 base::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 CRXFileInfo
& file
,
124 const base::FilePath
& temp_dir
,
125 base::RunLoop
* run_loop
)
132 bool finished() { return finished_
; }
133 bool success() { return success_
; }
134 const base::string16
& error() { return error_
; }
137 BrowserThread::PostTask(BrowserThread::FILE,
139 base::Bind(&ValidateCrxHelper::StartOnFileThread
,
144 ~ValidateCrxHelper() override
{}
146 void OnUnpackSuccess(const base::FilePath
& temp_dir
,
147 const base::FilePath
& extension_root
,
148 const base::DictionaryValue
* original_manifest
,
149 const Extension
* extension
,
150 const SkBitmap
& install_icon
) override
{
153 BrowserThread::PostTask(BrowserThread::UI
,
155 base::Bind(&ValidateCrxHelper::FinishOnUIThread
,
159 void OnUnpackFailure(const CrxInstallError
& error
) override
{
162 error_
= error
.message();
163 BrowserThread::PostTask(BrowserThread::UI
,
165 base::Bind(&ValidateCrxHelper::FinishOnUIThread
,
169 void FinishOnUIThread() {
170 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
171 if (run_loop_
->running())
175 void StartOnFileThread() {
176 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
177 scoped_refptr
<base::MessageLoopProxy
> file_thread_proxy
=
178 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
180 scoped_refptr
<SandboxedUnpacker
> unpacker(
181 new SandboxedUnpacker(crx_file_
,
183 0, /* no special creation flags */
185 file_thread_proxy
.get(),
190 // The file being validated.
191 const CRXFileInfo
& crx_file_
;
193 // The temporary directory where the sandboxed unpacker will do work.
194 const base::FilePath
& temp_dir_
;
196 // Unowned pointer to a runloop, so our consumer can wait for us to finish.
197 base::RunLoop
* run_loop_
;
199 // Whether we're finished unpacking;
202 // Whether the unpacking was successful.
205 // If the unpacking wasn't successful, this contains an error message.
206 base::string16 error_
;
211 bool StartupHelper::ValidateCrx(const base::CommandLine
& cmd_line
,
212 std::string
* error
) {
214 base::FilePath path
= cmd_line
.GetSwitchValuePath(switches::kValidateCrx
);
216 *error
= base::StringPrintf("Empty path passed for %s",
217 switches::kValidateCrx
);
220 base::ScopedTempDir temp_dir
;
222 if (!temp_dir
.CreateUniqueTempDir()) {
223 *error
= std::string("Failed to create temp dir");
227 base::RunLoop run_loop
;
228 CRXFileInfo
file(path
);
229 scoped_refptr
<ValidateCrxHelper
> helper(
230 new ValidateCrxHelper(file
, temp_dir
.path(), &run_loop
));
232 if (!helper
->finished())
235 bool success
= helper
->success();
237 *error
= base::UTF16ToUTF8(helper
->error());
243 class AppInstallHelper
{
245 // A callback for when the install process is done.
246 typedef base::Callback
<void()> DoneCallback
;
249 virtual ~AppInstallHelper();
250 bool success() { return success_
; }
251 const std::string
& error() { return error_
; }
252 void BeginInstall(Profile
* profile
,
253 const std::string
& id
,
255 DoneCallback callback
);
258 WebstoreStandaloneInstaller::Callback
Callback();
259 void OnAppInstallComplete(bool success
,
260 const std::string
& error
,
261 webstore_install::Result result
);
263 DoneCallback done_callback_
;
265 // These hold on to the result of the app install when it is complete.
269 scoped_refptr
<WebstoreStandaloneInstaller
> installer_
;
272 AppInstallHelper::AppInstallHelper() : success_(false) {}
274 AppInstallHelper::~AppInstallHelper() {}
276 WebstoreStandaloneInstaller::Callback
AppInstallHelper::Callback() {
277 return base::Bind(&AppInstallHelper::OnAppInstallComplete
,
278 base::Unretained(this));
281 void AppInstallHelper::BeginInstall(
283 const std::string
& id
,
285 DoneCallback done_callback
) {
286 done_callback_
= done_callback
;
288 WebstoreStandaloneInstaller::Callback callback
=
289 base::Bind(&AppInstallHelper::OnAppInstallComplete
,
290 base::Unretained(this));
292 installer_
= CreateEphemeralAppInstaller(profile
, id
, callback
);
293 if (installer_
.get()) {
294 installer_
->BeginInstall();
296 error_
= "Not a supported ephemeral app installation.";
297 done_callback_
.Run();
301 void AppInstallHelper::OnAppInstallComplete(bool success
,
302 const std::string
& error
,
303 webstore_install::Result result
) {
306 done_callback_
.Run();
311 bool StartupHelper::InstallEphemeralApp(const base::CommandLine
& cmd_line
,
314 cmd_line
.GetSwitchValueASCII(switches::kInstallEphemeralAppFromWebstore
);
315 if (!crx_file::id_util::IdIsValid(id
)) {
316 LOG(ERROR
) << "Invalid id for "
317 << switches::kInstallEphemeralAppFromWebstore
<< " : '" << id
<< "'";
321 AppInstallHelper helper
;
322 base::RunLoop run_loop
;
323 helper
.BeginInstall(profile
, id
, true, run_loop
.QuitClosure());
326 if (!helper
.success())
327 LOG(ERROR
) << "InstallFromWebstore failed with error: " << helper
.error();
328 return helper
.success();
331 StartupHelper::~StartupHelper() {
333 pack_job_
->ClearClient();
336 } // namespace extensions