Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / shell_integration.cc
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.
4
5 #include "chrome/browser/shell_integration.h"
6
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"
18
19 #if defined(OS_CHROMEOS)
20 #include "chromeos/chromeos_switches.h"
21 #endif
22
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
28
29 using content::BrowserThread;
30
31 ShellIntegration::DefaultWebClientSetPermission
32     ShellIntegration::CanSetAsDefaultProtocolClient() {
33   // Allowed as long as the browser can become the operating system default
34   // browser.
35   return CanSetAsDefaultBrowser();
36 }
37
38 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL;
39
40 // static
41 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) {
42   gAppModeInfo = info;
43 }
44
45 // static
46 const struct ShellIntegration::AppModeInfo* ShellIntegration::AppModeInfo() {
47   return gAppModeInfo;
48 }
49
50 // static
51 bool ShellIntegration::IsRunningInAppMode() {
52   return gAppModeInfo != NULL;
53 }
54
55 // static
56 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   CommandLine new_cmd_line(CommandLine::NO_PROGRAM);
62
63   AppendProfileArgs(
64       extension_app_id.empty() ? base::FilePath() : profile_path,
65       &new_cmd_line);
66
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());
77   }
78   return new_cmd_line;
79 }
80
81 // static
82 void ShellIntegration::AppendProfileArgs(
83     const base::FilePath& profile_path,
84     CommandLine* command_line) {
85   DCHECK(command_line);
86   const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
87
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);
99   }
100
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
111 }
112
113 #if !defined(OS_WIN)
114
115 base::string16 ShellIntegration::GetAppShortcutsSubdirName() {
116   if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_CANARY)
117     return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY);
118   return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME);
119 }
120
121 // static
122 bool ShellIntegration::SetAsDefaultBrowserInteractive() {
123   return false;
124 }
125
126 // static
127 bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
128     const std::string& protocol) {
129   return false;
130 }
131 #endif
132
133 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() {
134   return false;
135 }
136
137 bool ShellIntegration::DefaultWebClientObserver::
138     IsInteractiveSetDefaultPermitted() {
139   return false;
140 }
141
142 ///////////////////////////////////////////////////////////////////////////////
143 // ShellIntegration::DefaultWebClientWorker
144 //
145
146 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker(
147     DefaultWebClientObserver* observer)
148     : observer_(observer) {
149 }
150
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));
158   }
159 }
160
161 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() {
162   bool interactive_permitted = false;
163   if (observer_) {
164     observer_->SetDefaultWebClientUIState(STATE_PROCESSING);
165     interactive_permitted = observer_->IsInteractiveSetDefaultPermitted();
166   }
167   BrowserThread::PostTask(
168       BrowserThread::FILE, FROM_HERE,
169       base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this,
170                  interactive_permitted));
171 }
172
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;
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // DefaultWebClientWorker, private:
182
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));
190 }
191
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;
201   }
202 }
203
204 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault(
205     bool interactive_permitted) {
206   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
207
208   bool result = SetAsDefault(interactive_permitted);
209   BrowserThread::PostTask(
210       BrowserThread::UI, FROM_HERE,
211       base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result));
212 }
213
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();
222 }
223
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;
239     }
240   }
241 }
242
243 ///////////////////////////////////////////////////////////////////////////////
244 // ShellIntegration::DefaultBrowserWorker
245 //
246
247 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker(
248     DefaultWebClientObserver* observer)
249     : DefaultWebClientWorker(observer) {
250 }
251
252 ///////////////////////////////////////////////////////////////////////////////
253 // DefaultBrowserWorker, private:
254
255 ShellIntegration::DefaultWebClientState
256 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() {
257   return ShellIntegration::GetDefaultBrowser();
258 }
259
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();
273   }
274
275   return result;
276 }
277
278 ///////////////////////////////////////////////////////////////////////////////
279 // ShellIntegration::DefaultProtocolClientWorker
280 //
281
282 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker(
283     DefaultWebClientObserver* observer, const std::string& protocol)
284     : DefaultWebClientWorker(observer),
285       protocol_(protocol) {
286 }
287
288 ///////////////////////////////////////////////////////////////////////////////
289 // DefaultProtocolClientWorker, private:
290
291 ShellIntegration::DefaultWebClientState
292 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() {
293   return ShellIntegration::IsDefaultProtocolClient(protocol_);
294 }
295
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_);
310       }
311       break;
312   }
313
314   return result;
315 }