web_app.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/files/file_path.h"
12#include "base/strings/string16.h"
13#include "build/build_config.h"
14#include "chrome/browser/shell_integration.h"
15#include "chrome/common/web_application_info.h"
16
17namespace extensions {
18class Extension;
19}
20
21namespace gfx {
22class ImageFamily;
23}
24
25namespace web_app {
26
27// Policy on whether to create duplicate shortcuts.
28enum ShortcutCreationPolicy {
29  // Duplicate shortcuts may be created, at the discretion of the
30  // implementation.
31  ALLOW_DUPLICATE_SHORTCUTS,
32  DONT_CREATE_DUPLICATE_SHORTCUTS
33};
34
35// Gets the user data directory for given web app. The path for the directory is
36// based on |extension_id|. If |extension_id| is empty then |url| is used
37// to construct a unique ID.
38base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
39                                      const std::string& extension_id,
40                                      const GURL& url);
41
42// Gets the user data directory to use for |extension| located inside
43// |profile_path|.
44base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
45                                      const extensions::Extension& extension);
46
47// Compute a deterministic name based on data in the shortcut_info.
48std::string GenerateApplicationNameFromInfo(
49    const ShellIntegration::ShortcutInfo& shortcut_info);
50
51// Compute a deterministic name based on the URL. We use this pseudo name
52// as a key to store window location per application URLs in Browser and
53// as app id for BrowserWindow, shortcut and jump list.
54std::string GenerateApplicationNameFromURL(const GURL& url);
55
56// Compute a deterministic name based on an extension/apps's id.
57std::string GenerateApplicationNameFromExtensionId(const std::string& id);
58
59// Extracts the extension id from the app name.
60std::string GetExtensionIdFromApplicationName(const std::string& app_name);
61
62// Creates shortcuts for web application based on given shortcut data.
63// |shortcut_info| contains information about the shortcuts to create, and
64// |creation_locations| contains information about where to create them.
65void CreateShortcuts(
66    const ShellIntegration::ShortcutInfo& shortcut_info,
67    const ShellIntegration::ShortcutLocations& creation_locations,
68    ShortcutCreationPolicy creation_policy);
69
70// Delete all the shortcuts that have been created for the given
71// |shortcut_data| in the profile with |profile_path|.
72void DeleteAllShortcuts(const ShellIntegration::ShortcutInfo& shortcut_info);
73
74// Updates shortcuts for web application based on given shortcut data. This
75// refreshes existing shortcuts and their icons, but does not create new ones.
76// |old_app_title| contains the title of the app prior to this update.
77// |shortcut_info| contains information about the shortcuts to update.
78void UpdateAllShortcuts(const string16& old_app_title,
79                        const ShellIntegration::ShortcutInfo& shortcut_info);
80
81// Creates a shortcut. Must be called on the file thread. This is used to
82// implement CreateShortcuts() above, and can also be used directly from the
83// file thread. |shortcut_info| contains info about the shortcut to create, and
84// |creation_locations| contains information about where to create them.
85bool CreateShortcutsOnFileThread(
86    const ShellIntegration::ShortcutInfo& shortcut_info,
87    const ShellIntegration::ShortcutLocations& creation_locations,
88    ShortcutCreationPolicy creation_policy);
89
90// Returns true if given url is a valid web app url.
91bool IsValidUrl(const GURL& url);
92
93#if defined(TOOLKIT_VIEWS)
94// Extracts icons info from web app data. Take only square shaped icons and
95// sort them from smallest to largest.
96typedef std::vector<WebApplicationInfo::IconInfo> IconInfoList;
97void GetIconsInfo(const WebApplicationInfo& app_info,
98                  IconInfoList* icons);
99#endif
100
101#if defined(TOOLKIT_GTK)
102// GTK+ windows that correspond to web apps need to have a deterministic (and
103// different) WMClass than normal chrome windows so the window manager groups
104// them as a separate application.
105std::string GetWMClassFromAppName(std::string app_name);
106#endif
107
108// Gets the name of the Chrome Apps menu folder in which to place app shortcuts.
109string16 GetAppShortcutsSubdirName();
110
111namespace internals {
112
113#if defined(OS_WIN)
114std::vector<base::FilePath> GetShortcutPaths(
115    const ShellIntegration::ShortcutLocations& creation_locations);
116#endif
117
118// Implemented for each platform, does the platform specific parts of creating
119// shortcuts. Used internally by CreateShortcutsOnFileThread.
120// |shortcut_data_path| is where to store any resources created for the
121// shortcut, and is also used as the UserDataDir for platform app shortcuts.
122// |shortcut_info| contains info about the shortcut to create, and
123// |creation_locations| contains information about where to create them.
124bool CreatePlatformShortcuts(
125    const base::FilePath& shortcut_data_path,
126    const ShellIntegration::ShortcutInfo& shortcut_info,
127    const ShellIntegration::ShortcutLocations& creation_locations,
128    ShortcutCreationPolicy creation_policy);
129
130// Delete all the shortcuts we have added for this extension. This is the
131// platform specific implementation of the DeleteAllShortcuts function, and
132// is executed on the FILE thread.
133void DeletePlatformShortcuts(
134    const base::FilePath& shortcut_data_path,
135    const ShellIntegration::ShortcutInfo& shortcut_info);
136
137// Updates all the shortcuts we have added for this extension. This is the
138// platform specific implementation of the UpdateAllShortcuts function, and
139// is executed on the FILE thread.
140void UpdatePlatformShortcuts(
141    const base::FilePath& shortcut_data_path,
142    const string16& old_app_title,
143    const ShellIntegration::ShortcutInfo& shortcut_info);
144
145// Delete all the shortcuts for an entire profile.
146// This is executed on the FILE thread.
147void DeleteAllShortcutsForProfile(const base::FilePath& profile_path);
148
149// Sanitizes |name| and returns a version of it that is safe to use as an
150// on-disk file name .
151base::FilePath GetSanitizedFileName(const string16& name);
152
153}  // namespace internals
154
155}  // namespace web_app
156
157#endif  // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_
158