Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / shell_integration.cc
blobebaf4158c023df9f8d5fc6f649eaf3e48b92a12e
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/shell_integration.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "chrome/browser/policy/policy_path_parser.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "components/version_info/version_info.h"
18 #include "content/public/browser/browser_thread.h"
20 #if defined(OS_CHROMEOS)
21 #include "chromeos/chromeos_switches.h"
22 #endif
24 #if !defined(OS_WIN)
25 #include "chrome/common/channel_info.h"
26 #include "chrome/grit/chromium_strings.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #endif
30 using content::BrowserThread;
32 ShellIntegration::DefaultWebClientSetPermission
33 ShellIntegration::CanSetAsDefaultProtocolClient() {
34 // Allowed as long as the browser can become the operating system default
35 // browser.
36 return CanSetAsDefaultBrowser();
39 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
41 // static
42 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
43 gAppModeInfo = info;
46 // static
47 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
48 return gAppModeInfo;
51 // static
52 bool ShellIntegration::IsRunningInAppMode() {
53 return gAppModeInfo != NULL;
56 // static
57 base::CommandLine ShellIntegration::CommandLineArgsForLauncher(
58 const GURL& url,
59 const std::string& extension_app_id,
60 const base::FilePath& profile_path) {
61 base::ThreadRestrictions::AssertIOAllowed();
62 base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);
64 AppendProfileArgs(
65 extension_app_id.empty() ? base::FilePath() : profile_path,
66 &new_cmd_line);
68 // If |extension_app_id| is present, we use the kAppId switch rather than
69 // the kApp switch (the launch url will be read from the extension app
70 // during launch.
71 if (!extension_app_id.empty()) {
72 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
73 } else {
74 // Use '--app=url' instead of just 'url' to launch the browser with minimal
75 // chrome.
76 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
77 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
79 return new_cmd_line;
82 // static
83 void ShellIntegration::AppendProfileArgs(const base::FilePath& profile_path,
84 base::CommandLine* command_line) {
85 DCHECK(command_line);
86 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
88 // Use the same UserDataDir for new launches that we currently have set.
89 base::FilePath user_data_dir =
90 cmd_line.GetSwitchValuePath(switches::kUserDataDir);
91 #if defined(OS_MACOSX) || defined(OS_WIN)
92 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
93 #endif
94 if (!user_data_dir.empty()) {
95 // Make sure user_data_dir is an absolute path.
96 user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
97 if (!user_data_dir.empty() && base::PathExists(user_data_dir))
98 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
101 #if defined(OS_CHROMEOS)
102 base::FilePath profile = cmd_line.GetSwitchValuePath(
103 chromeos::switches::kLoginProfile);
104 if (!profile.empty())
105 command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
106 #else
107 if (!profile_path.empty())
108 command_line->AppendSwitchPath(switches::kProfileDirectory,
109 profile_path.BaseName());
110 #endif
113 #if !defined(OS_WIN)
115 base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
116 if (chrome::GetChannel() == version_info::Channel::CANARY)
117 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
118 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
121 // static
122 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
123 return false;
126 // static
127 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
128 const std::string& protocol) {
129 return false;
131 #endif
133 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
134 return false;
137 bool ShellIntegration::DefaultWebClientObserver::
138 IsInteractiveSetDefaultPermitted() {
139 return false;
142 ///////////////////////////////////////////////////////////////////////////////
143 // ShellIntegration::DefaultWebClientWorker
146 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
147 DefaultWebClientObserver* observer)
148 : observer_(observer) {
151 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
152 if (observer_) {
153 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
154 BrowserThread::PostTask(
155 BrowserThread::FILE, FROM_HERE,
156 base::Bind(
157 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
161 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
162 bool interactive_permitted = false;
163 if (observer_) {
164 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
165 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
167 BrowserThread::PostTask(
168 BrowserThread::FILE, FROM_HERE,
169 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
170 interactive_permitted));
173 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
174 // Our associated view has gone away, so we shouldn't call back to it if
175 // our worker thread returns after the view is dead.
176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
177 observer_ = NULL;
180 ///////////////////////////////////////////////////////////////////////////////
181 // DefaultWebClientWorker, private:
183 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
185 DefaultWebClientState state = CheckIsDefault();
186 BrowserThread::PostTask(
187 BrowserThread::UI, FROM_HERE,
188 base::Bind(
189 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
192 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
193 DefaultWebClientState state) {
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
195 UpdateUI(state);
196 // The worker has finished everything it needs to do, so free the observer
197 // if we own it.
198 if (observer_ && observer_->IsOwnedByWorker()) {
199 delete observer_;
200 observer_ = NULL;
204 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
205 bool interactive_permitted) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
208 bool result = SetAsDefault(interactive_permitted);
209 BrowserThread::PostTask(
210 BrowserThread::UI, FROM_HERE,
211 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
214 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
215 bool succeeded) {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
217 // First tell the observer what the SetAsDefault call has returned.
218 if (observer_)
219 observer_->OnSetAsDefaultConcluded(succeeded);
220 // Set as default completed, check again to make sure it stuck...
221 StartCheckIsDefault();
224 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
225 DefaultWebClientState state) {
226 if (observer_) {
227 switch (state) {
228 case NOT_DEFAULT:
229 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
230 break;
231 case IS_DEFAULT:
232 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
233 break;
234 case UNKNOWN_DEFAULT:
235 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
236 break;
237 default:
238 break;
243 ///////////////////////////////////////////////////////////////////////////////
244 // ShellIntegration::DefaultBrowserWorker
247 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
248 DefaultWebClientObserver* observer)
249 : DefaultWebClientWorker(observer) {
252 ///////////////////////////////////////////////////////////////////////////////
253 // DefaultBrowserWorker, private:
255 ShellIntegration::DefaultWebClientState
256 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
257 return ShellIntegration::GetDefaultBrowser();
260 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
261 bool interactive_permitted) {
262 bool result = false;
263 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
264 case ShellIntegration::SET_DEFAULT_UNATTENDED:
265 result = ShellIntegration::SetAsDefaultBrowser();
266 break;
267 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
268 if (interactive_permitted)
269 result = ShellIntegration::SetAsDefaultBrowserInteractive();
270 break;
271 default:
272 NOTREACHED();
275 return result;
278 ///////////////////////////////////////////////////////////////////////////////
279 // ShellIntegration::DefaultProtocolClientWorker
282 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
283 DefaultWebClientObserver* observer, const std::string& protocol)
284 : DefaultWebClientWorker(observer),
285 protocol_(protocol) {
288 ///////////////////////////////////////////////////////////////////////////////
289 // DefaultProtocolClientWorker, private:
291 ShellIntegration::DefaultWebClientState
292 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
293 return ShellIntegration::IsDefaultProtocolClient(protocol_);
296 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
297 bool interactive_permitted) {
298 bool result = false;
299 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
300 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED:
301 result = false;
302 break;
303 case ShellIntegration::SET_DEFAULT_UNATTENDED:
304 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
305 break;
306 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
307 if (interactive_permitted) {
308 result = ShellIntegration::SetAsDefaultProtocolClientInteractive(
309 protocol_);
311 break;
314 return result;