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