Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / zipfile_installer.cc
blobb6a68de94987a0572f7fcc61ed30bd1aaef55dd7
1 // Copyright 2014 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/zipfile_installer.h"
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/extensions/extension_error_reporter.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/unpacked_installer.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/utility_process_host.h"
17 #include "extensions/common/extension_utility_messages.h"
18 #include "ui/base/l10n/l10n_util.h"
20 using content::BrowserThread;
21 using content::UtilityProcessHost;
23 namespace {
25 const char kExtensionHandlerTempDirError[] =
26 "Could not create temporary directory for zipped extension.";
28 } // namespace
30 namespace extensions {
32 ZipFileInstaller::ZipFileInstaller(ExtensionService* extension_service)
33 : be_noisy_on_failure_(true),
34 extension_service_weak_(extension_service->AsWeakPtr()) {
37 void ZipFileInstaller::LoadFromZipFile(const base::FilePath& path) {
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 zip_path_ = path;
40 BrowserThread::PostTask(BrowserThread::FILE,
41 FROM_HERE,
42 base::Bind(&ZipFileInstaller::PrepareTempDir, this));
45 void ZipFileInstaller::PrepareTempDir() {
46 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
47 base::FilePath temp_dir;
48 PathService::Get(base::DIR_TEMP, &temp_dir);
49 base::FilePath new_temp_dir;
50 if (!base::CreateTemporaryDirInDir(
51 temp_dir,
52 zip_path_.RemoveExtension().BaseName().value() +
53 FILE_PATH_LITERAL("_"),
54 &new_temp_dir)) {
55 OnUnzipFailed(std::string(kExtensionHandlerTempDirError));
56 return;
58 BrowserThread::PostTask(
59 BrowserThread::IO,
60 FROM_HERE,
61 base::Bind(&ZipFileInstaller::StartWorkOnIOThread, this, new_temp_dir));
64 ZipFileInstaller::~ZipFileInstaller() {
67 // static
68 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create(
69 ExtensionService* extension_service) {
70 DCHECK(extension_service);
71 return scoped_refptr<ZipFileInstaller>(
72 new ZipFileInstaller(extension_service));
75 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) {
76 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
77 UtilityProcessHost* host =
78 UtilityProcessHost::Create(this,
79 base::ThreadTaskRunnerHandle::Get().get());
80 host->SetName(l10n_util::GetStringUTF16(
81 IDS_UTILITY_PROCESS_ZIP_FILE_INSTALLER_NAME));
82 host->SetExposedDir(temp_dir);
83 host->Send(new ExtensionUtilityMsg_UnzipToDir(zip_path_, temp_dir));
86 void ZipFileInstaller::ReportSuccessOnUIThread(
87 const base::FilePath& unzipped_path) {
88 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
89 if (extension_service_weak_.get())
90 UnpackedInstaller::Create(extension_service_weak_.get())
91 ->Load(unzipped_path);
94 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) {
95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 if (extension_service_weak_.get()) {
97 ExtensionErrorReporter::GetInstance()->ReportLoadError(
98 zip_path_,
99 error,
100 extension_service_weak_->profile(),
101 be_noisy_on_failure_);
105 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath& unzipped_path) {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO);
107 BrowserThread::PostTask(
108 BrowserThread::UI,
109 FROM_HERE,
110 base::Bind(
111 &ZipFileInstaller::ReportSuccessOnUIThread, this, unzipped_path));
114 void ZipFileInstaller::OnUnzipFailed(const std::string& error) {
115 DCHECK_CURRENTLY_ON(BrowserThread::IO);
116 BrowserThread::PostTask(
117 BrowserThread::UI,
118 FROM_HERE,
119 base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, error));
122 bool ZipFileInstaller::OnMessageReceived(const IPC::Message& message) {
123 bool handled = true;
124 IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller, message)
125 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Succeeded,
126 OnUnzipSucceeded)
127 IPC_MESSAGE_HANDLER(ExtensionUtilityHostMsg_UnzipToDir_Failed, OnUnzipFailed)
128 IPC_MESSAGE_UNHANDLED(handled = false)
129 IPC_END_MESSAGE_MAP()
130 return handled;
133 } // namespace extensions