extensions_startup.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/extensions_startup.h"
6
7#include "base/string_util.h"
8#include "base/stringprintf.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/common/chrome_switches.h"
13
14#if defined(OS_WIN)
15#include "app/win_util.h"
16#endif
17
18ExtensionsStartupUtil::ExtensionsStartupUtil() : pack_job_succeeded_(false) {}
19
20void ExtensionsStartupUtil::OnPackSuccess(
21    const FilePath& crx_path,
22    const FilePath& output_private_key_path) {
23  pack_job_succeeded_ = true;
24  ShowPackExtensionMessage(L"Extension Packaging Success",
25                           PackExtensionJob::StandardSuccessMessage(
26                               crx_path, output_private_key_path));
27}
28
29void ExtensionsStartupUtil::OnPackFailure(const std::string& error_message) {
30  ShowPackExtensionMessage(L"Extension Packaging Error",
31                           UTF8ToWide(error_message));
32}
33
34void ExtensionsStartupUtil::ShowPackExtensionMessage(
35    const std::wstring& caption,
36    const std::wstring& message) {
37#if defined(OS_WIN)
38  win_util::MessageBox(NULL, message, caption, MB_OK | MB_SETFOREGROUND);
39#else
40  // Just send caption & text to stdout on mac & linux.
41  std::string out_text = WideToASCII(caption);
42  out_text.append("\n\n");
43  out_text.append(WideToASCII(message));
44  out_text.append("\n");
45  base::StringPrintf("%s", out_text.c_str());
46#endif
47}
48
49bool ExtensionsStartupUtil::PackExtension(const CommandLine& cmd_line) {
50  if (!cmd_line.HasSwitch(switches::kPackExtension))
51    return false;
52
53  // Input Paths.
54  FilePath src_dir = cmd_line.GetSwitchValuePath(switches::kPackExtension);
55  FilePath private_key_path;
56  if (cmd_line.HasSwitch(switches::kPackExtensionKey)) {
57    private_key_path = cmd_line.GetSwitchValuePath(switches::kPackExtensionKey);
58  }
59
60  // Launch a job to perform the packing on the file thread.
61  pack_job_ = new PackExtensionJob(this, src_dir, private_key_path);
62  pack_job_->set_asynchronous(false);
63  pack_job_->Start();
64
65  return pack_job_succeeded_;
66}
67
68bool ExtensionsStartupUtil::UninstallExtension(const CommandLine& cmd_line,
69                                               Profile* profile) {
70  DCHECK(profile);
71
72  if (!cmd_line.HasSwitch(switches::kUninstallExtension))
73    return false;
74
75  ExtensionService* extension_service = profile->GetExtensionService();
76  if (!extension_service)
77    return false;
78
79  std::string extension_id = cmd_line.GetSwitchValueASCII(
80      switches::kUninstallExtension);
81  if (ExtensionService::UninstallExtensionHelper(extension_service,
82                                                 extension_id)) {
83    return true;
84  }
85
86  return false;
87}
88
89ExtensionsStartupUtil::~ExtensionsStartupUtil() {
90  if (pack_job_.get())
91    pack_job_->ClearClient();
92}
93