Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / zipfile_installer.cc
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.
4
5 #include "chrome/browser/extensions/zipfile_installer.h"
6
7 #include "base/files/file_util.h"
8 #include "base/path_service.h"
9 #include "chrome/browser/extensions/extension_error_reporter.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/unpacked_installer.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/utility_process_host.h"
16
17 using content::BrowserThread;
18 using content::UtilityProcessHost;
19
20 namespace {
21
22 const char kExtensionHandlerTempDirError[] =
23     "Could not create temporary directory for zipped extension.";
24
25 }  // namespace
26
27 namespace extensions {
28
29 ZipFileInstaller::ZipFileInstaller(ExtensionService* extension_service)
30     : be_noisy_on_failure_(true),
31       extension_service_weak_(extension_service->AsWeakPtr()) {
32 }
33
34 void ZipFileInstaller::LoadFromZipFile(const base::FilePath& path) {
35   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36   zip_path_ = path;
37   BrowserThread::PostTask(BrowserThread::FILE,
38                           FROM_HERE,
39                           base::Bind(&ZipFileInstaller::PrepareTempDir, this));
40 }
41
42 void ZipFileInstaller::PrepareTempDir() {
43   CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
44   base::FilePath temp_dir;
45   PathService::Get(base::DIR_TEMP, &temp_dir);
46   base::FilePath new_temp_dir;
47   if (!base::CreateTemporaryDirInDir(
48           temp_dir,
49           zip_path_.RemoveExtension().BaseName().value() +
50               FILE_PATH_LITERAL("_"),
51           &new_temp_dir)) {
52     OnUnzipFailed(std::string(kExtensionHandlerTempDirError));
53     return;
54   }
55   BrowserThread::PostTask(
56       BrowserThread::IO,
57       FROM_HERE,
58       base::Bind(&ZipFileInstaller::StartWorkOnIOThread, this, new_temp_dir));
59 }
60
61 ZipFileInstaller::~ZipFileInstaller() {
62 }
63
64 // static
65 scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create(
66     ExtensionService* extension_service) {
67   DCHECK(extension_service);
68   return scoped_refptr<ZipFileInstaller>(
69       new ZipFileInstaller(extension_service));
70 }
71
72 void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) {
73   CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
74   UtilityProcessHost* host =
75       UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get());
76   host->SetExposedDir(temp_dir);
77   host->Send(new ChromeUtilityMsg_UnzipToDir(zip_path_, temp_dir));
78 }
79
80 void ZipFileInstaller::ReportSuccessOnUIThread(
81     const base::FilePath& unzipped_path) {
82   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83   if (extension_service_weak_.get())
84     UnpackedInstaller::Create(extension_service_weak_.get())
85         ->Load(unzipped_path);
86 }
87
88 void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) {
89   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
90   if (extension_service_weak_.get()) {
91     ExtensionErrorReporter::GetInstance()->ReportLoadError(
92         zip_path_,
93         error,
94         extension_service_weak_->profile(),
95         be_noisy_on_failure_);
96   }
97 }
98
99 void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath& unzipped_path) {
100   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
101   BrowserThread::PostTask(
102       BrowserThread::UI,
103       FROM_HERE,
104       base::Bind(
105           &ZipFileInstaller::ReportSuccessOnUIThread, this, unzipped_path));
106 }
107
108 void ZipFileInstaller::OnUnzipFailed(const std::string& error) {
109   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
110   BrowserThread::PostTask(
111       BrowserThread::UI,
112       FROM_HERE,
113       base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, error));
114 }
115
116 bool ZipFileInstaller::OnMessageReceived(const IPC::Message& message) {
117   bool handled = true;
118   IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller, message)
119   IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Succeeded,
120                       OnUnzipSucceeded)
121   IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Failed, OnUnzipFailed)
122   IPC_MESSAGE_UNHANDLED(handled = false)
123   IPC_END_MESSAGE_MAP()
124   return handled;
125 }
126
127 }  // namespace extensions