| OLD | NEW |
| (Empty) |
| 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/string16.h" | |
| 14 #include "googleurl/src/gurl.h" | |
| 15 #include "ui/gfx/image/image.h" | |
| 16 | |
| 17 class CommandLine; | |
| 18 | |
| 19 class ShellIntegration { | |
| 20 public: | |
| 21 // Sets Chrome as the default browser (only for the current user). Returns | |
| 22 // false if this operation fails. | |
| 23 static bool SetAsDefaultBrowser(); | |
| 24 | |
| 25 // Initiates an OS shell flow which (if followed by the user) should set | |
| 26 // Chrome as the default browser. Returns false if the flow cannot be | |
| 27 // initialized, if it is not supported (introduced for Windows 8) or if the | |
| 28 // user cancels the operation. This is a blocking call and requires a FILE | |
| 29 // thread. If Chrome is already default browser, no interactive dialog will be | |
| 30 // shown and this method returns true. | |
| 31 static bool SetAsDefaultBrowserInteractive(); | |
| 32 | |
| 33 // Sets Chrome as the default client application for the given protocol | |
| 34 // (only for the current user). Returns false if this operation fails. | |
| 35 static bool SetAsDefaultProtocolClient(const std::string& protocol); | |
| 36 | |
| 37 // Initiates an OS shell flow which (if followed by the user) should set | |
| 38 // Chrome as the default handler for |protocol|. Returns false if the flow | |
| 39 // cannot be initialized, if it is not supported (introduced for Windows 8) | |
| 40 // or if the user cancels the operation. This is a blocking call and requires | |
| 41 // a FILE thread. If Chrome is already default for |protocol|, no interactive | |
| 42 // dialog will be shown and this method returns true. | |
| 43 static bool SetAsDefaultProtocolClientInteractive( | |
| 44 const std::string& protocol); | |
| 45 | |
| 46 // In Windows 8 a browser can be made default-in-metro only in an interactive | |
| 47 // flow. We will distinguish between two types of permissions here to avoid | |
| 48 // forcing the user into UI interaction when this should not be done. | |
| 49 enum DefaultWebClientSetPermission { | |
| 50 SET_DEFAULT_NOT_ALLOWED, | |
| 51 SET_DEFAULT_UNATTENDED, | |
| 52 SET_DEFAULT_INTERACTIVE, | |
| 53 }; | |
| 54 | |
| 55 // Returns requirements for making the running browser the user's default. | |
| 56 static DefaultWebClientSetPermission CanSetAsDefaultBrowser(); | |
| 57 | |
| 58 // Returns requirements for making the running browser the user's default | |
| 59 // client application for specific protocols. | |
| 60 static DefaultWebClientSetPermission CanSetAsDefaultProtocolClient(); | |
| 61 | |
| 62 // Returns the path of the application to be launched given the protocol | |
| 63 // of the requested url. Returns an empty string on failure. | |
| 64 static std::string GetApplicationForProtocol(const GURL& url); | |
| 65 | |
| 66 // On Linux, it may not be possible to determine or set the default browser | |
| 67 // on some desktop environments or configurations. So, we use this enum and | |
| 68 // not a plain bool. | |
| 69 enum DefaultWebClientState { | |
| 70 NOT_DEFAULT, | |
| 71 IS_DEFAULT, | |
| 72 UNKNOWN_DEFAULT, | |
| 73 NUM_DEFAULT_STATES | |
| 74 }; | |
| 75 | |
| 76 // Attempt to determine if this instance of Chrome is the default browser and | |
| 77 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS | |
| 78 // protocols; we don't want to report "no" here if the user has simply chosen | |
| 79 // to open HTML files in a text editor and FTP links with an FTP client.) | |
| 80 static DefaultWebClientState GetDefaultBrowser(); | |
| 81 | |
| 82 // Returns true if Firefox is likely to be the default browser for the current | |
| 83 // user. This method is very fast so it can be invoked in the UI thread. | |
| 84 static bool IsFirefoxDefaultBrowser(); | |
| 85 | |
| 86 // Attempt to determine if this instance of Chrome is the default client | |
| 87 // application for the given protocol and return the appropriate state. | |
| 88 static DefaultWebClientState | |
| 89 IsDefaultProtocolClient(const std::string& protocol); | |
| 90 | |
| 91 struct ShortcutInfo { | |
| 92 ShortcutInfo(); | |
| 93 ~ShortcutInfo(); | |
| 94 | |
| 95 GURL url; | |
| 96 // If |extension_id| is non-empty, this is short cut is to an extension-app | |
| 97 // and the launch url will be detected at start-up. In this case, |url| | |
| 98 // is still used to generate the app id (windows app id, not chrome app id). | |
| 99 std::string extension_id; | |
| 100 bool is_platform_app; | |
| 101 string16 title; | |
| 102 string16 description; | |
| 103 base::FilePath extension_path; | |
| 104 gfx::Image favicon; | |
| 105 base::FilePath profile_path; | |
| 106 | |
| 107 bool create_on_desktop; | |
| 108 bool create_in_applications_menu; | |
| 109 | |
| 110 // For Windows, this refers to quick launch bar prior to Win7. In Win7, | |
| 111 // this means "pin to taskbar". For Mac/Linux, this could be used for | |
| 112 // Mac dock or the gnome/kde application launcher. However, those are not | |
| 113 // implemented yet. | |
| 114 bool create_in_quick_launch_bar; | |
| 115 }; | |
| 116 | |
| 117 // Data that needs to be passed between the app launcher stub and Chrome. | |
| 118 struct AppModeInfo { | |
| 119 }; | |
| 120 static void SetAppModeInfo(const AppModeInfo* info); | |
| 121 static const AppModeInfo* AppModeInfo(); | |
| 122 | |
| 123 // Is the current instance of Chrome running in App mode. | |
| 124 bool IsRunningInAppMode(); | |
| 125 | |
| 126 // Set up command line arguments for launching a URL or an app. | |
| 127 // The new command line reuses the current process's user data directory (and | |
| 128 // login profile, for ChromeOS). | |
| 129 // If |extension_app_id| is non-empty, the arguments use kAppId=<id>. | |
| 130 // Otherwise, kApp=<url> is used. | |
| 131 static CommandLine CommandLineArgsForLauncher( | |
| 132 const GURL& url, | |
| 133 const std::string& extension_app_id, | |
| 134 const base::FilePath& profile_path); | |
| 135 | |
| 136 #if defined(OS_WIN) | |
| 137 // Generates an application user model ID (AppUserModelId) for a given app | |
| 138 // name and profile path. The returned app id is in the format of | |
| 139 // "|app_name|[.<profile_id>]". "profile_id" is appended when user override | |
| 140 // the default value. | |
| 141 // Note: If the app has an installation specific suffix (e.g. on user-level | |
| 142 // Chrome installs), |app_name| should already be suffixed, this method will | |
| 143 // then further suffix it with the profile id as described above. | |
| 144 static string16 GetAppModelIdForProfile(const string16& app_name, | |
| 145 const base::FilePath& profile_path); | |
| 146 | |
| 147 // Generates an application user model ID (AppUserModelId) for Chromium by | |
| 148 // calling GetAppModelIdForProfile() with ShellUtil::GetAppId() as app_name. | |
| 149 static string16 GetChromiumModelIdForProfile( | |
| 150 const base::FilePath& profile_path); | |
| 151 | |
| 152 // Get the AppUserModelId for the App List, for the profile in |profile_path|. | |
| 153 static string16 GetAppListAppModelIdForProfile( | |
| 154 const base::FilePath& profile_path); | |
| 155 | |
| 156 // Returns the location (path and index) of the Chromium icon, (e.g., | |
| 157 // "C:\path\to\chrome.exe,0"). This is used to specify the icon to use | |
| 158 // for the taskbar group on Win 7. | |
| 159 static string16 GetChromiumIconLocation(); | |
| 160 | |
| 161 // Migrates existing chrome shortcuts by tagging them with correct app id. | |
| 162 // see http://crbug.com/28104 | |
| 163 static void MigrateChromiumShortcuts(); | |
| 164 | |
| 165 // Migrates all shortcuts in |path| which point to |chrome_exe| such that they | |
| 166 // have the appropriate AppUserModelId. Also makes sure those shortcuts have | |
| 167 // the dual_mode property set if such is requested by |check_dual_mode|. | |
| 168 // Returns the number of shortcuts migrated. | |
| 169 // This method should not be called prior to Windows 7. | |
| 170 // This method is only public for the sake of tests and shouldn't be called | |
| 171 // externally otherwise. | |
| 172 static int MigrateShortcutsInPathInternal(const base::FilePath& chrome_exe, | |
| 173 const base::FilePath& path, | |
| 174 bool check_dual_mode); | |
| 175 | |
| 176 // Returns the path to the Start Menu shortcut for the given Chrome. | |
| 177 static base::FilePath GetStartMenuShortcut(const base::FilePath& chrome_exe); | |
| 178 #endif // defined(OS_WIN) | |
| 179 | |
| 180 // The current default web client application UI state. This is used when | |
| 181 // querying if Chrome is the default browser or the default handler | |
| 182 // application for a url protocol, and communicates the state and result of | |
| 183 // a request. | |
| 184 enum DefaultWebClientUIState { | |
| 185 STATE_PROCESSING, | |
| 186 STATE_NOT_DEFAULT, | |
| 187 STATE_IS_DEFAULT, | |
| 188 STATE_UNKNOWN | |
| 189 }; | |
| 190 | |
| 191 class DefaultWebClientObserver { | |
| 192 public: | |
| 193 virtual ~DefaultWebClientObserver() {} | |
| 194 // Updates the UI state to reflect the current default browser state. | |
| 195 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state) = 0; | |
| 196 // Called to notify the UI of the immediate result of invoking | |
| 197 // SetAsDefault. | |
| 198 virtual void OnSetAsDefaultConcluded(bool succeeded) {} | |
| 199 // Observer classes that return true to OwnedByWorker are automatically | |
| 200 // freed by the worker when they are no longer needed. False by default. | |
| 201 virtual bool IsOwnedByWorker(); | |
| 202 // An observer can permit or decline set-as-default operation if it | |
| 203 // requires triggering user interaction. By default not allowed. | |
| 204 virtual bool IsInteractiveSetDefaultPermitted(); | |
| 205 }; | |
| 206 | |
| 207 // Helper objects that handle checking if Chrome is the default browser | |
| 208 // or application for a url protocol on Windows and Linux, and also setting | |
| 209 // it as the default. These operations are performed asynchronously on the | |
| 210 // file thread since registry access (on Windows) or the preference database | |
| 211 // (on Linux) are involved and this can be slow. | |
| 212 class DefaultWebClientWorker | |
| 213 : public base::RefCountedThreadSafe<DefaultWebClientWorker> { | |
| 214 public: | |
| 215 explicit DefaultWebClientWorker(DefaultWebClientObserver* observer); | |
| 216 | |
| 217 // Checks to see if Chrome is the default web client application. The result | |
| 218 // will be passed back to the observer via the SetDefaultWebClientUIState | |
| 219 // function. If there is no observer, this function does not do anything. | |
| 220 void StartCheckIsDefault(); | |
| 221 | |
| 222 // Sets Chrome as the default web client application. If there is an | |
| 223 // observer, once the operation has completed the new default will be | |
| 224 // queried and the current status reported via SetDefaultWebClientUIState. | |
| 225 void StartSetAsDefault(); | |
| 226 | |
| 227 // Called to notify the worker that the view is gone. | |
| 228 void ObserverDestroyed(); | |
| 229 | |
| 230 protected: | |
| 231 friend class base::RefCountedThreadSafe<DefaultWebClientWorker>; | |
| 232 | |
| 233 virtual ~DefaultWebClientWorker() {} | |
| 234 | |
| 235 private: | |
| 236 // Function that performs the check. | |
| 237 virtual DefaultWebClientState CheckIsDefault() = 0; | |
| 238 | |
| 239 // Function that sets Chrome as the default web client. Returns false if | |
| 240 // the operation fails or has been cancelled by the user. | |
| 241 virtual bool SetAsDefault(bool interactive_permitted) = 0; | |
| 242 | |
| 243 // Function that handles performing the check on the file thread. This | |
| 244 // function is posted as a task onto the file thread, where it performs | |
| 245 // the check. When the check has finished the CompleteCheckIsDefault | |
| 246 // function is posted to the UI thread, where the result is sent back to | |
| 247 // the observer. | |
| 248 void ExecuteCheckIsDefault(); | |
| 249 | |
| 250 // Function that handles setting Chrome as the default web client on the | |
| 251 // file thread. This function is posted as a task onto the file thread. | |
| 252 // Once it is finished the CompleteSetAsDefault function is posted to the | |
| 253 // UI thread which will check the status of Chrome as the default, and | |
| 254 // send this to the observer. | |
| 255 // |interactive_permitted| indicates if the routine is allowed to carry on | |
| 256 // in context where user interaction is required (CanSetAsDefault* | |
| 257 // returns SET_DEFAULT_INTERACTIVE). | |
| 258 void ExecuteSetAsDefault(bool interactive_permitted); | |
| 259 | |
| 260 // Communicate results to the observer. This function is posted as a task | |
| 261 // onto the UI thread by the ExecuteCheckIsDefault function running in the | |
| 262 // file thread. | |
| 263 void CompleteCheckIsDefault(DefaultWebClientState state); | |
| 264 | |
| 265 // When the action to set Chrome as the default has completed this function | |
| 266 // is run. It is posted as a task back onto the UI thread by the | |
| 267 // ExecuteSetAsDefault function running in the file thread. This function | |
| 268 // will the start the check process, which, if an observer is present, | |
| 269 // reports to it the new status. | |
| 270 // |succeeded| is true if the actual call to a set-default function (from | |
| 271 // ExecuteSetAsDefault) was successful. | |
| 272 void CompleteSetAsDefault(bool succeeded); | |
| 273 | |
| 274 // Updates the UI in our associated view with the current default web | |
| 275 // client state. | |
| 276 void UpdateUI(DefaultWebClientState state); | |
| 277 | |
| 278 DefaultWebClientObserver* observer_; | |
| 279 | |
| 280 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); | |
| 281 }; | |
| 282 | |
| 283 // Worker for checking and setting the default browser. | |
| 284 class DefaultBrowserWorker : public DefaultWebClientWorker { | |
| 285 public: | |
| 286 explicit DefaultBrowserWorker(DefaultWebClientObserver* observer); | |
| 287 | |
| 288 private: | |
| 289 virtual ~DefaultBrowserWorker() {} | |
| 290 | |
| 291 // Check if Chrome is the default browser. | |
| 292 virtual DefaultWebClientState CheckIsDefault() OVERRIDE; | |
| 293 | |
| 294 // Set Chrome as the default browser. | |
| 295 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE; | |
| 296 | |
| 297 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); | |
| 298 }; | |
| 299 | |
| 300 // Worker for checking and setting the default client application | |
| 301 // for a given protocol. A different worker instance is needed for each | |
| 302 // protocol you are interested in, so to check or set the default for | |
| 303 // multiple protocols you should use multiple worker objects. | |
| 304 class DefaultProtocolClientWorker : public DefaultWebClientWorker { | |
| 305 public: | |
| 306 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, | |
| 307 const std::string& protocol); | |
| 308 | |
| 309 const std::string& protocol() const { return protocol_; } | |
| 310 | |
| 311 protected: | |
| 312 virtual ~DefaultProtocolClientWorker() {} | |
| 313 | |
| 314 private: | |
| 315 // Check is Chrome is the default handler for this protocol. | |
| 316 virtual DefaultWebClientState CheckIsDefault() OVERRIDE; | |
| 317 | |
| 318 // Set Chrome as the default handler for this protocol. | |
| 319 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE; | |
| 320 | |
| 321 std::string protocol_; | |
| 322 | |
| 323 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); | |
| 324 }; | |
| 325 }; | |
| 326 | |
| 327 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ | |
| OLD | NEW |