Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/string16.h"
     14 #include "googleurl/src/gurl.h"
     15 #include "third_party/skia/include/core/SkBitmap.h"
     16 
     17 class CommandLine;
     18 class FilePath;
     19 class PrefService;
     20 
     21 #if defined(USE_X11)
     22 namespace base {
     23 class Environment;
     24 }
     25 #endif
     26 
     27 class ShellIntegration {
     28  public:
     29   // Sets Chrome as default browser (only for current user). Returns false if
     30   // this operation fails.
     31   static bool SetAsDefaultBrowser();
     32 
     33   // On Linux, it may not be possible to determine or set the default browser
     34   // on some desktop environments or configurations. So, we use this enum and
     35   // not a plain bool. (Note however that if used like a bool, this enum will
     36   // have reasonable behavior.)
     37   enum DefaultBrowserState {
     38     NOT_DEFAULT_BROWSER = 0,
     39     IS_DEFAULT_BROWSER,
     40     UNKNOWN_DEFAULT_BROWSER
     41   };
     42 
     43   // Attempt to determine if this instance of Chrome is the default browser and
     44   // return the appropriate state. (Defined as being the handler for HTTP/HTTPS
     45   // protocols; we don't want to report "no" here if the user has simply chosen
     46   // to open HTML files in a text editor and FTP links with an FTP client.)
     47   static DefaultBrowserState IsDefaultBrowser();
     48 
     49   // Returns true if Firefox is likely to be the default browser for the current
     50   // user. This method is very fast so it can be invoked in the UI thread.
     51   static bool IsFirefoxDefaultBrowser();
     52 
     53   struct ShortcutInfo {
     54     ShortcutInfo();
     55     ~ShortcutInfo();
     56 
     57     GURL url;
     58     // If |extension_id| is non-empty, this is short cut is to an extension-app
     59     // and the launch url will be detected at start-up. In this case, |url|
     60     // is still used to generate the app id (windows app id, not chrome app id).
     61     std::string extension_id;
     62     string16 title;
     63     string16 description;
     64     SkBitmap favicon;
     65 
     66     bool create_on_desktop;
     67     bool create_in_applications_menu;
     68 
     69     // For Windows, this refers to quick launch bar prior to Win7. In Win7,
     70     // this means "pin to taskbar". For Mac/Linux, this could be used for
     71     // Mac dock or the gnome/kde application launcher. However, those are not
     72     // implemented yet.
     73     bool create_in_quick_launch_bar;
     74   };
     75 
     76   // Set up command line arguments for launching a URL or an app.
     77   // The new command line reuses the current process's user data directory (and
     78   // login profile, for ChromeOS).
     79   // If |extension_app_id| is non-empty, the arguments use kAppId=<id>.
     80   // Otherwise, kApp=<url> is used.
     81   static CommandLine CommandLineArgsForLauncher(
     82       const GURL& url,
     83       const std::string& extension_app_id);
     84 
     85 #if defined(USE_X11)
     86   // Returns filename of the desktop shortcut used to launch the browser.
     87   static std::string GetDesktopName(base::Environment* env);
     88 
     89   static bool GetDesktopShortcutTemplate(base::Environment* env,
     90                                          std::string* output);
     91 
     92   // Returns filename for .desktop file based on |url|, sanitized for security.
     93   static FilePath GetDesktopShortcutFilename(const GURL& url);
     94 
     95   // Returns contents for .desktop file based on |template_contents|, |url|
     96   // and |title|. The |template_contents| should be contents of .desktop file
     97   // used to launch Chrome.
     98   static std::string GetDesktopFileContents(
     99       const std::string& template_contents,
    100       const std::string& app_name,
    101       const GURL& url,
    102       const std::string& extension_id,
    103       const string16& title,
    104       const std::string& icon_name);
    105 
    106   static void CreateDesktopShortcut(const ShortcutInfo& shortcut_info,
    107                                     const std::string& shortcut_template);
    108 #endif  // defined(USE_X11)
    109 
    110 #if defined(OS_WIN)
    111   // Generates Win7 app id for given app name and profile path. The returned app
    112   // id is in the format of "|app_name|[.<profile_id>]". "profile_id" is
    113   // appended when user override the default value.
    114   static std::wstring GetAppId(const std::wstring& app_name,
    115                                const FilePath& profile_path);
    116 
    117   // Generates Win7 app id for Chromium by calling GetAppId with
    118   // chrome::kBrowserAppID as app_name.
    119   static std::wstring GetChromiumAppId(const FilePath& profile_path);
    120 
    121   // Migrates existing chrome shortcuts by tagging them with correct app id.
    122   // see http://crbug.com/28104
    123   static void MigrateChromiumShortcuts();
    124 #endif  // defined(OS_WIN)
    125 
    126   // The current default browser UI state
    127   enum DefaultBrowserUIState {
    128     STATE_PROCESSING,
    129     STATE_NOT_DEFAULT,
    130     STATE_IS_DEFAULT,
    131     STATE_UNKNOWN
    132   };
    133 
    134   class DefaultBrowserObserver {
    135    public:
    136     // Updates the UI state to reflect the current default browser state.
    137     virtual void SetDefaultBrowserUIState(DefaultBrowserUIState state) = 0;
    138     virtual ~DefaultBrowserObserver() {}
    139   };
    140   //  A helper object that handles checking if Chrome is the default browser on
    141   //  Windows and also setting it as the default browser. These operations are
    142   //  performed asynchronously on the file thread since registry access is
    143   //  involved and this can be slow.
    144   //
    145   class DefaultBrowserWorker
    146       : public base::RefCountedThreadSafe<DefaultBrowserWorker> {
    147    public:
    148     explicit DefaultBrowserWorker(DefaultBrowserObserver* observer);
    149 
    150     // Checks if Chrome is the default browser.
    151     void StartCheckDefaultBrowser();
    152 
    153     // Sets Chrome as the default browser.
    154     void StartSetAsDefaultBrowser();
    155 
    156     // Called to notify the worker that the view is gone.
    157     void ObserverDestroyed();
    158 
    159    private:
    160     friend class base::RefCountedThreadSafe<DefaultBrowserWorker>;
    161 
    162     virtual ~DefaultBrowserWorker() {}
    163 
    164     // Functions that track the process of checking if Chrome is the default
    165     // browser.  |ExecuteCheckDefaultBrowser| checks the registry on the file
    166     // thread.  |CompleteCheckDefaultBrowser| notifies the view to update on the
    167     // UI thread.
    168     void ExecuteCheckDefaultBrowser();
    169     void CompleteCheckDefaultBrowser(DefaultBrowserState state);
    170 
    171     // Functions that track the process of setting Chrome as the default
    172     // browser.  |ExecuteSetAsDefaultBrowser| updates the registry on the file
    173     // thread.  |CompleteSetAsDefaultBrowser| notifies the view to update on the
    174     // UI thread.
    175     void ExecuteSetAsDefaultBrowser();
    176     void CompleteSetAsDefaultBrowser();
    177 
    178     // Updates the UI in our associated view with the current default browser
    179     // state.
    180     void UpdateUI(DefaultBrowserState state);
    181 
    182     DefaultBrowserObserver* observer_;
    183 
    184     DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker);
    185   };
    186 };
    187 
    188 #endif  // CHROME_BROWSER_SHELL_INTEGRATION_H_
    189