summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/zipfile_installer.cc
blob: f78fb4a6850e6f38eda3d909d05d5ed4bca38ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/zipfile_installer.h"

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "chrome/browser/extensions/extension_error_reporter.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/chrome_utility_extensions_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/utility_process_host.h"

using content::BrowserThread;
using content::UtilityProcessHost;

namespace {

const char kExtensionHandlerTempDirError[] =
    "Could not create temporary directory for zipped extension.";

}  // namespace

namespace extensions {

ZipFileInstaller::ZipFileInstaller(ExtensionService* extension_service)
    : be_noisy_on_failure_(true),
      extension_service_weak_(extension_service->AsWeakPtr()) {
}

void ZipFileInstaller::LoadFromZipFile(const base::FilePath& path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  zip_path_ = path;
  BrowserThread::PostTask(BrowserThread::FILE,
                          FROM_HERE,
                          base::Bind(&ZipFileInstaller::PrepareTempDir, this));
}

void ZipFileInstaller::PrepareTempDir() {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  base::FilePath temp_dir;
  PathService::Get(base::DIR_TEMP, &temp_dir);
  base::FilePath new_temp_dir;
  if (!base::CreateTemporaryDirInDir(
          temp_dir,
          zip_path_.RemoveExtension().BaseName().value() +
              FILE_PATH_LITERAL("_"),
          &new_temp_dir)) {
    OnUnzipFailed(std::string(kExtensionHandlerTempDirError));
    return;
  }
  BrowserThread::PostTask(
      BrowserThread::IO,
      FROM_HERE,
      base::Bind(&ZipFileInstaller::StartWorkOnIOThread, this, new_temp_dir));
}

ZipFileInstaller::~ZipFileInstaller() {
}

// static
scoped_refptr<ZipFileInstaller> ZipFileInstaller::Create(
    ExtensionService* extension_service) {
  DCHECK(extension_service);
  return scoped_refptr<ZipFileInstaller>(
      new ZipFileInstaller(extension_service));
}

void ZipFileInstaller::StartWorkOnIOThread(const base::FilePath& temp_dir) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  UtilityProcessHost* host =
      UtilityProcessHost::Create(this, base::MessageLoopProxy::current().get());
  host->SetExposedDir(temp_dir);
  host->Send(new ChromeUtilityMsg_UnzipToDir(zip_path_, temp_dir));
}

void ZipFileInstaller::ReportSuccessOnUIThread(
    const base::FilePath& unzipped_path) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (extension_service_weak_.get())
    UnpackedInstaller::Create(extension_service_weak_.get())
        ->Load(unzipped_path);
}

void ZipFileInstaller::ReportErrorOnUIThread(const std::string& error) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (extension_service_weak_.get()) {
    ExtensionErrorReporter::GetInstance()->ReportLoadError(
        zip_path_,
        error,
        extension_service_weak_->profile(),
        be_noisy_on_failure_);
  }
}

void ZipFileInstaller::OnUnzipSucceeded(const base::FilePath& unzipped_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  BrowserThread::PostTask(
      BrowserThread::UI,
      FROM_HERE,
      base::Bind(
          &ZipFileInstaller::ReportSuccessOnUIThread, this, unzipped_path));
}

void ZipFileInstaller::OnUnzipFailed(const std::string& error) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  BrowserThread::PostTask(
      BrowserThread::UI,
      FROM_HERE,
      base::Bind(&ZipFileInstaller::ReportErrorOnUIThread, this, error));
}

bool ZipFileInstaller::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ZipFileInstaller, message)
  IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Succeeded,
                      OnUnzipSucceeded)
  IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_UnzipToDir_Failed, OnUnzipFailed)
  IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

}  // namespace extensions