ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / web_applications / web_app.cc
blobabe41d0f2c8ac903463f038c6fbe78f8a64239ab
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 #include "chrome/browser/web_applications/web_app.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/files/file_util.h"
11 #include "base/i18n/file_util_icu.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/thread.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/extensions/extension_ui_util.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/chrome_version_info.h"
23 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
24 #include "chrome/common/pref_names.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/image_loader.h"
28 #include "extensions/common/constants.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/extension_set.h"
31 #include "extensions/common/manifest_handlers/icons_handler.h"
32 #include "extensions/grit/extensions_browser_resources.h"
33 #include "skia/ext/image_operations.h"
34 #include "third_party/skia/include/core/SkBitmap.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/gfx/image/image.h"
37 #include "ui/gfx/image/image_family.h"
38 #include "ui/gfx/image/image_skia.h"
39 #include "url/url_constants.h"
41 #if defined(OS_WIN)
42 #include "ui/gfx/icon_util.h"
43 #endif
45 #if defined(TOOLKIT_VIEWS)
46 #include "chrome/browser/extensions/tab_helper.h"
47 #include "chrome/browser/favicon/favicon_tab_helper.h"
48 #endif
50 using content::BrowserThread;
52 namespace {
54 #if defined(OS_MACOSX)
55 const int kDesiredSizes[] = {16, 32, 128, 256, 512};
56 const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
57 #elif defined(OS_LINUX)
58 // Linux supports icons of any size. FreeDesktop Icon Theme Specification states
59 // that "Minimally you should install a 48x48 icon in the hicolor theme."
60 const int kDesiredSizes[] = {16, 32, 48, 128, 256, 512};
61 const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
62 #elif defined(OS_WIN)
63 const int* kDesiredSizes = IconUtil::kIconDimensions;
64 const size_t kNumDesiredSizes = IconUtil::kNumIconDimensions;
65 #else
66 const int kDesiredSizes[] = {32};
67 const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
68 #endif
70 #if defined(TOOLKIT_VIEWS)
71 // Predicator for sorting images from largest to smallest.
72 bool IconPrecedes(const WebApplicationInfo::IconInfo& left,
73 const WebApplicationInfo::IconInfo& right) {
74 return left.width < right.width;
76 #endif
78 base::FilePath GetShortcutDataDir(const web_app::ShortcutInfo& shortcut_info) {
79 return web_app::GetWebAppDataDirectory(shortcut_info.profile_path,
80 shortcut_info.extension_id,
81 shortcut_info.url);
84 void UpdateAllShortcutsForShortcutInfo(
85 const base::string16& old_app_title,
86 const web_app::ShortcutInfo& shortcut_info,
87 const extensions::FileHandlersInfo& file_handlers_info) {
88 BrowserThread::PostTask(
89 BrowserThread::FILE,
90 FROM_HERE,
91 base::Bind(&web_app::internals::UpdatePlatformShortcuts,
92 GetShortcutDataDir(shortcut_info),
93 old_app_title, shortcut_info, file_handlers_info));
96 void OnImageLoaded(web_app::ShortcutInfo shortcut_info,
97 extensions::FileHandlersInfo file_handlers_info,
98 web_app::InfoCallback callback,
99 const gfx::ImageFamily& image_family) {
100 // If the image failed to load (e.g. if the resource being loaded was empty)
101 // use the standard application icon.
102 if (image_family.empty()) {
103 gfx::Image default_icon =
104 ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON);
105 int size = kDesiredSizes[kNumDesiredSizes - 1];
106 SkBitmap bmp = skia::ImageOperations::Resize(
107 *default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST,
108 size, size);
109 gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp);
110 // We are on the UI thread, and this image is needed from the FILE thread,
111 // for creating shortcut icon files.
112 image_skia.MakeThreadSafe();
113 shortcut_info.favicon.Add(gfx::Image(image_skia));
114 } else {
115 shortcut_info.favicon = image_family;
118 callback.Run(shortcut_info, file_handlers_info);
121 void IgnoreFileHandlersInfo(
122 const web_app::ShortcutInfoCallback& shortcut_info_callback,
123 const web_app::ShortcutInfo& shortcut_info,
124 const extensions::FileHandlersInfo& file_handlers_info) {
125 shortcut_info_callback.Run(shortcut_info);
128 void ScheduleCreatePlatformShortcut(
129 web_app::ShortcutCreationReason reason,
130 const web_app::ShortcutLocations& locations,
131 const web_app::ShortcutInfo& shortcut_info,
132 const extensions::FileHandlersInfo& file_handlers_info) {
133 BrowserThread::PostTask(
134 BrowserThread::FILE, FROM_HERE,
135 base::Bind(
136 base::IgnoreResult(&web_app::internals::CreatePlatformShortcuts),
137 GetShortcutDataDir(shortcut_info), shortcut_info, file_handlers_info,
138 locations, reason));
141 } // namespace
143 namespace web_app {
145 // The following string is used to build the directory name for
146 // shortcuts to chrome applications (the kind which are installed
147 // from a CRX). Application shortcuts to URLs use the {host}_{path}
148 // for the name of this directory. Hosts can't include an underscore.
149 // By starting this string with an underscore, we ensure that there
150 // are no naming conflicts.
151 static const char kCrxAppPrefix[] = "_crx_";
153 namespace internals {
155 base::FilePath GetSanitizedFileName(const base::string16& name) {
156 #if defined(OS_WIN)
157 base::string16 file_name = name;
158 #else
159 std::string file_name = base::UTF16ToUTF8(name);
160 #endif
161 base::i18n::ReplaceIllegalCharactersInPath(&file_name, '_');
162 return base::FilePath(file_name);
165 } // namespace internals
167 ShortcutInfo::ShortcutInfo()
168 : is_platform_app(false) {
171 ShortcutInfo::~ShortcutInfo() {}
173 ShortcutLocations::ShortcutLocations()
174 : on_desktop(false),
175 applications_menu_location(APP_MENU_LOCATION_NONE),
176 in_quick_launch_bar(false) {
179 #if defined(TOOLKIT_VIEWS)
180 void GetShortcutInfoForTab(content::WebContents* web_contents,
181 ShortcutInfo* info) {
182 DCHECK(info); // Must provide a valid info.
184 const FaviconTabHelper* favicon_tab_helper =
185 FaviconTabHelper::FromWebContents(web_contents);
186 const extensions::TabHelper* extensions_tab_helper =
187 extensions::TabHelper::FromWebContents(web_contents);
188 const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info();
190 info->url = app_info.app_url.is_empty() ? web_contents->GetURL() :
191 app_info.app_url;
192 info->title = app_info.title.empty() ?
193 (web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) :
194 web_contents->GetTitle()) :
195 app_info.title;
196 info->description = app_info.description;
197 info->favicon.Add(favicon_tab_helper->GetFavicon());
199 Profile* profile =
200 Profile::FromBrowserContext(web_contents->GetBrowserContext());
201 info->profile_path = profile->GetPath();
203 #endif
205 #if !defined(OS_WIN)
206 void UpdateShortcutForTabContents(content::WebContents* web_contents) {}
207 #endif
209 ShortcutInfo ShortcutInfoForExtensionAndProfile(
210 const extensions::Extension* app, Profile* profile) {
211 ShortcutInfo shortcut_info;
212 shortcut_info.extension_id = app->id();
213 shortcut_info.is_platform_app = app->is_platform_app();
214 shortcut_info.url = extensions::AppLaunchInfo::GetLaunchWebURL(app);
215 shortcut_info.title = base::UTF8ToUTF16(app->name());
216 shortcut_info.description = base::UTF8ToUTF16(app->description());
217 shortcut_info.extension_path = app->path();
218 shortcut_info.profile_path = profile->GetPath();
219 shortcut_info.profile_name =
220 profile->GetPrefs()->GetString(prefs::kProfileName);
221 return shortcut_info;
224 void GetInfoForApp(const extensions::Extension* extension,
225 Profile* profile,
226 const InfoCallback& callback) {
227 web_app::ShortcutInfo shortcut_info =
228 web_app::ShortcutInfoForExtensionAndProfile(extension, profile);
229 const std::vector<extensions::FileHandlerInfo>* file_handlers =
230 extensions::FileHandlers::GetFileHandlers(extension);
231 extensions::FileHandlersInfo file_handlers_info =
232 file_handlers ? *file_handlers : extensions::FileHandlersInfo();
234 std::vector<extensions::ImageLoader::ImageRepresentation> info_list;
235 for (size_t i = 0; i < kNumDesiredSizes; ++i) {
236 int size = kDesiredSizes[i];
237 extensions::ExtensionResource resource =
238 extensions::IconsInfo::GetIconResource(
239 extension, size, ExtensionIconSet::MATCH_EXACTLY);
240 if (!resource.empty()) {
241 info_list.push_back(extensions::ImageLoader::ImageRepresentation(
242 resource,
243 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
244 gfx::Size(size, size),
245 ui::SCALE_FACTOR_100P));
249 if (info_list.empty()) {
250 size_t i = kNumDesiredSizes - 1;
251 int size = kDesiredSizes[i];
253 // If there is no icon at the desired sizes, we will resize what we can get.
254 // Making a large icon smaller is preferred to making a small icon larger,
255 // so look for a larger icon first:
256 extensions::ExtensionResource resource =
257 extensions::IconsInfo::GetIconResource(
258 extension, size, ExtensionIconSet::MATCH_BIGGER);
259 if (resource.empty()) {
260 resource = extensions::IconsInfo::GetIconResource(
261 extension, size, ExtensionIconSet::MATCH_SMALLER);
263 info_list.push_back(extensions::ImageLoader::ImageRepresentation(
264 resource,
265 extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
266 gfx::Size(size, size),
267 ui::SCALE_FACTOR_100P));
270 // |info_list| may still be empty at this point, in which case
271 // LoadImageFamilyAsync will call the OnImageLoaded callback with an empty
272 // image and exit immediately.
273 extensions::ImageLoader::Get(profile)->LoadImageFamilyAsync(
274 extension,
275 info_list,
276 base::Bind(&OnImageLoaded, shortcut_info, file_handlers_info, callback));
279 void GetShortcutInfoForApp(const extensions::Extension* extension,
280 Profile* profile,
281 const ShortcutInfoCallback& callback) {
282 GetInfoForApp(
283 extension, profile, base::Bind(&IgnoreFileHandlersInfo, callback));
286 bool ShouldCreateShortcutFor(web_app::ShortcutCreationReason reason,
287 Profile* profile,
288 const extensions::Extension* extension) {
289 // Shortcuts should never be created for component apps, or for apps that
290 // cannot be shown in the launcher.
291 if (extension->location() == extensions::Manifest::COMPONENT ||
292 !extensions::ui_util::CanDisplayInAppLauncher(extension, profile)) {
293 return false;
296 // Otherwise, always create shortcuts for v2 packaged apps.
297 if (extension->is_platform_app())
298 return true;
300 #if defined(OS_MACOSX)
301 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
302 switches::kDisableHostedAppShimCreation)) {
303 return extension->is_hosted_app();
306 return false;
307 #else
308 // For other platforms, allow shortcut creation if it was explicitly
309 // requested by the user (i.e. is not automatic).
310 return reason == SHORTCUT_CREATION_BY_USER;
311 #endif
314 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
315 const std::string& extension_id,
316 const GURL& url) {
317 DCHECK(!profile_path.empty());
318 base::FilePath app_data_dir(profile_path.Append(chrome::kWebAppDirname));
320 if (!extension_id.empty()) {
321 return app_data_dir.AppendASCII(
322 GenerateApplicationNameFromExtensionId(extension_id));
325 std::string host(url.host());
326 std::string scheme(url.has_scheme() ? url.scheme() : "http");
327 std::string port(url.has_port() ? url.port() : "80");
328 std::string scheme_port(scheme + "_" + port);
330 #if defined(OS_WIN)
331 base::FilePath::StringType host_path(base::UTF8ToUTF16(host));
332 base::FilePath::StringType scheme_port_path(base::UTF8ToUTF16(scheme_port));
333 #elif defined(OS_POSIX)
334 base::FilePath::StringType host_path(host);
335 base::FilePath::StringType scheme_port_path(scheme_port);
336 #endif
338 return app_data_dir.Append(host_path).Append(scheme_port_path);
341 base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
342 const extensions::Extension& extension) {
343 return GetWebAppDataDirectory(
344 profile_path,
345 extension.id(),
346 GURL(extensions::AppLaunchInfo::GetLaunchWebURL(&extension)));
349 std::string GenerateApplicationNameFromInfo(const ShortcutInfo& shortcut_info) {
350 if (!shortcut_info.extension_id.empty())
351 return GenerateApplicationNameFromExtensionId(shortcut_info.extension_id);
352 else
353 return GenerateApplicationNameFromURL(shortcut_info.url);
356 std::string GenerateApplicationNameFromURL(const GURL& url) {
357 std::string t;
358 t.append(url.host());
359 t.append("_");
360 t.append(url.path());
361 return t;
364 std::string GenerateApplicationNameFromExtensionId(const std::string& id) {
365 std::string t(kCrxAppPrefix);
366 t.append(id);
367 return t;
370 std::string GetExtensionIdFromApplicationName(const std::string& app_name) {
371 std::string prefix(kCrxAppPrefix);
372 if (app_name.substr(0, prefix.length()) != prefix)
373 return std::string();
374 return app_name.substr(prefix.length());
377 void CreateShortcutsWithInfo(
378 ShortcutCreationReason reason,
379 const ShortcutLocations& locations,
380 const ShortcutInfo& shortcut_info,
381 const extensions::FileHandlersInfo& file_handlers_info) {
382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
384 // If the shortcut is for an application shortcut with the new bookmark app
385 // flow disabled, there will be no corresponding extension.
386 if (!shortcut_info.extension_id.empty()) {
387 // It's possible for the extension to be deleted before we get here.
388 // For example, creating a hosted app from a website. Double check that
389 // it still exists.
390 Profile* profile = g_browser_process->profile_manager()->GetProfileByPath(
391 shortcut_info.profile_path);
392 if (!profile)
393 return;
395 extensions::ExtensionRegistry* registry =
396 extensions::ExtensionRegistry::Get(profile);
397 const extensions::Extension* extension = registry->GetExtensionById(
398 shortcut_info.extension_id, extensions::ExtensionRegistry::EVERYTHING);
399 if (!extension)
400 return;
403 ScheduleCreatePlatformShortcut(reason, locations, shortcut_info,
404 file_handlers_info);
407 void CreateNonAppShortcut(const ShortcutLocations& locations,
408 const ShortcutInfo& shortcut_info) {
409 ScheduleCreatePlatformShortcut(SHORTCUT_CREATION_AUTOMATED, locations,
410 shortcut_info, extensions::FileHandlersInfo());
413 void CreateShortcuts(ShortcutCreationReason reason,
414 const ShortcutLocations& locations,
415 Profile* profile,
416 const extensions::Extension* app) {
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
419 if (!ShouldCreateShortcutFor(reason, profile, app))
420 return;
422 GetInfoForApp(
423 app, profile, base::Bind(&CreateShortcutsWithInfo, reason, locations));
426 void DeleteAllShortcuts(Profile* profile, const extensions::Extension* app) {
427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
429 ShortcutInfo shortcut_info =
430 ShortcutInfoForExtensionAndProfile(app, profile);
431 BrowserThread::PostTask(
432 BrowserThread::FILE,
433 FROM_HERE,
434 base::Bind(&web_app::internals::DeletePlatformShortcuts,
435 GetShortcutDataDir(shortcut_info), shortcut_info));
438 void UpdateAllShortcuts(const base::string16& old_app_title,
439 Profile* profile,
440 const extensions::Extension* app) {
441 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
443 GetInfoForApp(app,
444 profile,
445 base::Bind(&UpdateAllShortcutsForShortcutInfo, old_app_title));
448 bool IsValidUrl(const GURL& url) {
449 static const char* const kValidUrlSchemes[] = {
450 url::kFileScheme,
451 url::kFileSystemScheme,
452 url::kFtpScheme,
453 url::kHttpScheme,
454 url::kHttpsScheme,
455 extensions::kExtensionScheme,
458 for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) {
459 if (url.SchemeIs(kValidUrlSchemes[i]))
460 return true;
463 return false;
466 #if defined(TOOLKIT_VIEWS)
467 void GetIconsInfo(const WebApplicationInfo& app_info,
468 IconInfoList* icons) {
469 DCHECK(icons);
471 icons->clear();
472 for (size_t i = 0; i < app_info.icons.size(); ++i) {
473 // We only take square shaped icons (i.e. width == height).
474 if (app_info.icons[i].width == app_info.icons[i].height) {
475 icons->push_back(app_info.icons[i]);
479 std::sort(icons->begin(), icons->end(), &IconPrecedes);
481 #endif
483 #if defined(OS_LINUX)
484 std::string GetWMClassFromAppName(std::string app_name) {
485 base::i18n::ReplaceIllegalCharactersInPath(&app_name, '_');
486 base::TrimString(app_name, "_", &app_name);
487 return app_name;
489 #endif
491 } // namespace web_app