web_app.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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
19class Profile;
20
21namespace content {
22class WebContents;
23}
24
25namespace extensions {
26class Extension;
27}
28
29namespace gfx {
30class 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.
36namespace web_app {
37
38// Represents the info required to create a shortcut for an app.
39struct 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// NB: On Linux, these locations may not be used by the window manager (e.g
64// Unity and Gnome Shell).
65enum ApplicationsMenuLocation {
66  APP_MENU_LOCATION_NONE,
67  APP_MENU_LOCATION_ROOT,
68  APP_MENU_LOCATION_SUBDIR_CHROME,
69  APP_MENU_LOCATION_SUBDIR_CHROMEAPPS,
70};
71
72// Info about which locations to create app shortcuts in.
73struct ShortcutLocations {
74  ShortcutLocations();
75
76  bool on_desktop;
77
78  ApplicationsMenuLocation applications_menu_location;
79
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
83  // implemented yet.
84  bool in_quick_launch_bar;
85
86#if defined(OS_POSIX)
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.
91  bool hidden;
92#endif
93};
94
95// This encodes the cause of shortcut creation as the correct behavior in each
96// case is implementation specific.
97enum ShortcutCreationReason {
98  SHORTCUT_CREATION_BY_USER,
99  SHORTCUT_CREATION_AUTOMATED,
100};
101
102typedef base::Callback<void(const ShortcutInfo&)> ShortcutInfoCallback;
103
104// Extracts shortcut info of the given WebContents.
105void GetShortcutInfoForTab(content::WebContents* web_contents,
106                           ShortcutInfo* info);
107
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.
113void UpdateShortcutForTabContents(content::WebContents* web_contents);
114
115ShortcutInfo ShortcutInfoForExtensionAndProfile(
116    const extensions::Extension* app,
117    Profile* profile);
118
119// Fetches the icon for |extension| and calls |callback| with shortcut info
120// filled out as by UpdateShortcutInfoForApp.
121void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension,
122                                     Profile* profile,
123                                     const ShortcutInfoCallback& callback);
124
125// Whether to create a shortcut for this type of extension.
126bool ShouldCreateShortcutFor(Profile* profile,
127                             const extensions::Extension* extension);
128
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.
132base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
133                                      const std::string& extension_id,
134                                      const GURL& url);
135
136// Gets the user data directory to use for |extension| located inside
137// |profile_path|.
138base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
139                                      const extensions::Extension& extension);
140
141// Compute a deterministic name based on data in the shortcut_info.
142std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info);
143
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.
147std::string GenerateApplicationNameFromURL(const GURL& url);
148
149// Compute a deterministic name based on an extension/apps's id.
150std::string GenerateApplicationNameFromExtensionId(const std::string& id);
151
152// Extracts the extension id from the app name.
153std::string GetExtensionIdFromApplicationName(const std::string& app_name);
154
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.
158void CreateShortcutsForShortcutInfo(ShortcutCreationReason reason,
159                                    const ShortcutLocations& locations,
160                                    const ShortcutInfo& shortcut_info);
161
162// Creates shortcuts for an app.
163void CreateShortcuts(ShortcutCreationReason reason,
164                     const ShortcutLocations& locations,
165                     Profile* profile,
166                     const extensions::Extension* app);
167
168// Delete all shortcuts that have been created for the given profile and
169// extension.
170void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app);
171
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.
175void UpdateAllShortcuts(const base::string16& old_app_title,
176                        Profile* profile,
177                        const extensions::Extension* app);
178
179// Updates shortcuts for all apps in this profile. This is expected to be called
180// on the UI thread.
181void UpdateShortcutsForAllApps(Profile* profile,
182                               const base::Closure& callback);
183
184// Returns true if given url is a valid web app url.
185bool IsValidUrl(const GURL& url);
186
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.
190typedef std::vector<WebApplicationInfo::IconInfo> IconInfoList;
191void GetIconsInfo(const WebApplicationInfo& app_info, IconInfoList* icons);
192#endif
193
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.
198std::string GetWMClassFromAppName(std::string app_name);
199#endif
200
201namespace internals {
202
203#if defined(OS_WIN)
204// Returns the Windows user-level shortcut paths that are specified in
205// |creation_locations|.
206std::vector<base::FilePath> GetShortcutPaths(
207    const ShortcutLocations& creation_locations);
208#endif
209
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.
214bool CreateShortcutsOnFileThread(ShortcutCreationReason reason,
215                                 const ShortcutLocations& locations,
216                                 const ShortcutInfo& shortcut_info);
217
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.
224bool 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);
230
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.
234void DeletePlatformShortcuts(const base::FilePath& shortcut_data_path,
235                             const ShortcutInfo& shortcut_info);
236
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.
240void 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);
245
246// Delete all the shortcuts for an entire profile.
247// This is executed on the FILE thread.
248void DeleteAllShortcutsForProfile(const base::FilePath& profile_path);
249
250// Sanitizes |name| and returns a version of it that is safe to use as an
251// on-disk file name .
252base::FilePath GetSanitizedFileName(const base::string16& name);
253
254}  // namespace internals
255
256}  // namespace web_app
257
258#endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
259