dillon - branch off of chromium-67.0.3396.87 and apply 67.bad-dfly
[chromium-dfly.git] / chrome / browser / web_applications / web_app.h
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 <memory>
9 #include <string>
10 #include <vector>
11
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/macros.h"
15 #include "base/strings/string16.h"
16 #include "build/build_config.h"
17 #include "chrome/browser/shell_integration.h"
18 #include "chrome/common/web_application_info.h"
19
20 class Profile;
21
22 namespace base {
23 class TaskRunner;
24 }
25
26 namespace extensions {
27 class Extension;
28 }
29
30 namespace gfx {
31 class ImageFamily;
32 }
33
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
36 // associations, etc.
37 namespace web_app {
38
39 // Represents the info required to create a shortcut for an app.
40 struct ShortcutInfo {
41   ShortcutInfo();
42   ~ShortcutInfo();
43
44   // Run an IO task on a worker thread. Ownership of |shortcut_info| transfers
45   // to a closure that deletes it on the UI thread when the task is complete.
46   // Tasks posted here run with BACKGROUND priority and block shutdown.
47   // TODO(tapted): |reply| should be a OnceClosure, but first
48   // extensions::ImageLoaderImageCallback must be converted.
49   static void PostIOTask(base::OnceCallback<void(const ShortcutInfo&)> task,
50                          std::unique_ptr<ShortcutInfo> shortcut_info);
51   static void PostIOTaskAndReply(
52       base::OnceCallback<void(const ShortcutInfo&)> task,
53       std::unique_ptr<ShortcutInfo> shortcut_info,
54       const base::Closure& reply);
55
56   // The task runner for running shortcut tasks. On Windows this will be a task
57   // runner that permits access to COM libraries. Shortcut tasks typically deal
58   // with ensuring Profile changes are reflected on disk, so shutdown is always
59   // blocked so that an inconsistent shortcut state is not left on disk.
60   static scoped_refptr<base::TaskRunner> GetTaskRunner();
61
62   GURL url;
63   // If |extension_id| is non-empty, this is short cut is to an extension-app
64   // and the launch url will be detected at start-up. In this case, |url|
65   // is still used to generate the app id (windows app id, not chrome app id).
66   std::string extension_id;
67   bool is_platform_app = false;
68   bool from_bookmark = false;
69   base::string16 title;
70   base::string16 description;
71   base::FilePath extension_path;
72   gfx::ImageFamily favicon;
73   base::FilePath profile_path;
74   std::string profile_name;
75   std::string version_for_display;
76
77  private:
78   // ShortcutInfo must not be copied; generally it is passed around via
79   // unique_ptr. Since ImageFamily has a non-thread-safe reference count in
80   // its member and is bound to UI thread, destroy ShortcutInfo instance
81   // on UI thread.
82   DISALLOW_COPY_AND_ASSIGN(ShortcutInfo);
83 };
84
85 // This specifies a folder in the system applications menu (e.g the Start Menu
86 // on Windows).
87 //
88 // These represent the applications menu root, the "Google Chrome" folder and
89 // the "Chrome Apps" folder respectively.
90 //
91 // APP_MENU_LOCATION_HIDDEN specifies a shortcut that is used to register the
92 // app with the OS (in order to give its windows shelf icons, and correct icons
93 // and titles), but the app should not show up in menus or search results.
94 //
95 // NB: On Linux, these locations may not be used by the window manager (e.g
96 // Unity and Gnome Shell).
97 enum ApplicationsMenuLocation {
98   APP_MENU_LOCATION_NONE,
99   APP_MENU_LOCATION_SUBDIR_CHROMEAPPS,
100   APP_MENU_LOCATION_HIDDEN,
101 };
102
103 // Info about which locations to create app shortcuts in.
104 struct ShortcutLocations {
105   ShortcutLocations();
106
107   bool on_desktop;
108
109   ApplicationsMenuLocation applications_menu_location;
110
111   // For Windows, this refers to quick launch bar prior to Win7. In Win7,
112   // this means "pin to taskbar". For Mac/Linux, this could be used for
113   // Mac dock or the gnome/kde application launcher. However, those are not
114   // implemented yet.
115   bool in_quick_launch_bar;
116 };
117
118 // This encodes the cause of shortcut creation as the correct behavior in each
119 // case is implementation specific.
120 enum ShortcutCreationReason {
121   SHORTCUT_CREATION_BY_USER,
122   SHORTCUT_CREATION_AUTOMATED,
123 };
124
125 // Called by GetShortcutInfoForApp after fetching the ShortcutInfo.
126 typedef base::Callback<void(std::unique_ptr<ShortcutInfo>)>
127     ShortcutInfoCallback;
128
129 std::unique_ptr<ShortcutInfo> ShortcutInfoForExtensionAndProfile(
130     const extensions::Extension* app,
131     Profile* profile);
132
133 // Populates a ShortcutInfo for the given |extension| in |profile| and passes
134 // it to |callback| after asynchronously loading all icon representations.
135 void GetShortcutInfoForApp(const extensions::Extension* extension,
136                            Profile* profile,
137                            const ShortcutInfoCallback& callback);
138
139 // Whether to create a shortcut for this type of extension.
140 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason,
141                              Profile* profile,
142                              const extensions::Extension* extension);
143
144 // Gets the user data directory for given web app. The path for the directory is
145 // based on |extension_id|. If |extension_id| is empty then |url| is used
146 // to construct a unique ID.
147 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
148                                       const std::string& extension_id,
149                                       const GURL& url);
150
151 // Gets the user data directory to use for |extension| located inside
152 // |profile_path|.
153 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
154                                       const extensions::Extension& extension);
155
156 // Compute a deterministic name based on data in the shortcut_info.
157 std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info);
158
159 // Compute a deterministic name based on the URL. We use this pseudo name
160 // as a key to store window location per application URLs in Browser and
161 // as app id for BrowserWindow, shortcut and jump list.
162 std::string GenerateApplicationNameFromURL(const GURL& url);
163
164 // Compute a deterministic name based on an extension/apps's id.
165 std::string GenerateApplicationNameFromExtensionId(const std::string& id);
166
167 // Extracts the extension id from the app name.
168 std::string GetExtensionIdFromApplicationName(const std::string& app_name);
169
170 // Create shortcuts for web application based on given shortcut data.
171 // |shortcut_info| contains information about the shortcuts to create, and
172 // |locations| contains information about where to create them.
173 void CreateShortcutsWithInfo(ShortcutCreationReason reason,
174                              const ShortcutLocations& locations,
175                              std::unique_ptr<ShortcutInfo> shortcut_info);
176
177 // Creates shortcuts for an app. This loads the app's icon from disk, and calls
178 // CreateShortcutsWithInfo(). If you already have a ShortcutInfo with the app's
179 // icon loaded, you should use CreateShortcutsWithInfo() directly.
180 void CreateShortcuts(ShortcutCreationReason reason,
181                      const ShortcutLocations& locations,
182                      Profile* profile,
183                      const extensions::Extension* app);
184
185 // Delete all shortcuts that have been created for the given profile and
186 // extension.
187 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app);
188
189 // Updates shortcuts for |app|, but does not create new ones if shortcuts are
190 // not present in user-facing locations. Some platforms may still (re)create
191 // hidden shortcuts to interact correctly with the system shelf.
192 // |old_app_title| contains the title of the app prior to this update.
193 // |callback| is invoked once the FILE thread tasks have completed.
194 void UpdateAllShortcuts(const base::string16& old_app_title,
195                         Profile* profile,
196                         const extensions::Extension* app,
197                         const base::Closure& callback);
198
199 // Updates shortcuts for all apps in this profile. This is expected to be called
200 // on the UI thread.
201 void UpdateShortcutsForAllApps(Profile* profile,
202                                const base::Closure& callback);
203
204 // Returns true if given url is a valid web app url.
205 bool IsValidUrl(const GURL& url);
206
207 #if defined(OS_LINUX) || defined(OS_BSD)
208 // Windows that correspond to web apps need to have a deterministic (and
209 // different) WMClass than normal chrome windows so the window manager groups
210 // them as a separate application.
211 std::string GetWMClassFromAppName(std::string app_name);
212 #endif
213
214 namespace internals {
215
216 #if defined(OS_WIN)
217 // Returns the Windows user-level shortcut paths that are specified in
218 // |creation_locations|.
219 std::vector<base::FilePath> GetShortcutPaths(
220     const ShortcutLocations& creation_locations);
221 #endif
222
223 // Implemented for each platform, does the platform specific parts of creating
224 // shortcuts. Used internally by CreateShortcuts methods.
225 // |shortcut_data_path| is where to store any resources created for the
226 // shortcut, and is also used as the UserDataDir for platform app shortcuts.
227 // |shortcut_info| contains info about the shortcut to create, and
228 // |creation_locations| contains information about where to create them.
229 bool CreatePlatformShortcuts(const base::FilePath& shortcut_data_path,
230                              const ShortcutLocations& creation_locations,
231                              ShortcutCreationReason creation_reason,
232                              const ShortcutInfo& shortcut_info);
233
234 // Delete all the shortcuts we have added for this extension. This is the
235 // platform specific implementation of the DeleteAllShortcuts function, and
236 // is executed on the FILE thread.
237 void DeletePlatformShortcuts(const base::FilePath& shortcut_data_path,
238                              const ShortcutInfo& shortcut_info);
239
240 // Updates all the shortcuts we have added for this extension. This is the
241 // platform specific implementation of the UpdateAllShortcuts function, and
242 // is executed on the FILE thread.
243 void UpdatePlatformShortcuts(const base::FilePath& shortcut_data_path,
244                              const base::string16& old_app_title,
245                              const ShortcutInfo& shortcut_info);
246
247 // Delete all the shortcuts for an entire profile.
248 // This is executed on the FILE thread.
249 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path);
250
251 // Sanitizes |name| and returns a version of it that is safe to use as an
252 // on-disk file name .
253 base::FilePath GetSanitizedFileName(const base::string16& name);
254
255 }  // namespace internals
256
257 }  // namespace web_app
258
259 #endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_