OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extensions_startup.h" | 5 #include "chrome/browser/extensions/extensions_startup.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extensions_service.h" | 10 #include "chrome/browser/extensions/extensions_service.h" |
11 #include "chrome/browser/extensions/pack_extension_job.h" | |
12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
14 | 13 |
15 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
16 #include "app/win_util.h" | 15 #include "app/win_util.h" |
17 #endif | 16 #endif |
18 | 17 |
19 namespace extensions_startup { | 18 ExtensionsStartupUtil::ExtensionsStartupUtil() : pack_job_succeeded_(false) {} |
20 | 19 |
21 class PackExtensionLogger : public PackExtensionJob::Client { | 20 void ExtensionsStartupUtil::OnPackSuccess( |
22 public: | |
23 PackExtensionLogger() : process_startup_(false) {} | |
24 virtual void OnPackSuccess(const FilePath& crx_path, | |
25 const FilePath& output_private_key_path); | |
26 virtual void OnPackFailure(const std::string& error_message); | |
27 | |
28 private: | |
29 // We need to track if this extension packing job was created on process | |
30 // startup or not so we know if we should Quit() the message loop after | |
31 // packaging the extension. | |
32 bool process_startup_; | |
33 void ShowPackExtensionMessage(const std::wstring& caption, | |
34 const std::wstring& message); | |
35 | |
36 DISALLOW_COPY_AND_ASSIGN(PackExtensionLogger); | |
37 }; | |
38 | |
39 void PackExtensionLogger::OnPackSuccess( | |
40 const FilePath& crx_path, | 21 const FilePath& crx_path, |
41 const FilePath& output_private_key_path) { | 22 const FilePath& output_private_key_path) { |
| 23 pack_job_succeeded_ = true; |
42 ShowPackExtensionMessage(L"Extension Packaging Success", | 24 ShowPackExtensionMessage(L"Extension Packaging Success", |
43 PackExtensionJob::StandardSuccessMessage( | 25 PackExtensionJob::StandardSuccessMessage( |
44 crx_path, output_private_key_path)); | 26 crx_path, output_private_key_path)); |
45 } | 27 } |
46 | 28 |
47 void PackExtensionLogger::OnPackFailure(const std::string& error_message) { | 29 void ExtensionsStartupUtil::OnPackFailure(const std::string& error_message) { |
48 ShowPackExtensionMessage(L"Extension Packaging Error", | 30 ShowPackExtensionMessage(L"Extension Packaging Error", |
49 UTF8ToWide(error_message)); | 31 UTF8ToWide(error_message)); |
50 } | 32 } |
51 | 33 |
52 void PackExtensionLogger::ShowPackExtensionMessage( | 34 void ExtensionsStartupUtil::ShowPackExtensionMessage( |
53 const std::wstring& caption, | 35 const std::wstring& caption, |
54 const std::wstring& message) { | 36 const std::wstring& message) { |
55 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
56 win_util::MessageBox(NULL, message, caption, MB_OK | MB_SETFOREGROUND); | 38 win_util::MessageBox(NULL, message, caption, MB_OK | MB_SETFOREGROUND); |
57 #else | 39 #else |
58 // Just send caption & text to stdout on mac & linux. | 40 // Just send caption & text to stdout on mac & linux. |
59 std::string out_text = WideToASCII(caption); | 41 std::string out_text = WideToASCII(caption); |
60 out_text.append("\n\n"); | 42 out_text.append("\n\n"); |
61 out_text.append(WideToASCII(message)); | 43 out_text.append(WideToASCII(message)); |
62 out_text.append("\n"); | 44 out_text.append("\n"); |
63 base::StringPrintf("%s", out_text.c_str()); | 45 base::StringPrintf("%s", out_text.c_str()); |
64 #endif | 46 #endif |
65 | |
66 // We got the notification and processed it; we don't expect any further tasks | |
67 // to be posted to the current thread, so we should stop blocking and exit. | |
68 // This call to |Quit()| matches the call to |Run()| in | |
69 // |ProcessCmdLineImpl()|. | |
70 MessageLoop::current()->Quit(); | |
71 } | 47 } |
72 | 48 |
73 bool HandlePackExtension(const CommandLine& cmd_line) { | 49 bool ExtensionsStartupUtil::PackExtension(const CommandLine& cmd_line) { |
74 if (cmd_line.HasSwitch(switches::kPackExtension)) { | 50 if (!cmd_line.HasSwitch(switches::kPackExtension)) |
75 // Input Paths. | 51 return false; |
76 FilePath src_dir = cmd_line.GetSwitchValuePath( | |
77 switches::kPackExtension); | |
78 FilePath private_key_path; | |
79 if (cmd_line.HasSwitch(switches::kPackExtensionKey)) { | |
80 private_key_path = cmd_line.GetSwitchValuePath( | |
81 switches::kPackExtensionKey); | |
82 } | |
83 | 52 |
84 // Launch a job to perform the packing on the file thread. | 53 // Input Paths. |
85 PackExtensionLogger pack_client; | 54 FilePath src_dir = cmd_line.GetSwitchValuePath(switches::kPackExtension); |
86 scoped_refptr<PackExtensionJob> pack_job( | 55 FilePath private_key_path; |
87 new PackExtensionJob(&pack_client, src_dir, private_key_path)); | 56 if (cmd_line.HasSwitch(switches::kPackExtensionKey)) { |
88 pack_job->Start(); | 57 private_key_path = cmd_line.GetSwitchValuePath(switches::kPackExtensionKey); |
| 58 } |
89 | 59 |
90 // The job will post a notification task to the current thread's message | 60 // Launch a job to perform the packing on the file thread. |
91 // loop when it is finished. We manually run the loop here so that we | 61 pack_job_ = new PackExtensionJob(this, src_dir, private_key_path); |
92 // block and catch the notification. Otherwise, the process would exit; | 62 pack_job_->set_asynchronous(false); |
93 // in particular, this would mean that |pack_client| would be destroyed | 63 pack_job_->Start(); |
94 // and we wouldn't be able to report success or failure back to the user. | |
95 // This call to |Run()| is matched by a call to |Quit()| in the | |
96 // |PackExtensionLogger|'s notification handling code. | |
97 MessageLoop::current()->Run(); | |
98 | 64 |
| 65 return pack_job_succeeded_; |
| 66 } |
| 67 |
| 68 bool 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 ExtensionsService* extensions_service = profile->GetExtensionsService(); |
| 76 if (!extensions_service) |
| 77 return false; |
| 78 |
| 79 std::string extension_id = cmd_line.GetSwitchValueASCII( |
| 80 switches::kUninstallExtension); |
| 81 if (ExtensionsService::UninstallExtensionHelper(extensions_service, |
| 82 extension_id)) { |
99 return true; | 83 return true; |
100 } | 84 } |
101 | 85 |
102 return false; | 86 return false; |
103 } | 87 } |
104 | 88 |
105 bool HandleUninstallExtension(const CommandLine& cmd_line, Profile* profile) { | 89 ExtensionsStartupUtil::~ExtensionsStartupUtil() { |
106 DCHECK(profile); | 90 if (pack_job_.get()) |
107 | 91 pack_job_->ClearClient(); |
108 if (cmd_line.HasSwitch(switches::kUninstallExtension)) { | |
109 ExtensionsService* extensions_service = profile->GetExtensionsService(); | |
110 if (extensions_service) { | |
111 std::string extension_id = cmd_line.GetSwitchValueASCII( | |
112 switches::kUninstallExtension); | |
113 if (ExtensionsService::UninstallExtensionHelper(extensions_service, | |
114 extension_id)) { | |
115 return true; | |
116 } | |
117 } | |
118 } | |
119 | |
120 return false; | |
121 } | 92 } |
122 | |
123 } // namespace extensions_startup | |
OLD | NEW |