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