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.
5 #ifndef CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
6 #define CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
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"
25 namespace extensions
{
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
38 // Represents the info required to create a shortcut for an app.
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
;
50 base::string16 description
;
51 base::FilePath extension_path
;
52 gfx::ImageFamily favicon
;
53 base::FilePath profile_path
;
54 std::string profile_name
;
57 // This specifies a folder in the system applications menu (e.g the Start Menu
60 // These represent the applications menu root, the "Google Chrome" folder and
61 // the "Chrome Apps" folder respectively.
63 // NB: On Linux, these locations may not be used by the window manager (e.g
64 // Unity and Gnome Shell).
65 enum ApplicationsMenuLocation
{
66 APP_MENU_LOCATION_NONE
,
67 APP_MENU_LOCATION_ROOT
,
68 APP_MENU_LOCATION_SUBDIR_CHROME
,
69 APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
,
72 // Info about which locations to create app shortcuts in.
73 struct ShortcutLocations
{
78 ApplicationsMenuLocation applications_menu_location
;
80 // For Windows, this refers to quick launch bar prior to Win7. In Win7,
81 // this means "pin to taskbar". For Mac/Linux, this could be used for
82 // Mac dock or the gnome/kde application launcher. However, those are not
84 bool in_quick_launch_bar
;
87 // For Linux, this refers to a shortcut which the system knows about (for
88 // the purpose of identifying windows and giving them the correct
89 // title/icon), but which does not show up in menus or search results.
90 // Ignored if applications_menu_location is not APP_MENU_LOCATION_NONE.
95 // This encodes the cause of shortcut creation as the correct behavior in each
96 // case is implementation specific.
97 enum ShortcutCreationReason
{
98 SHORTCUT_CREATION_BY_USER
,
99 SHORTCUT_CREATION_AUTOMATED
,
102 typedef base::Callback
<void(const ShortcutInfo
&)> ShortcutInfoCallback
;
104 // Extracts shortcut info of the given WebContents.
105 void GetShortcutInfoForTab(content::WebContents
* web_contents
,
108 // Updates web app shortcut of the WebContents. This function checks and
109 // updates web app icon and shortcuts if needed. For icon, the check is based
110 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu
111 // and quick launch (as well as pinned shortcut) for shortcut and only
112 // updates (recreates) them if they exits.
113 void UpdateShortcutForTabContents(content::WebContents
* web_contents
);
115 ShortcutInfo
ShortcutInfoForExtensionAndProfile(
116 const extensions::Extension
* app
,
119 // Fetches the icon for |extension| and calls |callback| with shortcut info
120 // filled out as by UpdateShortcutInfoForApp.
121 void UpdateShortcutInfoAndIconForApp(const extensions::Extension
* extension
,
123 const ShortcutInfoCallback
& callback
);
125 // Whether to create a shortcut for this type of extension.
126 bool ShouldCreateShortcutFor(Profile
* profile
,
127 const extensions::Extension
* extension
);
129 // Gets the user data directory for given web app. The path for the directory is
130 // based on |extension_id|. If |extension_id| is empty then |url| is used
131 // to construct a unique ID.
132 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
133 const std::string
& extension_id
,
136 // Gets the user data directory to use for |extension| located inside
138 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
139 const extensions::Extension
& extension
);
141 // Compute a deterministic name based on data in the shortcut_info.
142 std::string
GenerateApplicationNameFromInfo(const ShortcutInfo
& shortcut_info
);
144 // Compute a deterministic name based on the URL. We use this pseudo name
145 // as a key to store window location per application URLs in Browser and
146 // as app id for BrowserWindow, shortcut and jump list.
147 std::string
GenerateApplicationNameFromURL(const GURL
& url
);
149 // Compute a deterministic name based on an extension/apps's id.
150 std::string
GenerateApplicationNameFromExtensionId(const std::string
& id
);
152 // Extracts the extension id from the app name.
153 std::string
GetExtensionIdFromApplicationName(const std::string
& app_name
);
155 // Create shortcuts for web application based on given shortcut data.
156 // |shortcut_info| contains information about the shortcuts to create, and
157 // |creation_locations| contains information about where to create them.
158 void CreateShortcutsForShortcutInfo(ShortcutCreationReason reason
,
159 const ShortcutLocations
& locations
,
160 const ShortcutInfo
& shortcut_info
);
162 // Creates shortcuts for an app.
163 void CreateShortcuts(ShortcutCreationReason reason
,
164 const ShortcutLocations
& locations
,
166 const extensions::Extension
* app
);
168 // Delete all shortcuts that have been created for the given profile and
170 void DeleteAllShortcuts(Profile
* profile
, const extensions::Extension
* app
);
172 // Updates shortcuts for web application based on given shortcut data. This
173 // refreshes existing shortcuts and their icons, but does not create new ones.
174 // |old_app_title| contains the title of the app prior to this update.
175 void UpdateAllShortcuts(const base::string16
& old_app_title
,
177 const extensions::Extension
* app
);
179 // Updates shortcuts for all apps in this profile. This is expected to be called
181 void UpdateShortcutsForAllApps(Profile
* profile
,
182 const base::Closure
& callback
);
184 // Returns true if given url is a valid web app url.
185 bool IsValidUrl(const GURL
& url
);
187 #if defined(TOOLKIT_VIEWS)
188 // Extracts icons info from web app data. Take only square shaped icons and
189 // sort them from smallest to largest.
190 typedef std::vector
<WebApplicationInfo::IconInfo
> IconInfoList
;
191 void GetIconsInfo(const WebApplicationInfo
& app_info
, IconInfoList
* icons
);
194 #if defined(OS_LINUX)
195 // Windows that correspond to web apps need to have a deterministic (and
196 // different) WMClass than normal chrome windows so the window manager groups
197 // them as a separate application.
198 std::string
GetWMClassFromAppName(std::string app_name
);
201 namespace internals
{
204 // Returns the Windows user-level shortcut paths that are specified in
205 // |creation_locations|.
206 std::vector
<base::FilePath
> GetShortcutPaths(
207 const ShortcutLocations
& creation_locations
);
210 // Creates a shortcut. Must be called on the file thread. This is used to
211 // implement CreateShortcuts() above, and can also be used directly from the
212 // file thread. |shortcut_info| contains info about the shortcut to create, and
213 // |creation_locations| contains information about where to create them.
214 bool CreateShortcutsOnFileThread(ShortcutCreationReason reason
,
215 const ShortcutLocations
& locations
,
216 const ShortcutInfo
& shortcut_info
);
218 // Implemented for each platform, does the platform specific parts of creating
219 // shortcuts. Used internally by CreateShortcutsOnFileThread.
220 // |shortcut_data_path| is where to store any resources created for the
221 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
222 // |shortcut_info| contains info about the shortcut to create, and
223 // |creation_locations| contains information about where to create them.
224 bool CreatePlatformShortcuts(
225 const base::FilePath
& shortcut_data_path
,
226 const ShortcutInfo
& shortcut_info
,
227 const extensions::FileHandlersInfo
& file_handlers_info
,
228 const ShortcutLocations
& creation_locations
,
229 ShortcutCreationReason creation_reason
);
231 // Delete all the shortcuts we have added for this extension. This is the
232 // platform specific implementation of the DeleteAllShortcuts function, and
233 // is executed on the FILE thread.
234 void DeletePlatformShortcuts(const base::FilePath
& shortcut_data_path
,
235 const ShortcutInfo
& shortcut_info
);
237 // Updates all the shortcuts we have added for this extension. This is the
238 // platform specific implementation of the UpdateAllShortcuts function, and
239 // is executed on the FILE thread.
240 void UpdatePlatformShortcuts(
241 const base::FilePath
& shortcut_data_path
,
242 const base::string16
& old_app_title
,
243 const ShortcutInfo
& shortcut_info
,
244 const extensions::FileHandlersInfo
& file_handlers_info
);
246 // Delete all the shortcuts for an entire profile.
247 // This is executed on the FILE thread.
248 void DeleteAllShortcutsForProfile(const base::FilePath
& profile_path
);
250 // Sanitizes |name| and returns a version of it that is safe to use as an
251 // on-disk file name .
252 base::FilePath
GetSanitizedFileName(const base::string16
& name
);
254 } // namespace internals
256 } // namespace web_app
258 #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_