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 a string representing the application to be launched given the
65 // protocol of the requested url. This string may be a name or a path, but
66 // neither is guaranteed and it should only be used as a display string.
67 // Returns an empty string on failure.
68 static base::string16
GetApplicationNameForProtocol(const GURL
& url
);
70 // On Linux, it may not be possible to determine or set the default browser
71 // on some desktop environments or configurations. So, we use this enum and
73 enum DefaultWebClientState
{
80 // Attempt to determine if this instance of Chrome is the default browser and
81 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS
82 // protocols; we don't want to report "no" here if the user has simply chosen
83 // to open HTML files in a text editor and FTP links with an FTP client.)
84 static DefaultWebClientState
GetDefaultBrowser();
86 // Returns true if Firefox is likely to be the default browser for the current
87 // user. This method is very fast so it can be invoked in the UI thread.
88 static bool IsFirefoxDefaultBrowser();
90 // Attempt to determine if this instance of Chrome is the default client
91 // application for the given protocol and return the appropriate state.
92 static DefaultWebClientState
93 IsDefaultProtocolClient(const std::string
& protocol
);
95 // Data that needs to be passed between the app launcher stub and Chrome.
98 static void SetAppModeInfo(const AppModeInfo
* info
);
99 static const AppModeInfo
* AppModeInfo();
101 // Is the current instance of Chrome running in App mode.
102 bool IsRunningInAppMode();
104 // Set up command line arguments for launching a URL or an app.
105 // The new command line reuses the current process's user data directory (and
106 // login profile, for ChromeOS).
107 // If |extension_app_id| is non-empty, the arguments use kAppId=<id>.
108 // Otherwise, kApp=<url> is used.
109 static base::CommandLine
CommandLineArgsForLauncher(
111 const std::string
& extension_app_id
,
112 const base::FilePath
& profile_path
);
114 // Append command line arguments for launching a new chrome.exe process
115 // based on the current process.
116 // The new command line reuses the current process's user data directory and
118 static void AppendProfileArgs(const base::FilePath
& profile_path
,
119 base::CommandLine
* command_line
);
122 // Generates an application user model ID (AppUserModelId) for a given app
123 // name and profile path. The returned app id is in the format of
124 // "|app_name|[.<profile_id>]". "profile_id" is appended when user override
125 // the default value.
126 // Note: If the app has an installation specific suffix (e.g. on user-level
127 // Chrome installs), |app_name| should already be suffixed, this method will
128 // then further suffix it with the profile id as described above.
129 static base::string16
GetAppModelIdForProfile(const base::string16
& app_name
,
130 const base::FilePath
& profile_path
);
132 // Generates an application user model ID (AppUserModelId) for Chromium by
133 // calling GetAppModelIdForProfile() with ShellUtil::GetAppId() as app_name.
134 static base::string16
GetChromiumModelIdForProfile(
135 const base::FilePath
& profile_path
);
137 // Get the AppUserModelId for the App List, for the profile in |profile_path|.
138 static base::string16
GetAppListAppModelIdForProfile(
139 const base::FilePath
& profile_path
);
141 // Migrates existing chrome shortcuts by tagging them with correct app id.
142 // see http://crbug.com/28104
143 static void MigrateChromiumShortcuts();
145 // Migrates all shortcuts in |path| which point to |chrome_exe| such that they
146 // have the appropriate AppUserModelId. Also makes sure those shortcuts have
147 // the dual_mode (ref. shell_util.h) property set if such is requested by
148 // |check_dual_mode|.
149 // Returns the number of shortcuts migrated.
150 // This method should not be called prior to Windows 7.
151 // This method is only public for the sake of tests and shouldn't be called
152 // externally otherwise.
153 static int MigrateShortcutsInPathInternal(const base::FilePath
& chrome_exe
,
154 const base::FilePath
& path
,
155 bool check_dual_mode
);
157 // Returns the path to the Start Menu shortcut for the given Chrome.
158 static base::FilePath
GetStartMenuShortcut(const base::FilePath
& chrome_exe
);
159 #endif // defined(OS_WIN)
162 // TODO(calamity): replace with
163 // BrowserDistribution::GetStartMenuShortcutSubfolder() once
164 // BrowserDistribution is cross-platform.
165 // Gets the name of the Chrome Apps menu folder in which to place app
166 // shortcuts. This is needed for Mac and Linux.
167 static base::string16
GetAppShortcutsSubdirName();
170 // The current default web client application UI state. This is used when
171 // querying if Chrome is the default browser or the default handler
172 // application for a url protocol, and communicates the state and result of
174 enum DefaultWebClientUIState
{
181 class DefaultWebClientObserver
{
183 virtual ~DefaultWebClientObserver() {}
184 // Updates the UI state to reflect the current default browser state.
185 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state
) = 0;
186 // Called to notify the UI of the immediate result of invoking
188 virtual void OnSetAsDefaultConcluded(bool succeeded
) {}
189 // Observer classes that return true to OwnedByWorker are automatically
190 // freed by the worker when they are no longer needed. False by default.
191 virtual bool IsOwnedByWorker();
192 // An observer can permit or decline set-as-default operation if it
193 // requires triggering user interaction. By default not allowed.
194 virtual bool IsInteractiveSetDefaultPermitted();
197 // Helper objects that handle checking if Chrome is the default browser
198 // or application for a url protocol on Windows and Linux, and also setting
199 // it as the default. These operations are performed asynchronously on the
200 // file thread since registry access (on Windows) or the preference database
201 // (on Linux) are involved and this can be slow.
202 class DefaultWebClientWorker
203 : public base::RefCountedThreadSafe
<DefaultWebClientWorker
> {
205 explicit DefaultWebClientWorker(DefaultWebClientObserver
* observer
);
207 // Checks to see if Chrome is the default web client application. The result
208 // will be passed back to the observer via the SetDefaultWebClientUIState
209 // function. If there is no observer, this function does not do anything.
210 void StartCheckIsDefault();
212 // Sets Chrome as the default web client application. If there is an
213 // observer, once the operation has completed the new default will be
214 // queried and the current status reported via SetDefaultWebClientUIState.
215 void StartSetAsDefault();
217 // Called to notify the worker that the view is gone.
218 void ObserverDestroyed();
221 friend class base::RefCountedThreadSafe
<DefaultWebClientWorker
>;
223 virtual ~DefaultWebClientWorker() {}
226 // Function that performs the check.
227 virtual DefaultWebClientState
CheckIsDefault() = 0;
229 // Function that sets Chrome as the default web client. Returns false if
230 // the operation fails or has been cancelled by the user.
231 virtual bool SetAsDefault(bool interactive_permitted
) = 0;
233 // Function that handles performing the check on the file thread. This
234 // function is posted as a task onto the file thread, where it performs
235 // the check. When the check has finished the CompleteCheckIsDefault
236 // function is posted to the UI thread, where the result is sent back to
238 void ExecuteCheckIsDefault();
240 // Function that handles setting Chrome as the default web client on the
241 // file thread. This function is posted as a task onto the file thread.
242 // Once it is finished the CompleteSetAsDefault function is posted to the
243 // UI thread which will check the status of Chrome as the default, and
244 // send this to the observer.
245 // |interactive_permitted| indicates if the routine is allowed to carry on
246 // in context where user interaction is required (CanSetAsDefault*
247 // returns SET_DEFAULT_INTERACTIVE).
248 void ExecuteSetAsDefault(bool interactive_permitted
);
250 // Communicate results to the observer. This function is posted as a task
251 // onto the UI thread by the ExecuteCheckIsDefault function running in the
253 void CompleteCheckIsDefault(DefaultWebClientState state
);
255 // When the action to set Chrome as the default has completed this function
256 // is run. It is posted as a task back onto the UI thread by the
257 // ExecuteSetAsDefault function running in the file thread. This function
258 // will the start the check process, which, if an observer is present,
259 // reports to it the new status.
260 // |succeeded| is true if the actual call to a set-default function (from
261 // ExecuteSetAsDefault) was successful.
262 void CompleteSetAsDefault(bool succeeded
);
264 // Updates the UI in our associated view with the current default web
266 void UpdateUI(DefaultWebClientState state
);
268 DefaultWebClientObserver
* observer_
;
270 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker
);
273 // Worker for checking and setting the default browser.
274 class DefaultBrowserWorker
: public DefaultWebClientWorker
{
276 explicit DefaultBrowserWorker(DefaultWebClientObserver
* observer
);
279 ~DefaultBrowserWorker() override
{}
281 // Check if Chrome is the default browser.
282 DefaultWebClientState
CheckIsDefault() override
;
284 // Set Chrome as the default browser.
285 bool SetAsDefault(bool interactive_permitted
) override
;
287 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker
);
290 // Worker for checking and setting the default client application
291 // for a given protocol. A different worker instance is needed for each
292 // protocol you are interested in, so to check or set the default for
293 // multiple protocols you should use multiple worker objects.
294 class DefaultProtocolClientWorker
: public DefaultWebClientWorker
{
296 DefaultProtocolClientWorker(DefaultWebClientObserver
* observer
,
297 const std::string
& protocol
);
299 const std::string
& protocol() const { return protocol_
; }
302 ~DefaultProtocolClientWorker() override
{}
305 // Check is Chrome is the default handler for this protocol.
306 DefaultWebClientState
CheckIsDefault() override
;
308 // Set Chrome as the default handler for this protocol.
309 bool SetAsDefault(bool interactive_permitted
) override
;
311 std::string protocol_
;
313 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker
);
317 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_