Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / shell_integration.cc
blob46d749623963117100a5748083cf0edf5074c189
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 "content/public/browser/browser_thread.h"
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/chromeos_switches.h"
21 #endif
23 #if !defined(OS_WIN)
24 #include "chrome/common/chrome_version_info.h"
25 #include "chrome/grit/chromium_strings.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #endif
29 using content::BrowserThread;
31 ShellIntegration::DefaultWebClientSetPermission
32 ShellIntegration::CanSetAsDefaultProtocolClient() {
33 // Allowed as long as the browser can become the operating system default
34 // browser.
35 return CanSetAsDefaultBrowser();
38 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
40 // static
41 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
42 gAppModeInfo = info;
45 // static
46 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
47 return gAppModeInfo;
50 // static
51 bool ShellIntegration::IsRunningInAppMode() {
52 return gAppModeInfo != NULL;
55 // static
56 base::CommandLine ShellIntegration::CommandLineArgsForLauncher(
57 const GURL& url,
58 const std::string& extension_app_id,
59 const base::FilePath& profile_path) {
60 base::ThreadRestrictions::AssertIOAllowed();
61 base::CommandLine new_cmd_line(base::CommandLine::NO_PROGRAM);
63 AppendProfileArgs(
64 extension_app_id.empty() ? base::FilePath() : profile_path,
65 &new_cmd_line);
67 // If |extension_app_id| is present, we use the kAppId switch rather than
68 // the kApp switch (the launch url will be read from the extension app
69 // during launch.
70 if (!extension_app_id.empty()) {
71 new_cmd_line.AppendSwitchASCII(switches::kAppId, extension_app_id);
72 } else {
73 // Use '--app=url' instead of just 'url' to launch the browser with minimal
74 // chrome.
75 // Note: Do not change this flag! Old Gears shortcuts will break if you do!
76 new_cmd_line.AppendSwitchASCII(switches::kApp, url.spec());
78 return new_cmd_line;
81 // static
82 void ShellIntegration::AppendProfileArgs(const base::FilePath& profile_path,
83 base::CommandLine* command_line) {
84 DCHECK(command_line);
85 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
87 // Use the same UserDataDir for new launches that we currently have set.
88 base::FilePath user_data_dir =
89 cmd_line.GetSwitchValuePath(switches::kUserDataDir);
90 #if defined(OS_MACOSX) || defined(OS_WIN)
91 policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
92 #endif
93 if (!user_data_dir.empty()) {
94 // Make sure user_data_dir is an absolute path.
95 user_data_dir = base::MakeAbsoluteFilePath(user_data_dir);
96 if (!user_data_dir.empty() && base::PathExists(user_data_dir))
97 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir);
100 #if defined(OS_CHROMEOS)
101 base::FilePath profile = cmd_line.GetSwitchValuePath(
102 chromeos::switches::kLoginProfile);
103 if (!profile.empty())
104 command_line->AppendSwitchPath(chromeos::switches::kLoginProfile, profile);
105 #else
106 if (!profile_path.empty())
107 command_line->AppendSwitchPath(switches::kProfileDirectory,
108 profile_path.BaseName());
109 #endif
112 #if !defined(OS_WIN)
114 base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
115 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY)
116 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
117 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
120 // static
121 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
122 return false;
125 // static
126 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
127 const std::string& protocol) {
128 return false;
130 #endif
132 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
133 return false;
136 bool ShellIntegration::DefaultWebClientObserver::
137 IsInteractiveSetDefaultPermitted() {
138 return false;
141 ///////////////////////////////////////////////////////////////////////////////
142 // ShellIntegration::DefaultWebClientWorker
145 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
146 DefaultWebClientObserver* observer)
147 : observer_(observer) {
150 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() {
151 if (observer_) {
152 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
153 BrowserThread::PostTask(
154 BrowserThread::FILE, FROM_HERE,
155 base::Bind(
156 &DefaultWebClientWorker::ExecuteCheckIsDefault, this));
160 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
161 bool interactive_permitted = false;
162 if (observer_) {
163 observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
164 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
166 BrowserThread::PostTask(
167 BrowserThread::FILE, FROM_HERE,
168 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
169 interactive_permitted));
172 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() {
173 // Our associated view has gone away, so we shouldn't call back to it if
174 // our worker thread returns after the view is dead.
175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
176 observer_ = NULL;
179 ///////////////////////////////////////////////////////////////////////////////
180 // DefaultWebClientWorker, private:
182 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
184 DefaultWebClientState state = CheckIsDefault();
185 BrowserThread::PostTask(
186 BrowserThread::UI, FROM_HERE,
187 base::Bind(
188 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state));
191 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault(
192 DefaultWebClientState state) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 UpdateUI(state);
195 // The worker has finished everything it needs to do, so free the observer
196 // if we own it.
197 if (observer_ && observer_->IsOwnedByWorker()) {
198 delete observer_;
199 observer_ = NULL;
203 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
204 bool interactive_permitted) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
207 bool result = SetAsDefault(interactive_permitted);
208 BrowserThread::PostTask(
209 BrowserThread::UI, FROM_HERE,
210 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
213 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault(
214 bool succeeded) {
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 // First tell the observer what the SetAsDefault call has returned.
217 if (observer_)
218 observer_->OnSetAsDefaultConcluded(succeeded);
219 // Set as default completed, check again to make sure it stuck...
220 StartCheckIsDefault();
223 void ShellIntegration::DefaultWebClientWorker::UpdateUI(
224 DefaultWebClientState state) {
225 if (observer_) {
226 switch (state) {
227 case NOT_DEFAULT:
228 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT);
229 break;
230 case IS_DEFAULT:
231 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT);
232 break;
233 case UNKNOWN_DEFAULT:
234 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN);
235 break;
236 default:
237 break;
242 ///////////////////////////////////////////////////////////////////////////////
243 // ShellIntegration::DefaultBrowserWorker
246 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
247 DefaultWebClientObserver* observer)
248 : DefaultWebClientWorker(observer) {
251 ///////////////////////////////////////////////////////////////////////////////
252 // DefaultBrowserWorker, private:
254 ShellIntegration::DefaultWebClientState
255 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
256 return ShellIntegration::GetDefaultBrowser();
259 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault(
260 bool interactive_permitted) {
261 bool result = false;
262 switch (ShellIntegration::CanSetAsDefaultBrowser()) {
263 case ShellIntegration::SET_DEFAULT_UNATTENDED:
264 result = ShellIntegration::SetAsDefaultBrowser();
265 break;
266 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
267 if (interactive_permitted)
268 result = ShellIntegration::SetAsDefaultBrowserInteractive();
269 break;
270 default:
271 NOTREACHED();
274 return result;
277 ///////////////////////////////////////////////////////////////////////////////
278 // ShellIntegration::DefaultProtocolClientWorker
281 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
282 DefaultWebClientObserver* observer, const std::string& protocol)
283 : DefaultWebClientWorker(observer),
284 protocol_(protocol) {
287 ///////////////////////////////////////////////////////////////////////////////
288 // DefaultProtocolClientWorker, private:
290 ShellIntegration::DefaultWebClientState
291 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
292 return ShellIntegration::IsDefaultProtocolClient(protocol_);
295 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault(
296 bool interactive_permitted) {
297 bool result = false;
298 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) {
299 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED:
300 result = false;
301 break;
302 case ShellIntegration::SET_DEFAULT_UNATTENDED:
303 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_);
304 break;
305 case ShellIntegration::SET_DEFAULT_INTERACTIVE:
306 if (interactive_permitted) {
307 result = ShellIntegration::SetAsDefaultProtocolClientInteractive(
308 protocol_);
310 break;
313 return result;