shell_integration.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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#ifndef CHROME_BROWSER_SHELL_INTEGRATION_H_ 6#define CHROME_BROWSER_SHELL_INTEGRATION_H_ 7 8#include <string> 9 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" 15#include "url/gurl.h" 16 17namespace base { 18class CommandLine; 19} 20 21class ShellIntegration { 22 public: 23 // Sets Chrome as the default browser (only for the current user). Returns 24 // false if this operation fails. 25 static bool SetAsDefaultBrowser(); 26 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(); 34 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); 38 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); 47 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, 55 }; 56 57 // Returns requirements for making the running browser the user's default. 58 static DefaultWebClientSetPermission CanSetAsDefaultBrowser(); 59 60 // Returns requirements for making the running browser the user's default 61 // client application for specific protocols. 62 static DefaultWebClientSetPermission CanSetAsDefaultProtocolClient(); 63 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); 69 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 72 // not a plain bool. 73 enum DefaultWebClientState { 74 NOT_DEFAULT, 75 IS_DEFAULT, 76 UNKNOWN_DEFAULT, 77 NUM_DEFAULT_STATES 78 }; 79 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(); 85 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(); 89 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); 94 95 struct ShortcutInfo { 96 ShortcutInfo(); 97 ~ShortcutInfo(); 98 99 GURL url; 100 // If |extension_id| is non-empty, this is short cut is to an extension-app 101 // and the launch url will be detected at start-up. In this case, |url| 102 // is still used to generate the app id (windows app id, not chrome app id). 103 std::string extension_id; 104 bool is_platform_app; 105 base::string16 title; 106 base::string16 description; 107 base::FilePath extension_path; 108 gfx::ImageFamily favicon; 109 base::FilePath profile_path; 110 std::string profile_name; 111 }; 112 113 // This specifies a folder in the system applications menu (e.g the Start Menu 114 // on Windows). 115 // 116 // These represent the applications menu root, the "Google Chrome" folder and 117 // the "Chrome Apps" folder respectively. 118 // 119 // NB: On Linux, these locations may not be used by the window manager (e.g 120 // Unity and Gnome Shell). 121 enum ApplicationsMenuLocation { 122 APP_MENU_LOCATION_NONE, 123 APP_MENU_LOCATION_ROOT, 124 APP_MENU_LOCATION_SUBDIR_CHROME, 125 APP_MENU_LOCATION_SUBDIR_CHROMEAPPS, 126 }; 127 128 // Info about which locations to create app shortcuts in. 129 struct ShortcutLocations { 130 ShortcutLocations(); 131 132 bool on_desktop; 133 134 ApplicationsMenuLocation applications_menu_location; 135 136 // For Windows, this refers to quick launch bar prior to Win7. In Win7, 137 // this means "pin to taskbar". For Mac/Linux, this could be used for 138 // Mac dock or the gnome/kde application launcher. However, those are not 139 // implemented yet. 140 bool in_quick_launch_bar; 141 142#if defined(OS_POSIX) 143 // For Linux, this refers to a shortcut which the system knows about (for 144 // the purpose of identifying windows and giving them the correct 145 // title/icon), but which does not show up in menus or search results. 146 // Ignored if applications_menu_location is not APP_MENU_LOCATION_NONE. 147 bool hidden; 148#endif 149 }; 150 151 // Data that needs to be passed between the app launcher stub and Chrome. 152 struct AppModeInfo { 153 }; 154 static void SetAppModeInfo(const AppModeInfo* info); 155 static const AppModeInfo* AppModeInfo(); 156 157 // Is the current instance of Chrome running in App mode. 158 bool IsRunningInAppMode(); 159 160 // Set up command line arguments for launching a URL or an app. 161 // The new command line reuses the current process's user data directory (and 162 // login profile, for ChromeOS). 163 // If |extension_app_id| is non-empty, the arguments use kAppId=<id>. 164 // Otherwise, kApp=<url> is used. 165 static base::CommandLine CommandLineArgsForLauncher( 166 const GURL& url, 167 const std::string& extension_app_id, 168 const base::FilePath& profile_path); 169 170 // Append command line arguments for launching a new chrome.exe process 171 // based on the current process. 172 // The new command line reuses the current process's user data directory and 173 // profile. 174 static void AppendProfileArgs(const base::FilePath& profile_path, 175 base::CommandLine* command_line); 176 177#if defined(OS_WIN) 178 // Generates an application user model ID (AppUserModelId) for a given app 179 // name and profile path. The returned app id is in the format of 180 // "|app_name|[.<profile_id>]". "profile_id" is appended when user override 181 // the default value. 182 // Note: If the app has an installation specific suffix (e.g. on user-level 183 // Chrome installs), |app_name| should already be suffixed, this method will 184 // then further suffix it with the profile id as described above. 185 static base::string16 GetAppModelIdForProfile(const base::string16& app_name, 186 const base::FilePath& profile_path); 187 188 // Generates an application user model ID (AppUserModelId) for Chromium by 189 // calling GetAppModelIdForProfile() with ShellUtil::GetAppId() as app_name. 190 static base::string16 GetChromiumModelIdForProfile( 191 const base::FilePath& profile_path); 192 193 // Get the AppUserModelId for the App List, for the profile in |profile_path|. 194 static base::string16 GetAppListAppModelIdForProfile( 195 const base::FilePath& profile_path); 196 197 // Migrates existing chrome shortcuts by tagging them with correct app id. 198 // see http://crbug.com/28104 199 static void MigrateChromiumShortcuts(); 200 201 // Migrates all shortcuts in |path| which point to |chrome_exe| such that they 202 // have the appropriate AppUserModelId. Also makes sure those shortcuts have 203 // the dual_mode property set if such is requested by |check_dual_mode|. 204 // Returns the number of shortcuts migrated. 205 // This method should not be called prior to Windows 7. 206 // This method is only public for the sake of tests and shouldn't be called 207 // externally otherwise. 208 static int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe, 209 const base::FilePath& path, 210 bool check_dual_mode); 211 212 // Returns the path to the Start Menu shortcut for the given Chrome. 213 static base::FilePath GetStartMenuShortcut(const base::FilePath& chrome_exe); 214#endif // defined(OS_WIN) 215 216#if !defined(OS_WIN) 217 // TODO(calamity): replace with 218 // BrowserDistribution::GetStartMenuShortcutSubfolder() once 219 // BrowserDistribution is cross-platform. 220 // Gets the name of the Chrome Apps menu folder in which to place app 221 // shortcuts. This is needed for Mac and Linux. 222 static base::string16 GetAppShortcutsSubdirName(); 223#endif 224 225 // The current default web client application UI state. This is used when 226 // querying if Chrome is the default browser or the default handler 227 // application for a url protocol, and communicates the state and result of 228 // a request. 229 enum DefaultWebClientUIState { 230 STATE_PROCESSING, 231 STATE_NOT_DEFAULT, 232 STATE_IS_DEFAULT, 233 STATE_UNKNOWN 234 }; 235 236 class DefaultWebClientObserver { 237 public: 238 virtual ~DefaultWebClientObserver() {} 239 // Updates the UI state to reflect the current default browser state. 240 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state) = 0; 241 // Called to notify the UI of the immediate result of invoking 242 // SetAsDefault. 243 virtual void OnSetAsDefaultConcluded(bool succeeded) {} 244 // Observer classes that return true to OwnedByWorker are automatically 245 // freed by the worker when they are no longer needed. False by default. 246 virtual bool IsOwnedByWorker(); 247 // An observer can permit or decline set-as-default operation if it 248 // requires triggering user interaction. By default not allowed. 249 virtual bool IsInteractiveSetDefaultPermitted(); 250 }; 251 252 // Helper objects that handle checking if Chrome is the default browser 253 // or application for a url protocol on Windows and Linux, and also setting 254 // it as the default. These operations are performed asynchronously on the 255 // file thread since registry access (on Windows) or the preference database 256 // (on Linux) are involved and this can be slow. 257 class DefaultWebClientWorker 258 : public base::RefCountedThreadSafe<DefaultWebClientWorker> { 259 public: 260 explicit DefaultWebClientWorker(DefaultWebClientObserver* observer); 261 262 // Checks to see if Chrome is the default web client application. The result 263 // will be passed back to the observer via the SetDefaultWebClientUIState 264 // function. If there is no observer, this function does not do anything. 265 void StartCheckIsDefault(); 266 267 // Sets Chrome as the default web client application. If there is an 268 // observer, once the operation has completed the new default will be 269 // queried and the current status reported via SetDefaultWebClientUIState. 270 void StartSetAsDefault(); 271 272 // Called to notify the worker that the view is gone. 273 void ObserverDestroyed(); 274 275 protected: 276 friend class base::RefCountedThreadSafe<DefaultWebClientWorker>; 277 278 virtual ~DefaultWebClientWorker() {} 279 280 private: 281 // Function that performs the check. 282 virtual DefaultWebClientState CheckIsDefault() = 0; 283 284 // Function that sets Chrome as the default web client. Returns false if 285 // the operation fails or has been cancelled by the user. 286 virtual bool SetAsDefault(bool interactive_permitted) = 0; 287 288 // Function that handles performing the check on the file thread. This 289 // function is posted as a task onto the file thread, where it performs 290 // the check. When the check has finished the CompleteCheckIsDefault 291 // function is posted to the UI thread, where the result is sent back to 292 // the observer. 293 void ExecuteCheckIsDefault(); 294 295 // Function that handles setting Chrome as the default web client on the 296 // file thread. This function is posted as a task onto the file thread. 297 // Once it is finished the CompleteSetAsDefault function is posted to the 298 // UI thread which will check the status of Chrome as the default, and 299 // send this to the observer. 300 // |interactive_permitted| indicates if the routine is allowed to carry on 301 // in context where user interaction is required (CanSetAsDefault* 302 // returns SET_DEFAULT_INTERACTIVE). 303 void ExecuteSetAsDefault(bool interactive_permitted); 304 305 // Communicate results to the observer. This function is posted as a task 306 // onto the UI thread by the ExecuteCheckIsDefault function running in the 307 // file thread. 308 void CompleteCheckIsDefault(DefaultWebClientState state); 309 310 // When the action to set Chrome as the default has completed this function 311 // is run. It is posted as a task back onto the UI thread by the 312 // ExecuteSetAsDefault function running in the file thread. This function 313 // will the start the check process, which, if an observer is present, 314 // reports to it the new status. 315 // |succeeded| is true if the actual call to a set-default function (from 316 // ExecuteSetAsDefault) was successful. 317 void CompleteSetAsDefault(bool succeeded); 318 319 // Updates the UI in our associated view with the current default web 320 // client state. 321 void UpdateUI(DefaultWebClientState state); 322 323 DefaultWebClientObserver* observer_; 324 325 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); 326 }; 327 328 // Worker for checking and setting the default browser. 329 class DefaultBrowserWorker : public DefaultWebClientWorker { 330 public: 331 explicit DefaultBrowserWorker(DefaultWebClientObserver* observer); 332 333 private: 334 virtual ~DefaultBrowserWorker() {} 335 336 // Check if Chrome is the default browser. 337 virtual DefaultWebClientState CheckIsDefault() OVERRIDE; 338 339 // Set Chrome as the default browser. 340 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE; 341 342 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); 343 }; 344 345 // Worker for checking and setting the default client application 346 // for a given protocol. A different worker instance is needed for each 347 // protocol you are interested in, so to check or set the default for 348 // multiple protocols you should use multiple worker objects. 349 class DefaultProtocolClientWorker : public DefaultWebClientWorker { 350 public: 351 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, 352 const std::string& protocol); 353 354 const std::string& protocol() const { return protocol_; } 355 356 protected: 357 virtual ~DefaultProtocolClientWorker() {} 358 359 private: 360 // Check is Chrome is the default handler for this protocol. 361 virtual DefaultWebClientState CheckIsDefault() OVERRIDE; 362 363 // Set Chrome as the default handler for this protocol. 364 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE; 365 366 std::string protocol_; 367 368 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); 369 }; 370}; 371 372#endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ 373