Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / shell_integration.cc
blob2ac4b5896e9b7a6900d43331a66bd853698a581f
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 // static
33 ShellIntegration::DefaultWebClientSetPermission
34 ShellIntegration::CanSetAsDefaultProtocolClient() {
35 // Allowed as long as the browser can become the operating system default
36 // browser.
37 return CanSetAsDefaultBrowser();
40 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
42 // static
43 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
44 gAppModeInfo = info;
47 // static
48 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
49 return gAppModeInfo;
52 // static
53 bool ShellIntegration::IsRunningInAppMode() {
54 return gAppModeInfo != NULL;
57 // static
58 base::CommandLine ShellIntegration::CommandLineArgsForLauncher(
59 const GURL& url,
60 const std::string& extension_app_id,
61 const base::FilePath& profile_path) {
62 base::ThreadRestrictions::AssertIOAllowed();
63 base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);
65 AppendProfileArgs(
66 extension_app_id.empty() ? base::FilePath() : profile_path,
67 &new_cmd_line);
69 // If |extension_app_id| is present, we use the kAppId switch rather than
70 // the kApp switch (the launch url will be read from the extension app
71 // during launch.
72 if (!extension_app_id.empty()) {
73 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
74 } else {
75 // Use '--app=url' instead of just 'url' to launch the browser with minimal
76 // chrome.
77 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
78 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
80 return new_cmd_line;
83 // static
84 void ShellIntegration::AppendProfileArgs(const base::FilePath& profile_path,
85 base::CommandLine* command_line) {
86 DCHECK(command_line);
87 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
89 // Use the same UserDataDir for new launches that we currently have set.
90 base::FilePath user_data_dir =
91 cmd_line.GetSwitchValuePath(switches::kUserDataDir);
92 #if defined(OS_MACOSX) || defined(OS_WIN)
93 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
94 #endif
95 if (!user_data_dir.empty()) {
96 // Make sure user_data_dir is an absolute path.
97 user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
98 if (!user_data_dir.empty() && base::PathExists(user_data_dir))
99 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
102 #if defined(OS_CHROMEOS)
103 base::FilePath profile = cmd_line.GetSwitchValuePath(
104 chromeos::switches::kLoginProfile);
105 if (!profile.empty())
106 command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
107 #else
108 if (!profile_path.empty())
109 command_line->AppendSwitchPath(switches::kProfileDirectory,
110 profile_path.BaseName());
111 #endif
114 #if !defined(OS_WIN)
116 base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
117 if (chrome::GetChannel() == version_info::Channel::CANARY)
118 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
119 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
122 // static
123 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
124 return false;
127 // static
128 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
129 const std::string& protocol) {
130 return false;
133 // static
134 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() {
135 return false;
138 #endif // !defined(OS_WIN)
140 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
141 return false;
144 bool ShellIntegration::DefaultWebClientObserver::
145 IsInteractiveSetDefaultPermitted() {
146 return false;
149 ///////////////////////////////////////////////////////////////////////////////
150 // ShellIntegration::DefaultWebClientWorker
153 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
154 DefaultWebClientObserver* observer)
155 : observer_(observer) {
158 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
159 if (observer_) {
160 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
161 BrowserThread::PostTask(
162 BrowserThread::FILE, FROM_HERE,
163 base::Bind(
164 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
168 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
169 bool interactive_permitted = false;
170 if (observer_) {
171 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
172 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
174 BrowserThread::PostTask(
175 BrowserThread::FILE, FROM_HERE,
176 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
177 interactive_permitted));
180 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
181 // Our associated view has gone away, so we shouldn't call back to it if
182 // our worker thread returns after the view is dead.
183 DCHECK_CURRENTLY_ON(BrowserThread::UI);
184 observer_ = NULL;
187 ///////////////////////////////////////////////////////////////////////////////
188 // DefaultWebClientWorker, private:
190 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
191 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
192 DefaultWebClientState state = CheckIsDefault();
193 BrowserThread::PostTask(
194 BrowserThread::UI, FROM_HERE,
195 base::Bind(
196 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
199 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
200 DefaultWebClientState state) {
201 DCHECK_CURRENTLY_ON(BrowserThread::UI);
202 UpdateUI(state);
203 // The worker has finished everything it needs to do, so free the observer
204 // if we own it.
205 if (observer_ && observer_->IsOwnedByWorker()) {
206 delete observer_;
207 observer_ = NULL;
211 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
212 bool interactive_permitted) {
213 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
215 bool result = SetAsDefault(interactive_permitted);
216 BrowserThread::PostTask(
217 BrowserThread::UI, FROM_HERE,
218 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
221 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
222 bool succeeded) {
223 DCHECK_CURRENTLY_ON(BrowserThread::UI);
224 // First tell the observer what the SetAsDefault call has returned.
225 if (observer_)
226 observer_->OnSetAsDefaultConcluded(succeeded);
227 // Set as default completed, check again to make sure it stuck...
228 StartCheckIsDefault();
231 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
232 DefaultWebClientState state) {
233 if (observer_) {
234 switch (state) {
235 case NOT_DEFAULT:
236 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
237 break;
238 case IS_DEFAULT:
239 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
240 break;
241 case UNKNOWN_DEFAULT:
242 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
243 break;
244 default:
245 break;
250 ///////////////////////////////////////////////////////////////////////////////
251 // ShellIntegration::DefaultBrowserWorker
254 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
255 DefaultWebClientObserver* observer)
256 : DefaultWebClientWorker(observer) {
259 ///////////////////////////////////////////////////////////////////////////////
260 // DefaultBrowserWorker, private:
262 ShellIntegration::DefaultWebClientState
263 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
264 return ShellIntegration::GetDefaultBrowser();
267 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
268 bool interactive_permitted) {
269 bool result = false;
270 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
271 case ShellIntegration::SET_DEFAULT_UNATTENDED:
272 result = ShellIntegration::SetAsDefaultBrowser();
273 break;
274 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
275 if (interactive_permitted)
276 result = ShellIntegration::SetAsDefaultBrowserInteractive();
277 break;
278 default:
279 NOTREACHED();
282 return result;
285 ///////////////////////////////////////////////////////////////////////////////
286 // ShellIntegration::DefaultProtocolClientWorker
289 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
290 DefaultWebClientObserver* observer, const std::string& protocol)
291 : DefaultWebClientWorker(observer),
292 protocol_(protocol) {
295 ///////////////////////////////////////////////////////////////////////////////
296 // DefaultProtocolClientWorker, private:
298 ShellIntegration::DefaultWebClientState
299 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
300 return ShellIntegration::IsDefaultProtocolClient(protocol_);
303 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
304 bool interactive_permitted) {
305 bool result = false;
306 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
307 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED:
308 result = false;
309 break;
310 case ShellIntegration::SET_DEFAULT_UNATTENDED:
311 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
312 break;
313 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
314 if (interactive_permitted) {
315 result = ShellIntegration::SetAsDefaultProtocolClientInteractive(
316 protocol_);
318 break;
321 return result;