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 #ifndef CHROME_BROWSER_SHELL_INTEGRATION_H_
6 #define CHROME_BROWSER_SHELL_INTEGRATION_H_
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "ui/gfx/image/image_family.h"
21 class ShellIntegration
{
23 // Sets Chrome as the default browser (only for the current user). Returns
24 // false if this operation fails.
25 static bool SetAsDefaultBrowser();
27 // Initiates an OS shell flow which (if followed by the user) should set
28 // Chrome as the default browser. Returns false if the flow cannot be
29 // initialized, if it is not supported (introduced for Windows 8) or if the
30 // user cancels the operation. This is a blocking call and requires a FILE
31 // thread. If Chrome is already default browser, no interactive dialog will be
32 // shown and this method returns true.
33 static bool SetAsDefaultBrowserInteractive();
35 // Sets Chrome as the default client application for the given protocol
36 // (only for the current user). Returns false if this operation fails.
37 static bool SetAsDefaultProtocolClient(const std::string
& protocol
);
39 // Initiates an OS shell flow which (if followed by the user) should set
40 // Chrome as the default handler for |protocol|. Returns false if the flow
41 // cannot be initialized, if it is not supported (introduced for Windows 8)
42 // or if the user cancels the operation. This is a blocking call and requires
43 // a FILE thread. If Chrome is already default for |protocol|, no interactive
44 // dialog will be shown and this method returns true.
45 static bool SetAsDefaultProtocolClientInteractive(
46 const std::string
& protocol
);
48 // In Windows 8 a browser can be made default-in-metro only in an interactive
49 // flow. We will distinguish between two types of permissions here to avoid
50 // forcing the user into UI interaction when this should not be done.
51 enum DefaultWebClientSetPermission
{
52 SET_DEFAULT_NOT_ALLOWED
,
53 SET_DEFAULT_UNATTENDED
,
54 SET_DEFAULT_INTERACTIVE
,
57 // Returns requirements for making the running browser the user's default.
58 static DefaultWebClientSetPermission
CanSetAsDefaultBrowser();
60 // Returns requirements for making the running browser the user's default
61 // client application for specific protocols.
62 static DefaultWebClientSetPermission
CanSetAsDefaultProtocolClient();
64 // Returns true if making the running browser the default client for any
65 // protocol requires elevated privileges.
66 static bool IsElevationNeededForSettingDefaultProtocolClient();
68 // Returns a string representing the application to be launched given the
69 // protocol of the requested url. This string may be a name or a path, but
70 // neither is guaranteed and it should only be used as a display string.
71 // Returns an empty string on failure.
72 static base::string16
GetApplicationNameForProtocol(const GURL
& url
);
74 // On Linux, it may not be possible to determine or set the default browser
75 // on some desktop environments or configurations. So, we use this enum and
77 enum DefaultWebClientState
{
84 // Attempt to determine if this instance of Chrome is the default browser and
85 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS
86 // protocols; we don't want to report "no" here if the user has simply chosen
87 // to open HTML files in a text editor and FTP links with an FTP client.)
88 static DefaultWebClientState
GetDefaultBrowser();
90 // Returns true if Firefox is likely to be the default browser for the current
91 // user. This method is very fast so it can be invoked in the UI thread.
92 static bool IsFirefoxDefaultBrowser();
94 // Attempt to determine if this instance of Chrome is the default client
95 // application for the given protocol and return the appropriate state.
96 static DefaultWebClientState
97 IsDefaultProtocolClient(const std::string
& protocol
);
99 // Data that needs to be passed between the app launcher stub and Chrome.
102 static void SetAppModeInfo(const AppModeInfo
* info
);
103 static const AppModeInfo
* AppModeInfo();
105 // Is the current instance of Chrome running in App mode.
106 bool IsRunningInAppMode();
108 // Set up command line arguments for launching a URL or an app.
109 // The new command line reuses the current process's user data directory (and
110 // login profile, for ChromeOS).
111 // If |extension_app_id| is non-empty, the arguments use kAppId=<id>.
112 // Otherwise, kApp=<url> is used.
113 static base::CommandLine
CommandLineArgsForLauncher(
115 const std::string
& extension_app_id
,
116 const base::FilePath
& profile_path
);
118 // Append command line arguments for launching a new chrome.exe process
119 // based on the current process.
120 // The new command line reuses the current process's user data directory and
122 static void AppendProfileArgs(const base::FilePath
& profile_path
,
123 base::CommandLine
* command_line
);
126 // Generates an application user model ID (AppUserModelId) for a given app
127 // name and profile path. The returned app id is in the format of
128 // "|app_name|[.<profile_id>]". "profile_id" is appended when user override
129 // the default value.
130 // Note: If the app has an installation specific suffix (e.g. on user-level
131 // Chrome installs), |app_name| should already be suffixed, this method will
132 // then further suffix it with the profile id as described above.
133 static base::string16
GetAppModelIdForProfile(const base::string16
& app_name
,
134 const base::FilePath
& profile_path
);
136 // Generates an application user model ID (AppUserModelId) for Chromium by
137 // calling GetAppModelIdForProfile() with ShellUtil::GetAppId() as app_name.
138 static base::string16
GetChromiumModelIdForProfile(
139 const base::FilePath
& profile_path
);
141 // Get the AppUserModelId for the App List, for the profile in |profile_path|.
142 static base::string16
GetAppListAppModelIdForProfile(
143 const base::FilePath
& profile_path
);
145 // Migrates existing chrome shortcuts by tagging them with correct app id.
146 // see http://crbug.com/28104
147 static void MigrateChromiumShortcuts();
149 // Migrates all shortcuts in |path| which point to |chrome_exe| such that they
150 // have the appropriate AppUserModelId. Also makes sure those shortcuts have
151 // the dual_mode (ref. shell_util.h) property set if such is requested by
152 // |check_dual_mode|.
153 // Returns the number of shortcuts migrated.
154 // This method should not be called prior to Windows 7.
155 // This method is only public for the sake of tests and shouldn't be called
156 // externally otherwise.
157 static int MigrateShortcutsInPathInternal(const base::FilePath
& chrome_exe
,
158 const base::FilePath
& path
,
159 bool check_dual_mode
);
161 // Returns the path to the Start Menu shortcut for the given Chrome.
162 static base::FilePath
GetStartMenuShortcut(const base::FilePath
& chrome_exe
);
163 #endif // defined(OS_WIN)
166 // TODO(calamity): replace with
167 // BrowserDistribution::GetStartMenuShortcutSubfolder() once
168 // BrowserDistribution is cross-platform.
169 // Gets the name of the Chrome Apps menu folder in which to place app
170 // shortcuts. This is needed for Mac and Linux.
171 static base::string16
GetAppShortcutsSubdirName();
174 // The current default web client application UI state. This is used when
175 // querying if Chrome is the default browser or the default handler
176 // application for a url protocol, and communicates the state and result of
178 enum DefaultWebClientUIState
{
185 class DefaultWebClientObserver
{
187 virtual ~DefaultWebClientObserver() {}
188 // Updates the UI state to reflect the current default browser state.
189 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state
) = 0;
190 // Called to notify the UI of the immediate result of invoking
192 virtual void OnSetAsDefaultConcluded(bool succeeded
) {}
193 // Observer classes that return true to OwnedByWorker are automatically
194 // freed by the worker when they are no longer needed. False by default.
195 virtual bool IsOwnedByWorker();
196 // An observer can permit or decline set-as-default operation if it
197 // requires triggering user interaction. By default not allowed.
198 virtual bool IsInteractiveSetDefaultPermitted();
201 // Helper objects that handle checking if Chrome is the default browser
202 // or application for a url protocol on Windows and Linux, and also setting
203 // it as the default. These operations are performed asynchronously on the
204 // file thread since registry access (on Windows) or the preference database
205 // (on Linux) are involved and this can be slow.
206 class DefaultWebClientWorker
207 : public base::RefCountedThreadSafe
<DefaultWebClientWorker
> {
209 explicit DefaultWebClientWorker(DefaultWebClientObserver
* observer
);
211 // Checks to see if Chrome is the default web client application. The result
212 // will be passed back to the observer via the SetDefaultWebClientUIState
213 // function. If there is no observer, this function does not do anything.
214 void StartCheckIsDefault();
216 // Sets Chrome as the default web client application. If there is an
217 // observer, once the operation has completed the new default will be
218 // queried and the current status reported via SetDefaultWebClientUIState.
219 void StartSetAsDefault();
221 // Called to notify the worker that the view is gone.
222 void ObserverDestroyed();
225 friend class base::RefCountedThreadSafe
<DefaultWebClientWorker
>;
227 virtual ~DefaultWebClientWorker() {}
230 // Function that performs the check.
231 virtual DefaultWebClientState
CheckIsDefault() = 0;
233 // Function that sets Chrome as the default web client. Returns false if
234 // the operation fails or has been cancelled by the user.
235 virtual bool SetAsDefault(bool interactive_permitted
) = 0;
237 // Function that handles performing the check on the file thread. This
238 // function is posted as a task onto the file thread, where it performs
239 // the check. When the check has finished the CompleteCheckIsDefault
240 // function is posted to the UI thread, where the result is sent back to
242 void ExecuteCheckIsDefault();
244 // Function that handles setting Chrome as the default web client on the
245 // file thread. This function is posted as a task onto the file thread.
246 // Once it is finished the CompleteSetAsDefault function is posted to the
247 // UI thread which will check the status of Chrome as the default, and
248 // send this to the observer.
249 // |interactive_permitted| indicates if the routine is allowed to carry on
250 // in context where user interaction is required (CanSetAsDefault*
251 // returns SET_DEFAULT_INTERACTIVE).
252 void ExecuteSetAsDefault(bool interactive_permitted
);
254 // Communicate results to the observer. This function is posted as a task
255 // onto the UI thread by the ExecuteCheckIsDefault function running in the
257 void CompleteCheckIsDefault(DefaultWebClientState state
);
259 // When the action to set Chrome as the default has completed this function
260 // is run. It is posted as a task back onto the UI thread by the
261 // ExecuteSetAsDefault function running in the file thread. This function
262 // will the start the check process, which, if an observer is present,
263 // reports to it the new status.
264 // |succeeded| is true if the actual call to a set-default function (from
265 // ExecuteSetAsDefault) was successful.
266 void CompleteSetAsDefault(bool succeeded
);
268 // Updates the UI in our associated view with the current default web
270 void UpdateUI(DefaultWebClientState state
);
272 DefaultWebClientObserver
* observer_
;
274 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker
);
277 // Worker for checking and setting the default browser.
278 class DefaultBrowserWorker
: public DefaultWebClientWorker
{
280 explicit DefaultBrowserWorker(DefaultWebClientObserver
* observer
);
283 ~DefaultBrowserWorker() override
{}
285 // Check if Chrome is the default browser.
286 DefaultWebClientState
CheckIsDefault() override
;
288 // Set Chrome as the default browser.
289 bool SetAsDefault(bool interactive_permitted
) override
;
291 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker
);
294 // Worker for checking and setting the default client application
295 // for a given protocol. A different worker instance is needed for each
296 // protocol you are interested in, so to check or set the default for
297 // multiple protocols you should use multiple worker objects.
298 class DefaultProtocolClientWorker
: public DefaultWebClientWorker
{
300 DefaultProtocolClientWorker(DefaultWebClientObserver
* observer
,
301 const std::string
& protocol
);
303 const std::string
& protocol() const { return protocol_
; }
306 ~DefaultProtocolClientWorker() override
{}
309 // Check is Chrome is the default handler for this protocol.
310 DefaultWebClientState
CheckIsDefault() override
;
312 // Set Chrome as the default handler for this protocol.
313 bool SetAsDefault(bool interactive_permitted
) override
;
315 std::string protocol_
;
317 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker
);
321 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_