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/extensions/file_handler_info.h"
17 #include "chrome/common/web_application_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 web_app::ShortcutInfo&)>
103 ShortcutInfoCallback;
105 // Extracts shortcut info of the given WebContents.
106 void GetShortcutInfoForTab(content::WebContents* web_contents,
107 web_app::ShortcutInfo* info);
109 // Updates web app shortcut of the WebContents. This function checks and
110 // updates web app icon and shortcuts if needed. For icon, the check is based
111 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu
112 // and quick launch (as well as pinned shortcut) for shortcut and only
113 // updates (recreates) them if they exits.
114 void UpdateShortcutForTabContents(content::WebContents* web_contents);
116 web_app::ShortcutInfo ShortcutInfoForExtensionAndProfile(
117 const extensions::Extension* app,
120 // Fetches the icon for |extension| and calls |callback| with shortcut info
121 // filled out as by UpdateShortcutInfoForApp.
122 void UpdateShortcutInfoAndIconForApp(
123 const extensions::Extension* extension,
125 const ShortcutInfoCallback& callback);
127 // Gets the user data directory for given web app. The path for the directory is
128 // based on |extension_id|. If |extension_id| is empty then |url| is used
129 // to construct a unique ID.
130 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
131 const std::string& extension_id,
134 // Gets the user data directory to use for |extension| located inside
136 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
137 const extensions::Extension& extension);
139 // Compute a deterministic name based on data in the shortcut_info.
140 std::string GenerateApplicationNameFromInfo(
141 const web_app::ShortcutInfo& shortcut_info);
143 // Compute a deterministic name based on the URL. We use this pseudo name
144 // as a key to store window location per application URLs in Browser and
145 // as app id for BrowserWindow, shortcut and jump list.
146 std::string GenerateApplicationNameFromURL(const GURL& url);
148 // Compute a deterministic name based on an extension/apps's id.
149 std::string GenerateApplicationNameFromExtensionId(const std::string& id);
151 // Extracts the extension id from the app name.
152 std::string GetExtensionIdFromApplicationName(const std::string& app_name);
154 // Create shortcuts for web application based on given shortcut data.
155 // |shortcut_info| contains information about the shortcuts to create, and
156 // |creation_locations| contains information about where to create them.
157 void CreateShortcutsForShortcutInfo(
158 web_app::ShortcutCreationReason reason,
159 const web_app::ShortcutLocations& locations,
160 const web_app::ShortcutInfo& shortcut_info);
162 // Creates shortcuts for an app.
163 void CreateShortcuts(
164 ShortcutCreationReason reason,
165 const web_app::ShortcutLocations& locations,
167 const extensions::Extension* app);
169 // Delete all shortcuts that have been created for the given profile and
171 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app);
173 // Updates shortcuts for web application based on given shortcut data. This
174 // refreshes existing shortcuts and their icons, but does not create new ones.
175 // |old_app_title| contains the title of the app prior to this update.
176 void UpdateAllShortcuts(const base::string16& old_app_title,
178 const extensions::Extension* app);
180 // Returns true if given url is a valid web app url.
181 bool IsValidUrl(const GURL& url);
183 #if defined(TOOLKIT_VIEWS)
184 // Extracts icons info from web app data. Take only square shaped icons and
185 // sort them from smallest to largest.
186 typedef std::vector<WebApplicationInfo::IconInfo> IconInfoList;
187 void GetIconsInfo(const WebApplicationInfo& app_info,
188 IconInfoList* icons);
191 #if defined(OS_LINUX)
192 // Windows that correspond to web apps need to have a deterministic (and
193 // different) WMClass than normal chrome windows so the window manager groups
194 // them as a separate application.
195 std::string GetWMClassFromAppName(std::string app_name);
198 namespace internals {
201 // Returns the Windows user-level shortcut paths that are specified in
202 // |creation_locations|.
203 std::vector<base::FilePath> GetShortcutPaths(
204 const web_app::ShortcutLocations& creation_locations);
207 // Creates a shortcut. Must be called on the file thread. This is used to
208 // implement CreateShortcuts() above, and can also be used directly from the
209 // file thread. |shortcut_info| contains info about the shortcut to create, and
210 // |creation_locations| contains information about where to create them.
211 bool CreateShortcutsOnFileThread(
212 ShortcutCreationReason reason,
213 const web_app::ShortcutLocations& locations,
214 const web_app::ShortcutInfo& shortcut_info);
216 // Implemented for each platform, does the platform specific parts of creating
217 // shortcuts. Used internally by CreateShortcutsOnFileThread.
218 // |shortcut_data_path| is where to store any resources created for the
219 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
220 // |shortcut_info| contains info about the shortcut to create, and
221 // |creation_locations| contains information about where to create them.
222 bool CreatePlatformShortcuts(
223 const base::FilePath& shortcut_data_path,
224 const web_app::ShortcutInfo& shortcut_info,
225 const extensions::FileHandlersInfo& file_handlers_info,
226 const web_app::ShortcutLocations& creation_locations,
227 ShortcutCreationReason creation_reason);
229 // Delete all the shortcuts we have added for this extension. This is the
230 // platform specific implementation of the DeleteAllShortcuts function, and
231 // is executed on the FILE thread.
232 void DeletePlatformShortcuts(
233 const base::FilePath& shortcut_data_path,
234 const web_app::ShortcutInfo& shortcut_info);
236 // Updates all the shortcuts we have added for this extension. This is the
237 // platform specific implementation of the UpdateAllShortcuts function, and
238 // is executed on the FILE thread.
239 void UpdatePlatformShortcuts(
240 const base::FilePath& shortcut_data_path,
241 const base::string16& old_app_title,
242 const web_app::ShortcutInfo& shortcut_info,
243 const extensions::FileHandlersInfo& file_handlers_info);
245 // Delete all the shortcuts for an entire profile.
246 // This is executed on the FILE thread.
247 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path);
249 // Sanitizes |name| and returns a version of it that is safe to use as an
250 // on-disk file name .
251 base::FilePath GetSanitizedFileName(const base::string16& name);
253 } // namespace internals
255 } // namespace web_app
257 #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_