• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WEB_APPLICATIONS_WEB_APP_H_
6 #define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/strings/string16.h"
14 #include "build/build_config.h"
15 #include "chrome/browser/shell_integration.h"
16 #include "chrome/common/web_application_info.h"
17 #include "extensions/common/manifest_handlers/file_handler_info.h"
18 
19 class Profile;
20 
21 namespace content {
22 class WebContents;
23 }
24 
25 namespace extensions {
26 class Extension;
27 }
28 
29 namespace gfx {
30 class ImageFamily;
31 }
32 
33 // This namespace contains everything related to integrating Chrome apps into
34 // the OS. E.g. creating and updating shorcuts for apps, setting up file
35 // associations, etc.
36 namespace web_app {
37 
38 // Represents the info required to create a shortcut for an app.
39 struct ShortcutInfo {
40   ShortcutInfo();
41   ~ShortcutInfo();
42 
43   GURL url;
44   // If |extension_id| is non-empty, this is short cut is to an extension-app
45   // and the launch url will be detected at start-up. In this case, |url|
46   // is still used to generate the app id (windows app id, not chrome app id).
47   std::string extension_id;
48   bool is_platform_app;
49   base::string16 title;
50   base::string16 description;
51   base::FilePath extension_path;
52   gfx::ImageFamily favicon;
53   base::FilePath profile_path;
54   std::string profile_name;
55 };
56 
57 // This specifies a folder in the system applications menu (e.g the Start Menu
58 // on Windows).
59 //
60 // These represent the applications menu root, the "Google Chrome" folder and
61 // the "Chrome Apps" folder respectively.
62 //
63 // APP_MENU_LOCATION_HIDDEN specifies a shortcut that is used to register the
64 // app with the OS (in order to give its windows shelf icons, and correct icons
65 // and titles), but the app should not show up in menus or search results.
66 //
67 // NB: On Linux, these locations may not be used by the window manager (e.g
68 // Unity and Gnome Shell).
69 enum ApplicationsMenuLocation {
70   APP_MENU_LOCATION_NONE,
71   APP_MENU_LOCATION_ROOT,
72   APP_MENU_LOCATION_SUBDIR_CHROME,
73   APP_MENU_LOCATION_SUBDIR_CHROMEAPPS,
74   APP_MENU_LOCATION_HIDDEN,
75 };
76 
77 // Info about which locations to create app shortcuts in.
78 struct ShortcutLocations {
79   ShortcutLocations();
80 
81   bool on_desktop;
82 
83   ApplicationsMenuLocation applications_menu_location;
84 
85   // For Windows, this refers to quick launch bar prior to Win7. In Win7,
86   // this means "pin to taskbar". For Mac/Linux, this could be used for
87   // Mac dock or the gnome/kde application launcher. However, those are not
88   // implemented yet.
89   bool in_quick_launch_bar;
90 };
91 
92 // This encodes the cause of shortcut creation as the correct behavior in each
93 // case is implementation specific.
94 enum ShortcutCreationReason {
95   SHORTCUT_CREATION_BY_USER,
96   SHORTCUT_CREATION_AUTOMATED,
97 };
98 
99 // Called by GetInfoForApp after fetching the ShortcutInfo and FileHandlersInfo.
100 typedef base::Callback<void(const ShortcutInfo&,
101                             const extensions::FileHandlersInfo&)> InfoCallback;
102 
103 // Called by GetShortcutInfoForApp after fetching the ShortcutInfo.
104 typedef base::Callback<void(const ShortcutInfo&)> ShortcutInfoCallback;
105 
106 #if defined(TOOLKIT_VIEWS)
107 // Extracts shortcut info of the given WebContents.
108 void GetShortcutInfoForTab(content::WebContents* web_contents,
109                            ShortcutInfo* info);
110 #endif
111 
112 // Updates web app shortcut of the WebContents. This function checks and
113 // updates web app icon and shortcuts if needed. For icon, the check is based
114 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu
115 // and quick launch (as well as pinned shortcut) for shortcut and only
116 // updates (recreates) them if they exits.
117 void UpdateShortcutForTabContents(content::WebContents* web_contents);
118 
119 ShortcutInfo ShortcutInfoForExtensionAndProfile(
120     const extensions::Extension* app,
121     Profile* profile);
122 
123 // Populates a ShortcutInfo and FileHandlersInfo for the given |extension| in
124 // |profile| and passes them to |callback| after asynchronously loading all icon
125 // representations.
126 void GetInfoForApp(const extensions::Extension* extension,
127                    Profile* profile,
128                    const InfoCallback& callback);
129 
130 // Populates a ShortcutInfo for the given |extension| in |profile| and passes
131 // it to |callback| after asynchronously loading all icon representations. This
132 // is equivalent to GetInfoForApp, but it throws away the FileHandlersInfo.
133 void GetShortcutInfoForApp(const extensions::Extension* extension,
134                            Profile* profile,
135                            const ShortcutInfoCallback& callback);
136 
137 // Whether to create a shortcut for this type of extension.
138 bool ShouldCreateShortcutFor(Profile* profile,
139                              const extensions::Extension* extension);
140 
141 // Gets the user data directory for given web app. The path for the directory is
142 // based on |extension_id|. If |extension_id| is empty then |url| is used
143 // to construct a unique ID.
144 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
145                                       const std::string& extension_id,
146                                       const GURL& url);
147 
148 // Gets the user data directory to use for |extension| located inside
149 // |profile_path|.
150 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
151                                       const extensions::Extension& extension);
152 
153 // Compute a deterministic name based on data in the shortcut_info.
154 std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info);
155 
156 // Compute a deterministic name based on the URL. We use this pseudo name
157 // as a key to store window location per application URLs in Browser and
158 // as app id for BrowserWindow, shortcut and jump list.
159 std::string GenerateApplicationNameFromURL(const GURL& url);
160 
161 // Compute a deterministic name based on an extension/apps's id.
162 std::string GenerateApplicationNameFromExtensionId(const std::string& id);
163 
164 // Extracts the extension id from the app name.
165 std::string GetExtensionIdFromApplicationName(const std::string& app_name);
166 
167 // Create shortcuts for web application based on given shortcut data.
168 // |shortcut_info| contains information about the shortcuts to create, and
169 // |locations| contains information about where to create them, while
170 // |file_handlers_info| contains information about the file handlers to create.
171 void CreateShortcutsWithInfo(
172     ShortcutCreationReason reason,
173     const ShortcutLocations& locations,
174     const ShortcutInfo& shortcut_info,
175     const extensions::FileHandlersInfo& file_handlers_info);
176 
177 // Creates shortcuts for an app. This loads the app's icon from disk, and calls
178 // CreateShortcutsWithInfo(). If you already have a ShortcutInfo with the app's
179 // icon loaded, you should use CreateShortcutsWithInfo() directly.
180 void CreateShortcuts(ShortcutCreationReason reason,
181                      const ShortcutLocations& locations,
182                      Profile* profile,
183                      const extensions::Extension* app);
184 
185 // Delete all shortcuts that have been created for the given profile and
186 // extension.
187 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app);
188 
189 // Updates shortcuts for web application based on given shortcut data. This
190 // refreshes existing shortcuts and their icons, but does not create new ones.
191 // |old_app_title| contains the title of the app prior to this update.
192 void UpdateAllShortcuts(const base::string16& old_app_title,
193                         Profile* profile,
194                         const extensions::Extension* app);
195 
196 // Updates shortcuts for all apps in this profile. This is expected to be called
197 // on the UI thread.
198 void UpdateShortcutsForAllApps(Profile* profile,
199                                const base::Closure& callback);
200 
201 // Returns true if given url is a valid web app url.
202 bool IsValidUrl(const GURL& url);
203 
204 #if defined(TOOLKIT_VIEWS)
205 // Extracts icons info from web app data. Take only square shaped icons and
206 // sort them from smallest to largest.
207 typedef std::vector<WebApplicationInfo::IconInfo> IconInfoList;
208 void GetIconsInfo(const WebApplicationInfo& app_info, IconInfoList* icons);
209 #endif
210 
211 #if defined(OS_LINUX)
212 // Windows that correspond to web apps need to have a deterministic (and
213 // different) WMClass than normal chrome windows so the window manager groups
214 // them as a separate application.
215 std::string GetWMClassFromAppName(std::string app_name);
216 #endif
217 
218 namespace internals {
219 
220 #if defined(OS_WIN)
221 // Returns the Windows user-level shortcut paths that are specified in
222 // |creation_locations|.
223 std::vector<base::FilePath> GetShortcutPaths(
224     const ShortcutLocations& creation_locations);
225 #endif
226 
227 // Implemented for each platform, does the platform specific parts of creating
228 // shortcuts. Used internally by CreateShortcuts methods.
229 // |shortcut_data_path| is where to store any resources created for the
230 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
231 // |shortcut_info| contains info about the shortcut to create, and
232 // |creation_locations| contains information about where to create them.
233 bool CreatePlatformShortcuts(
234     const base::FilePath& shortcut_data_path,
235     const ShortcutInfo& shortcut_info,
236     const extensions::FileHandlersInfo& file_handlers_info,
237     const ShortcutLocations& creation_locations,
238     ShortcutCreationReason creation_reason);
239 
240 // Delete all the shortcuts we have added for this extension. This is the
241 // platform specific implementation of the DeleteAllShortcuts function, and
242 // is executed on the FILE thread.
243 void DeletePlatformShortcuts(const base::FilePath& shortcut_data_path,
244                              const ShortcutInfo& shortcut_info);
245 
246 // Updates all the shortcuts we have added for this extension. This is the
247 // platform specific implementation of the UpdateAllShortcuts function, and
248 // is executed on the FILE thread.
249 void UpdatePlatformShortcuts(
250     const base::FilePath& shortcut_data_path,
251     const base::string16& old_app_title,
252     const ShortcutInfo& shortcut_info,
253     const extensions::FileHandlersInfo& file_handlers_info);
254 
255 // Delete all the shortcuts for an entire profile.
256 // This is executed on the FILE thread.
257 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path);
258 
259 // Sanitizes |name| and returns a version of it that is safe to use as an
260 // on-disk file name .
261 base::FilePath GetSanitizedFileName(const base::string16& name);
262 
263 }  // namespace internals
264 
265 }  // namespace web_app
266 
267 #endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
268