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
;
52 base::string16 description
;
53 base::FilePath extension_path
;
54 gfx::ImageFamily favicon
;
55 base::FilePath profile_path
;
56 std::string profile_name
;
57 std::string version_for_display
;
60 // ShortcutInfo must not be copied; generally it is passed around via
61 // scoped_ptrs. This is to allow passing ShortcutInfos between threads,
62 // despite ImageFamily having a non-thread-safe reference count.
63 DISALLOW_COPY_AND_ASSIGN(ShortcutInfo
);
66 // This specifies a folder in the system applications menu (e.g the Start Menu
69 // These represent the applications menu root, the "Google Chrome" folder and
70 // the "Chrome Apps" folder respectively.
72 // APP_MENU_LOCATION_HIDDEN specifies a shortcut that is used to register the
73 // app with the OS (in order to give its windows shelf icons, and correct icons
74 // and titles), but the app should not show up in menus or search results.
76 // NB: On Linux, these locations may not be used by the window manager (e.g
77 // Unity and Gnome Shell).
78 enum ApplicationsMenuLocation
{
79 APP_MENU_LOCATION_NONE
,
80 APP_MENU_LOCATION_ROOT
,
81 APP_MENU_LOCATION_SUBDIR_CHROME
,
82 APP_MENU_LOCATION_SUBDIR_CHROMEAPPS
,
83 APP_MENU_LOCATION_HIDDEN
,
86 // Info about which locations to create app shortcuts in.
87 struct ShortcutLocations
{
92 ApplicationsMenuLocation applications_menu_location
;
94 // For Windows, this refers to quick launch bar prior to Win7. In Win7,
95 // this means "pin to taskbar". For Mac/Linux, this could be used for
96 // Mac dock or the gnome/kde application launcher. However, those are not
98 bool in_quick_launch_bar
;
101 // This encodes the cause of shortcut creation as the correct behavior in each
102 // case is implementation specific.
103 enum ShortcutCreationReason
{
104 SHORTCUT_CREATION_BY_USER
,
105 SHORTCUT_CREATION_AUTOMATED
,
108 // Called by GetInfoForApp after fetching the ShortcutInfo and FileHandlersInfo.
109 typedef base::Callback
<void(scoped_ptr
<ShortcutInfo
>,
110 const extensions::FileHandlersInfo
&)> InfoCallback
;
112 // Called by GetShortcutInfoForApp after fetching the ShortcutInfo.
113 typedef base::Callback
<void(scoped_ptr
<ShortcutInfo
>)> ShortcutInfoCallback
;
115 #if defined(TOOLKIT_VIEWS)
116 // Extracts shortcut info of the given WebContents.
117 scoped_ptr
<ShortcutInfo
> GetShortcutInfoForTab(
118 content::WebContents
* web_contents
);
121 // Updates web app shortcut of the WebContents. This function checks and
122 // updates web app icon and shortcuts if needed. For icon, the check is based
123 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu
124 // and quick launch (as well as pinned shortcut) for shortcut and only
125 // updates (recreates) them if they exits.
126 void UpdateShortcutForTabContents(content::WebContents
* web_contents
);
128 scoped_ptr
<ShortcutInfo
> ShortcutInfoForExtensionAndProfile(
129 const extensions::Extension
* app
,
132 // Populates a ShortcutInfo and FileHandlersInfo for the given |extension| in
133 // |profile| and passes them to |callback| after asynchronously loading all icon
135 void GetInfoForApp(const extensions::Extension
* extension
,
137 const InfoCallback
& callback
);
139 // Populates a ShortcutInfo for the given |extension| in |profile| and passes
140 // it to |callback| after asynchronously loading all icon representations. This
141 // is equivalent to GetInfoForApp, but it throws away the FileHandlersInfo.
142 void GetShortcutInfoForApp(const extensions::Extension
* extension
,
144 const ShortcutInfoCallback
& callback
);
146 // Whether to create a shortcut for this type of extension.
147 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason
,
149 const extensions::Extension
* extension
);
151 // Gets the user data directory for given web app. The path for the directory is
152 // based on |extension_id|. If |extension_id| is empty then |url| is used
153 // to construct a unique ID.
154 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
155 const std::string
& extension_id
,
158 // Gets the user data directory to use for |extension| located inside
160 base::FilePath
GetWebAppDataDirectory(const base::FilePath
& profile_path
,
161 const extensions::Extension
& extension
);
163 // Compute a deterministic name based on data in the shortcut_info.
164 std::string
GenerateApplicationNameFromInfo(const ShortcutInfo
& shortcut_info
);
166 // Compute a deterministic name based on the URL. We use this pseudo name
167 // as a key to store window location per application URLs in Browser and
168 // as app id for BrowserWindow, shortcut and jump list.
169 std::string
GenerateApplicationNameFromURL(const GURL
& url
);
171 // Compute a deterministic name based on an extension/apps's id.
172 std::string
GenerateApplicationNameFromExtensionId(const std::string
& id
);
174 // Extracts the extension id from the app name.
175 std::string
GetExtensionIdFromApplicationName(const std::string
& app_name
);
177 // Create shortcuts for web application based on given shortcut data.
178 // |shortcut_info| contains information about the shortcuts to create, and
179 // |locations| contains information about where to create them, while
180 // |file_handlers_info| contains information about the file handlers to create.
181 void CreateShortcutsWithInfo(
182 ShortcutCreationReason reason
,
183 const ShortcutLocations
& locations
,
184 scoped_ptr
<ShortcutInfo
> shortcut_info
,
185 const extensions::FileHandlersInfo
& file_handlers_info
);
187 // Currently only called by app_list_service_mac to create a shim for the
189 void CreateNonAppShortcut(const ShortcutLocations
& locations
,
190 scoped_ptr
<ShortcutInfo
> shortcut_info
);
192 // Creates shortcuts for an app. This loads the app's icon from disk, and calls
193 // CreateShortcutsWithInfo(). If you already have a ShortcutInfo with the app's
194 // icon loaded, you should use CreateShortcutsWithInfo() directly.
195 void CreateShortcuts(ShortcutCreationReason reason
,
196 const ShortcutLocations
& locations
,
198 const extensions::Extension
* app
);
200 // Delete all shortcuts that have been created for the given profile and
202 void DeleteAllShortcuts(Profile
* profile
, const extensions::Extension
* app
);
204 // Updates shortcuts for web application based on given shortcut data. This
205 // refreshes existing shortcuts and their icons, but does not create new ones.
206 // |old_app_title| contains the title of the app prior to this update.
207 void UpdateAllShortcuts(const base::string16
& old_app_title
,
209 const extensions::Extension
* app
);
211 // Updates shortcuts for all apps in this profile. This is expected to be called
213 void UpdateShortcutsForAllApps(Profile
* profile
,
214 const base::Closure
& callback
);
216 // Returns true if given url is a valid web app url.
217 bool IsValidUrl(const GURL
& url
);
219 #if defined(TOOLKIT_VIEWS)
220 // Extracts icons info from web app data. Take only square shaped icons and
221 // sort them from smallest to largest.
222 typedef std::vector
<WebApplicationInfo::IconInfo
> IconInfoList
;
223 void GetIconsInfo(const WebApplicationInfo
& app_info
, IconInfoList
* icons
);
226 #if defined(OS_LINUX)
227 // Windows that correspond to web apps need to have a deterministic (and
228 // different) WMClass than normal chrome windows so the window manager groups
229 // them as a separate application.
230 std::string
GetWMClassFromAppName(std::string app_name
);
233 namespace internals
{
236 // Returns the Windows user-level shortcut paths that are specified in
237 // |creation_locations|.
238 std::vector
<base::FilePath
> GetShortcutPaths(
239 const ShortcutLocations
& creation_locations
);
242 // Implemented for each platform, does the platform specific parts of creating
243 // shortcuts. Used internally by CreateShortcuts methods.
244 // |shortcut_data_path| is where to store any resources created for the
245 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
246 // |shortcut_info| contains info about the shortcut to create, and
247 // |creation_locations| contains information about where to create them.
248 bool CreatePlatformShortcuts(
249 const base::FilePath
& shortcut_data_path
,
250 scoped_ptr
<ShortcutInfo
> shortcut_info
,
251 const extensions::FileHandlersInfo
& file_handlers_info
,
252 const ShortcutLocations
& creation_locations
,
253 ShortcutCreationReason creation_reason
);
255 // Delete all the shortcuts we have added for this extension. This is the
256 // platform specific implementation of the DeleteAllShortcuts function, and
257 // is executed on the FILE thread.
258 void DeletePlatformShortcuts(const base::FilePath
& shortcut_data_path
,
259 scoped_ptr
<ShortcutInfo
> shortcut_info
);
261 // Updates all the shortcuts we have added for this extension. This is the
262 // platform specific implementation of the UpdateAllShortcuts function, and
263 // is executed on the FILE thread.
264 void UpdatePlatformShortcuts(
265 const base::FilePath
& shortcut_data_path
,
266 const base::string16
& old_app_title
,
267 scoped_ptr
<ShortcutInfo
> shortcut_info
,
268 const extensions::FileHandlersInfo
& file_handlers_info
);
270 // Delete all the shortcuts for an entire profile.
271 // This is executed on the FILE thread.
272 void DeleteAllShortcutsForProfile(const base::FilePath
& profile_path
);
274 // Sanitizes |name| and returns a version of it that is safe to use as an
275 // on-disk file name .
276 base::FilePath
GetSanitizedFileName(const base::string16
& name
);
278 } // namespace internals
280 } // namespace web_app
282 #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_