blob: c51ed25af9ae88ec9273be7bccefede6c0d0467a [file] [log] [blame]
[email protected]3bf6ade02009-07-23 19:51:181// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]4a0765a2009-05-08 23:12:255#include "chrome/browser/shell_integration.h"
6
[email protected]28375ae2010-02-05 04:45:507#include "base/command_line.h"
8#include "base/file_util.h"
[email protected]b96aa932009-08-12 21:34:499#include "base/path_service.h"
[email protected]5d91c9e2010-07-28 17:25:2810#include "base/string_util.h"
[email protected]64048bd2010-03-08 23:28:5811#include "base/utf_string_conversions.h"
[email protected]b96aa932009-08-12 21:34:4912#include "chrome/common/chrome_paths.h"
[email protected]28375ae2010-02-05 04:45:5013#include "chrome/common/chrome_switches.h"
[email protected]0d3dc8e22009-11-03 02:27:0114#include "chrome/browser/chrome_thread.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]28375ae2010-02-05 04:45:5016std::string ShellIntegration::GetCommandLineArgumentsCommon(const GURL& url,
17 const string16& extension_app_id) {
18 const CommandLine cmd = *CommandLine::ForCurrentProcess();
19 std::wstring arguments_w;
20
21 // Use the same UserDataDir for new launches that we currently have set.
[email protected]63597e4e2010-07-08 17:49:0522 FilePath user_data_dir = cmd.GetSwitchValuePath(switches::kUserDataDir);
23 if (!user_data_dir.value().empty()) {
[email protected]28375ae2010-02-05 04:45:5024 // Make sure user_data_dir is an absolute path.
25 if (file_util::AbsolutePath(&user_data_dir) &&
[email protected]63597e4e2010-07-08 17:49:0526 file_util::PathExists(user_data_dir)) {
27 // TODO: This is wrong in pathological quoting scenarios; we shouldn't be
28 // passing around command lines as strings at all.
[email protected]28375ae2010-02-05 04:45:5029 arguments_w += std::wstring(L"--") + ASCIIToWide(switches::kUserDataDir) +
[email protected]63597e4e2010-07-08 17:49:0530 L"=\"" + user_data_dir.ToWStringHack() + L"\" ";
[email protected]28375ae2010-02-05 04:45:5031 }
32 }
33
[email protected]660e428f2010-08-04 01:23:0034#if defined(OS_CHROMEOS)
35 FilePath profile = cmd.GetSwitchValuePath(switches::kLoginProfile);
[email protected]28375ae2010-02-05 04:45:5036 if (!profile.empty()) {
[email protected]74d1397a2010-08-02 16:33:3337 arguments_w += std::wstring(L"--") + ASCIIToWide(switches::kLoginProfile) +
[email protected]660e428f2010-08-04 01:23:0038 L"=\"" + profile.ToWStringHack() + L"\" ";
[email protected]28375ae2010-02-05 04:45:5039 }
40#endif
41
42 // If |extension_app_id| is present, we use the kAppId switch rather than
43 // the kApp switch (the launch url will be read from the extension app
44 // during launch.
[email protected]ec5b50d2010-10-09 16:35:1845 if (!extension_app_id.empty()) {
[email protected]28375ae2010-02-05 04:45:5046 arguments_w += std::wstring(L"--") + ASCIIToWide(switches::kAppId) +
[email protected]385e5b5692010-08-02 22:56:3847 L"=\"" + ASCIIToWide(UTF16ToASCII(extension_app_id));
[email protected]64048bd2010-03-08 23:28:5848 } else {
[email protected]28375ae2010-02-05 04:45:5049 // Use '--app=url' instead of just 'url' to launch the browser with minimal
50 // chrome.
51 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
52 std::string url_string = url.spec();
53 ReplaceSubstringsAfterOffset(&url_string, 0, "\\", "%5C");
54 ReplaceSubstringsAfterOffset(&url_string, 0, "\"", "%22");
55 ReplaceSubstringsAfterOffset(&url_string, 0, ";", "%3B");
56 ReplaceSubstringsAfterOffset(&url_string, 0, "$", "%24");
57#if defined(OS_WIN) // Windows shortcuts can't escape % so we use \x instead.
58 ReplaceSubstringsAfterOffset(&url_string, 0, "%", "\\x");
59#endif
60 std::wstring url_w = UTF8ToWide(url_string);
61 arguments_w += std::wstring(L"--") + ASCIIToWide(switches::kApp) +
62 L"=\"" + url_w + L"\"";
63 }
64 return WideToUTF8(arguments_w);
65}
66
[email protected]d24c4012009-07-28 01:57:3167///////////////////////////////////////////////////////////////////////////////
68// ShellIntegration::DefaultBrowserWorker
69//
initial.commit09911bf2008-07-26 23:55:2970
[email protected]d24c4012009-07-28 01:57:3171ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
72 DefaultBrowserObserver* observer)
[email protected]0d3dc8e22009-11-03 02:27:0173 : observer_(observer) {
initial.commit09911bf2008-07-26 23:55:2974}
75
[email protected]d24c4012009-07-28 01:57:3176void ShellIntegration::DefaultBrowserWorker::StartCheckDefaultBrowser() {
77 observer_->SetDefaultBrowserUIState(STATE_PROCESSING);
[email protected]0d3dc8e22009-11-03 02:27:0178 ChromeThread::PostTask(
79 ChromeThread::FILE, FROM_HERE,
80 NewRunnableMethod(
81 this, &DefaultBrowserWorker::ExecuteCheckDefaultBrowser));
initial.commit09911bf2008-07-26 23:55:2982}
83
[email protected]d24c4012009-07-28 01:57:3184void ShellIntegration::DefaultBrowserWorker::StartSetAsDefaultBrowser() {
85 observer_->SetDefaultBrowserUIState(STATE_PROCESSING);
[email protected]0d3dc8e22009-11-03 02:27:0186 ChromeThread::PostTask(
87 ChromeThread::FILE, FROM_HERE,
88 NewRunnableMethod(
89 this, &DefaultBrowserWorker::ExecuteSetAsDefaultBrowser));
[email protected]d24c4012009-07-28 01:57:3190}
91
92void ShellIntegration::DefaultBrowserWorker::ObserverDestroyed() {
93 // Our associated view has gone away, so we shouldn't call back to it if
94 // our worker thread returns after the view is dead.
95 observer_ = NULL;
96}
97
98///////////////////////////////////////////////////////////////////////////////
99// DefaultBrowserWorker, private:
100
101void ShellIntegration::DefaultBrowserWorker::ExecuteCheckDefaultBrowser() {
[email protected]0d3dc8e22009-11-03 02:27:01102 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
[email protected]264f74d12009-09-04 23:39:58103 DefaultBrowserState state = ShellIntegration::IsDefaultBrowser();
[email protected]0d3dc8e22009-11-03 02:27:01104 ChromeThread::PostTask(
105 ChromeThread::UI, FROM_HERE,
106 NewRunnableMethod(
107 this, &DefaultBrowserWorker::CompleteCheckDefaultBrowser, state));
[email protected]d24c4012009-07-28 01:57:31108}
109
110void ShellIntegration::DefaultBrowserWorker::CompleteCheckDefaultBrowser(
[email protected]264f74d12009-09-04 23:39:58111 DefaultBrowserState state) {
[email protected]0d3dc8e22009-11-03 02:27:01112 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]264f74d12009-09-04 23:39:58113 UpdateUI(state);
[email protected]d24c4012009-07-28 01:57:31114}
115
116void ShellIntegration::DefaultBrowserWorker::ExecuteSetAsDefaultBrowser() {
[email protected]0d3dc8e22009-11-03 02:27:01117 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
[email protected]d24c4012009-07-28 01:57:31118 ShellIntegration::SetAsDefaultBrowser();
[email protected]0d3dc8e22009-11-03 02:27:01119 ChromeThread::PostTask(
120 ChromeThread::UI, FROM_HERE,
121 NewRunnableMethod(
122 this, &DefaultBrowserWorker::CompleteSetAsDefaultBrowser));
[email protected]d24c4012009-07-28 01:57:31123}
124
125void ShellIntegration::DefaultBrowserWorker::CompleteSetAsDefaultBrowser() {
[email protected]0d3dc8e22009-11-03 02:27:01126 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
[email protected]d24c4012009-07-28 01:57:31127 if (observer_) {
128 // Set as default completed, check again to make sure it stuck...
129 StartCheckDefaultBrowser();
initial.commit09911bf2008-07-26 23:55:29130 }
[email protected]d24c4012009-07-28 01:57:31131}
132
[email protected]264f74d12009-09-04 23:39:58133void ShellIntegration::DefaultBrowserWorker::UpdateUI(
134 DefaultBrowserState state) {
[email protected]d24c4012009-07-28 01:57:31135 if (observer_) {
[email protected]264f74d12009-09-04 23:39:58136 switch (state) {
137 case NOT_DEFAULT_BROWSER:
138 observer_->SetDefaultBrowserUIState(STATE_NOT_DEFAULT);
139 break;
140 case IS_DEFAULT_BROWSER:
141 observer_->SetDefaultBrowserUIState(STATE_IS_DEFAULT);
142 break;
143 case UNKNOWN_DEFAULT_BROWSER:
144 observer_->SetDefaultBrowserUIState(STATE_UNKNOWN);
145 break;
146 default:
147 break;
148 }
[email protected]d24c4012009-07-28 01:57:31149 }
initial.commit09911bf2008-07-26 23:55:29150}