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/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "build/build_config.h"
16 #include "chrome/browser/shell_integration.h"
17 #include "chrome/common/web_application_info.h"
18 #include "extensions/common/manifest_handlers/file_handler_info.h"
26 namespace extensions
{
34 // This namespace contains everything related to integrating Chrome apps into
35 // the OS. E.g. creating and updating shorcuts for apps, setting up file
39 // Represents the info required to create a shortcut for an app.
45 // If |extension_id| is non-empty, this is short cut is to an extension-app
46 // and the launch url will be detected at start-up. In this case, |url|
47 // is still used to generate the app id (windows app id, not chrome app id).
48 std::string extension_id
;
51 base::string16 description
;
52 base::FilePath extension_path
;
53 gfx::ImageFamily favicon
;
54 base::FilePath profile_path
;
55 std::string profile_name
;
56 std::string version_for_display
;
59 // ShortcutInfo must not be copied; generally it is passed around via
60 // scoped_ptrs. This is to allow passing ShortcutInfos between threads,
61 // despite ImageFamily having a non-thread-safe reference count.
62 DISALLOW_COPY_AND_ASSIGN(ShortcutInfo
);
65 // This specifies a folder in the system applications menu (e.g the Start Menu
68 // These represent the applications menu root, the "Google Chrome" folder and
69 // the "Chrome Apps" folder respectively.
71 // APP_MENU_LOCATION_HIDDEN specifies a shortcut that is used to register the
72 // app with the OS (in order to give its windows shelf icons, and correct icons
73 // and titles), but the app should not show up in menus or search results.
75 // NB: On Linux, these locations may not be used by the window manager (e.g
76 // Unity and Gnome Shell).
77 enum ApplicationsMenuLocation
{
78 APP_MENU_LOCATION_NONE
,
79 APP_MENU_LOCATION_ROOT
,
80 APP_MENU_LOCATION_SUBDIR_CHROME
,
81 APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
,
82 APP_MENU_LOCATION_HIDDEN
,
85 // Info about which locations to create app shortcuts in.
86 struct ShortcutLocations
{
91 ApplicationsMenuLocation applications_menu_location
;
93 // For Windows, this refers to quick launch bar prior to Win7. In Win7,
94 // this means "pin to taskbar". For Mac/Linux, this could be used for
95 // Mac dock or the gnome/kde application launcher. However, those are not
97 bool in_quick_launch_bar
;
100 // This encodes the cause of shortcut creation as the correct behavior in each
101 // case is implementation specific.
102 enum ShortcutCreationReason
{
103 SHORTCUT_CREATION_BY_USER
,
104 SHORTCUT_CREATION_AUTOMATED
,
107 // Called by GetInfoForApp after fetching the ShortcutInfo and FileHandlersInfo.
108 typedef base::Callback
<void(scoped_ptr
<ShortcutInfo
>,
109 const extensions::FileHandlersInfo
&)> InfoCallback
;
111 // Called by GetShortcutInfoForApp after fetching the ShortcutInfo.
112 typedef base::Callback
<void(scoped_ptr
<ShortcutInfo
>)> ShortcutInfoCallback
;
114 #if defined(TOOLKIT_VIEWS)
115 // Extracts shortcut info of the given WebContents.
116 scoped_ptr
<ShortcutInfo
> GetShortcutInfoForTab(
117 content::WebContents
* web_contents
);
120 // Updates web app shortcut of the WebContents. This function checks and
121 // updates web app icon and shortcuts if needed. For icon, the check is based
122 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu
123 // and quick launch (as well as pinned shortcut) for shortcut and only
124 // updates (recreates) them if they exits.
125 void UpdateShortcutForTabContents(content::WebContents
* web_contents
);
127 scoped_ptr
<ShortcutInfo
> ShortcutInfoForExtensionAndProfile(
128 const extensions::Extension
* app
,
131 // Populates a ShortcutInfo and FileHandlersInfo for the given |extension| in
132 // |profile| and passes them to |callback| after asynchronously loading all icon
134 void GetInfoForApp(const extensions::Extension
* extension
,
136 const InfoCallback
& callback
);
138 // Populates a ShortcutInfo for the given |extension| in |profile| and passes
139 // it to |callback| after asynchronously loading all icon representations. This
140 // is equivalent to GetInfoForApp, but it throws away the FileHandlersInfo.
141 void GetShortcutInfoForApp(const extensions::Extension
* extension
,
143 const ShortcutInfoCallback
& callback
);
145 // Whether to create a shortcut for this type of extension.
146 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason
,
148 const extensions::Extension
* extension
);
150 // Gets the user data directory for given web app. The path for the directory is
151 // based on |extension_id|. If |extension_id| is empty then |url| is used
152 // to construct a unique ID.
153 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
154 const std::string
& extension_id
,
157 // Gets the user data directory to use for |extension| located inside
159 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
160 const extensions::Extension
& extension
);
162 // Compute a deterministic name based on data in the shortcut_info.
163 std::string
GenerateApplicationNameFromInfo(const ShortcutInfo
& shortcut_info
);
165 // Compute a deterministic name based on the URL. We use this pseudo name
166 // as a key to store window location per application URLs in Browser and
167 // as app id for BrowserWindow, shortcut and jump list.
168 std::string
GenerateApplicationNameFromURL(const GURL
& url
);
170 // Compute a deterministic name based on an extension/apps's id.
171 std::string
GenerateApplicationNameFromExtensionId(const std::string
& id
);
173 // Extracts the extension id from the app name.
174 std::string
GetExtensionIdFromApplicationName(const std::string
& app_name
);
176 // Create shortcuts for web application based on given shortcut data.
177 // |shortcut_info| contains information about the shortcuts to create, and
178 // |locations| contains information about where to create them, while
179 // |file_handlers_info| contains information about the file handlers to create.
180 void CreateShortcutsWithInfo(
181 ShortcutCreationReason reason
,
182 const ShortcutLocations
& locations
,
183 scoped_ptr
<ShortcutInfo
> shortcut_info
,
184 const extensions::FileHandlersInfo
& file_handlers_info
);
186 // Currently only called by app_list_service_mac to create a shim for the
188 void CreateNonAppShortcut(const ShortcutLocations
& locations
,
189 scoped_ptr
<ShortcutInfo
> shortcut_info
);
191 // Creates shortcuts for an app. This loads the app's icon from disk, and calls
192 // CreateShortcutsWithInfo(). If you already have a ShortcutInfo with the app's
193 // icon loaded, you should use CreateShortcutsWithInfo() directly.
194 void CreateShortcuts(ShortcutCreationReason reason
,
195 const ShortcutLocations
& locations
,
197 const extensions::Extension
* app
);
199 // Delete all shortcuts that have been created for the given profile and
201 void DeleteAllShortcuts(Profile
* profile
, const extensions::Extension
* app
);
203 // Updates shortcuts for web application based on given shortcut data. This
204 // refreshes existing shortcuts and their icons, but does not create new ones.
205 // |old_app_title| contains the title of the app prior to this update.
206 void UpdateAllShortcuts(const base::string16
& old_app_title
,
208 const extensions::Extension
* app
);
210 // Updates shortcuts for all apps in this profile. This is expected to be called
212 void UpdateShortcutsForAllApps(Profile
* profile
,
213 const base::Closure
& callback
);
215 // Returns true if given url is a valid web app url.
216 bool IsValidUrl(const GURL
& url
);
218 #if defined(TOOLKIT_VIEWS)
219 // Extracts icons info from web app data. Take only square shaped icons and
220 // sort them from smallest to largest.
221 typedef std::vector
<WebApplicationInfo::IconInfo
> IconInfoList
;
222 void GetIconsInfo(const WebApplicationInfo
& app_info
, IconInfoList
* icons
);
225 #if defined(OS_LINUX)
226 // Windows that correspond to web apps need to have a deterministic (and
227 // different) WMClass than normal chrome windows so the window manager groups
228 // them as a separate application.
229 std::string
GetWMClassFromAppName(std::string app_name
);
232 namespace internals
{
235 // Returns the Windows user-level shortcut paths that are specified in
236 // |creation_locations|.
237 std::vector
<base::FilePath
> GetShortcutPaths(
238 const ShortcutLocations
& creation_locations
);
241 // Implemented for each platform, does the platform specific parts of creating
242 // shortcuts. Used internally by CreateShortcuts methods.
243 // |shortcut_data_path| is where to store any resources created for the
244 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
245 // |shortcut_info| contains info about the shortcut to create, and
246 // |creation_locations| contains information about where to create them.
247 bool CreatePlatformShortcuts(
248 const base::FilePath
& shortcut_data_path
,
249 scoped_ptr
<ShortcutInfo
> shortcut_info
,
250 const extensions::FileHandlersInfo
& file_handlers_info
,
251 const ShortcutLocations
& creation_locations
,
252 ShortcutCreationReason creation_reason
);
254 // Delete all the shortcuts we have added for this extension. This is the
255 // platform specific implementation of the DeleteAllShortcuts function, and
256 // is executed on the FILE thread.
257 void DeletePlatformShortcuts(const base::FilePath
& shortcut_data_path
,
258 scoped_ptr
<ShortcutInfo
> shortcut_info
);
260 // Updates all the shortcuts we have added for this extension. This is the
261 // platform specific implementation of the UpdateAllShortcuts function, and
262 // is executed on the FILE thread.
263 void UpdatePlatformShortcuts(
264 const base::FilePath
& shortcut_data_path
,
265 const base::string16
& old_app_title
,
266 scoped_ptr
<ShortcutInfo
> shortcut_info
,
267 const extensions::FileHandlersInfo
& file_handlers_info
);
269 // Delete all the shortcuts for an entire profile.
270 // This is executed on the FILE thread.
271 void DeleteAllShortcutsForProfile(const base::FilePath
& profile_path
);
273 // Sanitizes |name| and returns a version of it that is safe to use as an
274 // on-disk file name .
275 base::FilePath
GetSanitizedFileName(const base::string16
& name
);
277 } // namespace internals
279 } // namespace web_app
281 #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_