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 19 class Profile; 20 21 namespace content { 22 class WebContents; 23 } 24 25 namespace extensions { 26 class Extension; 27 } 28 29 namespace gfx { 30 class 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. 36 namespace web_app { 37 38 // Represents the info required to create a shortcut for an app. 39 struct 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 // APP_MENU_LOCATION_HIDDEN specifies a shortcut that is used to register the 64 // app with the OS (in order to give its windows shelf icons, and correct icons 65 // and titles), but the app should not show up in menus or search results. 66 // 67 // NB: On Linux, these locations may not be used by the window manager (e.g 68 // Unity and Gnome Shell). 69 enum ApplicationsMenuLocation { 70 APP_MENU_LOCATION_NONE, 71 APP_MENU_LOCATION_ROOT, 72 APP_MENU_LOCATION_SUBDIR_CHROME, 73 APP_MENU_LOCATION_SUBDIR_CHROMEAPPS, 74 APP_MENU_LOCATION_HIDDEN, 75 }; 76 77 // Info about which locations to create app shortcuts in. 78 struct ShortcutLocations { 79 ShortcutLocations(); 80 81 bool on_desktop; 82 83 ApplicationsMenuLocation applications_menu_location; 84 85 // For Windows, this refers to quick launch bar prior to Win7. In Win7, 86 // this means "pin to taskbar". For Mac/Linux, this could be used for 87 // Mac dock or the gnome/kde application launcher. However, those are not 88 // implemented yet. 89 bool in_quick_launch_bar; 90 }; 91 92 // This encodes the cause of shortcut creation as the correct behavior in each 93 // case is implementation specific. 94 enum ShortcutCreationReason { 95 SHORTCUT_CREATION_BY_USER, 96 SHORTCUT_CREATION_AUTOMATED, 97 }; 98 99 // Called by GetInfoForApp after fetching the ShortcutInfo and FileHandlersInfo. 100 typedef base::Callback<void(const ShortcutInfo&, 101 const extensions::FileHandlersInfo&)> InfoCallback; 102 103 // Called by UpdateShortcutInfoAndIconForApp after loading the icon. 104 typedef base::Callback<void(const ShortcutInfo&)> ShortcutInfoCallback; 105 106 // Extracts shortcut info of the given WebContents. 107 void GetShortcutInfoForTab(content::WebContents* web_contents, 108 ShortcutInfo* info); 109 110 // Updates web app shortcut of the WebContents. This function checks and 111 // updates web app icon and shortcuts if needed. For icon, the check is based 112 // on MD5 hash of icon image. For shortcuts, it checks the desktop, start menu 113 // and quick launch (as well as pinned shortcut) for shortcut and only 114 // updates (recreates) them if they exits. 115 void UpdateShortcutForTabContents(content::WebContents* web_contents); 116 117 ShortcutInfo ShortcutInfoForExtensionAndProfile( 118 const extensions::Extension* app, 119 Profile* profile); 120 121 // Fetches the icon for |extension| and calls |callback| with shortcut info 122 // filled out as by UpdateShortcutInfoForApp. 123 void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension, 124 Profile* profile, 125 const ShortcutInfoCallback& callback); 126 127 // Whether to create a shortcut for this type of extension. 128 bool ShouldCreateShortcutFor(Profile* profile, 129 const extensions::Extension* extension); 130 131 // Gets the user data directory for given web app. The path for the directory is 132 // based on |extension_id|. If |extension_id| is empty then |url| is used 133 // to construct a unique ID. 134 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path, 135 const std::string& extension_id, 136 const GURL& url); 137 138 // Gets the user data directory to use for |extension| located inside 139 // |profile_path|. 140 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path, 141 const extensions::Extension& extension); 142 143 // Compute a deterministic name based on data in the shortcut_info. 144 std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info); 145 146 // Compute a deterministic name based on the URL. We use this pseudo name 147 // as a key to store window location per application URLs in Browser and 148 // as app id for BrowserWindow, shortcut and jump list. 149 std::string GenerateApplicationNameFromURL(const GURL& url); 150 151 // Compute a deterministic name based on an extension/apps's id. 152 std::string GenerateApplicationNameFromExtensionId(const std::string& id); 153 154 // Extracts the extension id from the app name. 155 std::string GetExtensionIdFromApplicationName(const std::string& app_name); 156 157 // Create shortcuts for web application based on given shortcut data. 158 // |shortcut_info| contains information about the shortcuts to create, and 159 // |creation_locations| contains information about where to create them. 160 void CreateShortcutsForShortcutInfo(ShortcutCreationReason reason, 161 const ShortcutLocations& locations, 162 const ShortcutInfo& shortcut_info); 163 164 // Creates shortcuts for an app. 165 void CreateShortcuts(ShortcutCreationReason reason, 166 const ShortcutLocations& locations, 167 Profile* profile, 168 const extensions::Extension* app); 169 170 // Delete all shortcuts that have been created for the given profile and 171 // extension. 172 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app); 173 174 // Updates shortcuts for web application based on given shortcut data. This 175 // refreshes existing shortcuts and their icons, but does not create new ones. 176 // |old_app_title| contains the title of the app prior to this update. 177 void UpdateAllShortcuts(const base::string16& old_app_title, 178 Profile* profile, 179 const extensions::Extension* app); 180 181 // Updates shortcuts for all apps in this profile. This is expected to be called 182 // on the UI thread. 183 void UpdateShortcutsForAllApps(Profile* profile, 184 const base::Closure& callback); 185 186 // Returns true if given url is a valid web app url. 187 bool IsValidUrl(const GURL& url); 188 189 #if defined(TOOLKIT_VIEWS) 190 // Extracts icons info from web app data. Take only square shaped icons and 191 // sort them from smallest to largest. 192 typedef std::vector<WebApplicationInfo::IconInfo> IconInfoList; 193 void GetIconsInfo(const WebApplicationInfo& app_info, IconInfoList* icons); 194 #endif 195 196 #if defined(OS_LINUX) 197 // Windows that correspond to web apps need to have a deterministic (and 198 // different) WMClass than normal chrome windows so the window manager groups 199 // them as a separate application. 200 std::string GetWMClassFromAppName(std::string app_name); 201 #endif 202 203 namespace internals { 204 205 // Loads relevant info structs for the app and calls |callback|. 206 void GetInfoForApp(const extensions::Extension* extension, 207 Profile* profile, 208 const InfoCallback& callback); 209 210 #if defined(OS_WIN) 211 // Returns the Windows user-level shortcut paths that are specified in 212 // |creation_locations|. 213 std::vector<base::FilePath> GetShortcutPaths( 214 const ShortcutLocations& creation_locations); 215 #endif 216 217 // Creates a shortcut. Must be called on the file thread. This is used to 218 // implement CreateShortcuts() above, and can also be used directly from the 219 // file thread. |shortcut_info| contains info about the shortcut to create, and 220 // |creation_locations| contains information about where to create them. 221 bool CreateShortcutsOnFileThread(ShortcutCreationReason reason, 222 const ShortcutLocations& locations, 223 const ShortcutInfo& shortcut_info); 224 225 // Implemented for each platform, does the platform specific parts of creating 226 // shortcuts. Used internally by CreateShortcutsOnFileThread. 227 // |shortcut_data_path| is where to store any resources created for the 228 // shortcut, and is also used as the UserDataDir for platform app shortcuts. 229 // |shortcut_info| contains info about the shortcut to create, and 230 // |creation_locations| contains information about where to create them. 231 bool CreatePlatformShortcuts( 232 const base::FilePath& shortcut_data_path, 233 const ShortcutInfo& shortcut_info, 234 const extensions::FileHandlersInfo& file_handlers_info, 235 const ShortcutLocations& creation_locations, 236 ShortcutCreationReason creation_reason); 237 238 // Delete all the shortcuts we have added for this extension. This is the 239 // platform specific implementation of the DeleteAllShortcuts function, and 240 // is executed on the FILE thread. 241 void DeletePlatformShortcuts(const base::FilePath& shortcut_data_path, 242 const ShortcutInfo& shortcut_info); 243 244 // Updates all the shortcuts we have added for this extension. This is the 245 // platform specific implementation of the UpdateAllShortcuts function, and 246 // is executed on the FILE thread. 247 void UpdatePlatformShortcuts( 248 const base::FilePath& shortcut_data_path, 249 const base::string16& old_app_title, 250 const ShortcutInfo& shortcut_info, 251 const extensions::FileHandlersInfo& file_handlers_info); 252 253 // Delete all the shortcuts for an entire profile. 254 // This is executed on the FILE thread. 255 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path); 256 257 // Sanitizes |name| and returns a version of it that is safe to use as an 258 // on-disk file name . 259 base::FilePath GetSanitizedFileName(const base::string16& name); 260 261 } // namespace internals 262 263 } // namespace web_app 264 265 #endif // CHROME_BROWSER_WEB_APPLICATIONS_WEB_APP_H_ 266