This source file includes following definitions.
- IconPrecedes
- DeleteShortcutsOnFileThread
- UpdateShortcutsOnFileThread
- OnImageLoaded
- GetSanitizedFileName
- GetShortcutInfoForTab
- UpdateShortcutForTabContents
- ShortcutInfoForExtensionAndProfile
- UpdateShortcutInfoAndIconForApp
- GetWebAppDataDirectory
- GetWebAppDataDirectory
- GenerateApplicationNameFromInfo
- GenerateApplicationNameFromURL
- GenerateApplicationNameFromExtensionId
- GetExtensionIdFromApplicationName
- CreateShortcuts
- DeleteAllShortcuts
- UpdateAllShortcuts
- CreateShortcutsOnFileThread
- IsValidUrl
- GetIconsInfo
- GetWMClassFromAppName
#include "chrome/browser/web_applications/web_app.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/i18n/file_util_icu.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "chrome/browser/extensions/image_loader.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/common/extensions/manifest_handlers/icons_handler.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "grit/theme_resources.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/image/image_skia.h"
#if defined(OS_WIN)
#include "ui/gfx/icon_util.h"
#endif
using content::BrowserThread;
namespace {
#if defined(OS_MACOSX)
const int kDesiredSizes[] = {16, 32, 128, 256, 512};
const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
#elif defined(OS_LINUX)
const int kDesiredSizes[] = {16, 32, 48, 128, 256, 512};
const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
#elif defined(OS_WIN)
const int* kDesiredSizes = IconUtil::kIconDimensions;
const size_t kNumDesiredSizes = IconUtil::kNumIconDimensions;
#else
const int kDesiredSizes[] = {32};
const size_t kNumDesiredSizes = arraysize(kDesiredSizes);
#endif
#if defined(TOOLKIT_VIEWS)
bool IconPrecedes(const WebApplicationInfo::IconInfo& left,
const WebApplicationInfo::IconInfo& right) {
return left.width < right.width;
}
#endif
void DeleteShortcutsOnFileThread(
const ShellIntegration::ShortcutInfo& shortcut_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath shortcut_data_dir = web_app::GetWebAppDataDirectory(
shortcut_info.profile_path, shortcut_info.extension_id, GURL());
return web_app::internals::DeletePlatformShortcuts(
shortcut_data_dir, shortcut_info);
}
void UpdateShortcutsOnFileThread(
const base::string16& old_app_title,
const ShellIntegration::ShortcutInfo& shortcut_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath shortcut_data_dir = web_app::GetWebAppDataDirectory(
shortcut_info.profile_path, shortcut_info.extension_id, GURL());
return web_app::internals::UpdatePlatformShortcuts(
shortcut_data_dir, old_app_title, shortcut_info);
}
void OnImageLoaded(ShellIntegration::ShortcutInfo shortcut_info,
web_app::ShortcutInfoCallback callback,
const gfx::ImageFamily& image_family) {
if (image_family.empty()) {
gfx::Image default_icon =
ResourceBundle::GetSharedInstance().GetImageNamed(IDR_APP_DEFAULT_ICON);
int size = kDesiredSizes[kNumDesiredSizes - 1];
SkBitmap bmp = skia::ImageOperations::Resize(
*default_icon.ToSkBitmap(), skia::ImageOperations::RESIZE_BEST,
size, size);
gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(bmp);
image_skia.MakeThreadSafe();
shortcut_info.favicon.Add(gfx::Image(image_skia));
} else {
shortcut_info.favicon = image_family;
}
callback.Run(shortcut_info);
}
}
namespace web_app {
static const char* kCrxAppPrefix = "_crx_";
namespace internals {
base::FilePath GetSanitizedFileName(const base::string16& name) {
#if defined(OS_WIN)
base::string16 file_name = name;
#else
std::string file_name = base::UTF16ToUTF8(name);
#endif
file_util::ReplaceIllegalCharactersInPath(&file_name, '_');
return base::FilePath(file_name);
}
}
void GetShortcutInfoForTab(content::WebContents* web_contents,
ShellIntegration::ShortcutInfo* info) {
DCHECK(info);
const FaviconTabHelper* favicon_tab_helper =
FaviconTabHelper::FromWebContents(web_contents);
const extensions::TabHelper* extensions_tab_helper =
extensions::TabHelper::FromWebContents(web_contents);
const WebApplicationInfo& app_info = extensions_tab_helper->web_app_info();
info->url = app_info.app_url.is_empty() ? web_contents->GetURL() :
app_info.app_url;
info->title = app_info.title.empty() ?
(web_contents->GetTitle().empty() ? base::UTF8ToUTF16(info->url.spec()) :
web_contents->GetTitle()) :
app_info.title;
info->description = app_info.description;
info->favicon.Add(favicon_tab_helper->GetFavicon());
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
info->profile_path = profile->GetPath();
}
#if !defined(OS_WIN)
void UpdateShortcutForTabContents(content::WebContents* web_contents) {}
#endif
ShellIntegration::ShortcutInfo ShortcutInfoForExtensionAndProfile(
const extensions::Extension* app, Profile* profile) {
ShellIntegration::ShortcutInfo shortcut_info;
shortcut_info.extension_id = app->id();
shortcut_info.is_platform_app = app->is_platform_app();
shortcut_info.url = extensions::AppLaunchInfo::GetLaunchWebURL(app);
shortcut_info.title = base::UTF8ToUTF16(app->name());
shortcut_info.description = base::UTF8ToUTF16(app->description());
shortcut_info.extension_path = app->path();
shortcut_info.profile_path = profile->GetPath();
shortcut_info.profile_name =
profile->GetPrefs()->GetString(prefs::kProfileName);
return shortcut_info;
}
void UpdateShortcutInfoAndIconForApp(
const extensions::Extension* extension,
Profile* profile,
const web_app::ShortcutInfoCallback& callback) {
ShellIntegration::ShortcutInfo shortcut_info =
ShortcutInfoForExtensionAndProfile(extension, profile);
std::vector<extensions::ImageLoader::ImageRepresentation> info_list;
for (size_t i = 0; i < kNumDesiredSizes; ++i) {
int size = kDesiredSizes[i];
extensions::ExtensionResource resource =
extensions::IconsInfo::GetIconResource(
extension, size, ExtensionIconSet::MATCH_EXACTLY);
if (!resource.empty()) {
info_list.push_back(extensions::ImageLoader::ImageRepresentation(
resource,
extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
gfx::Size(size, size),
ui::SCALE_FACTOR_100P));
}
}
if (info_list.empty()) {
size_t i = kNumDesiredSizes - 1;
int size = kDesiredSizes[i];
extensions::ExtensionResource resource =
extensions::IconsInfo::GetIconResource(
extension, size, ExtensionIconSet::MATCH_BIGGER);
if (resource.empty()) {
resource = extensions::IconsInfo::GetIconResource(
extension, size, ExtensionIconSet::MATCH_SMALLER);
}
info_list.push_back(extensions::ImageLoader::ImageRepresentation(
resource,
extensions::ImageLoader::ImageRepresentation::ALWAYS_RESIZE,
gfx::Size(size, size),
ui::SCALE_FACTOR_100P));
}
extensions::ImageLoader::Get(profile)->LoadImageFamilyAsync(
extension,
info_list,
base::Bind(&OnImageLoaded, shortcut_info, callback));
}
base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
const std::string& extension_id,
const GURL& url) {
DCHECK(!profile_path.empty());
base::FilePath app_data_dir(profile_path.Append(chrome::kWebAppDirname));
if (!extension_id.empty()) {
return app_data_dir.AppendASCII(
GenerateApplicationNameFromExtensionId(extension_id));
}
std::string host(url.host());
std::string scheme(url.has_scheme() ? url.scheme() : "http");
std::string port(url.has_port() ? url.port() : "80");
std::string scheme_port(scheme + "_" + port);
#if defined(OS_WIN)
base::FilePath::StringType host_path(base::UTF8ToUTF16(host));
base::FilePath::StringType scheme_port_path(base::UTF8ToUTF16(scheme_port));
#elif defined(OS_POSIX)
base::FilePath::StringType host_path(host);
base::FilePath::StringType scheme_port_path(scheme_port);
#endif
return app_data_dir.Append(host_path).Append(scheme_port_path);
}
base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
const extensions::Extension& extension) {
return GetWebAppDataDirectory(
profile_path,
extension.id(),
GURL(extensions::AppLaunchInfo::GetLaunchWebURL(&extension)));
}
std::string GenerateApplicationNameFromInfo(
const ShellIntegration::ShortcutInfo& shortcut_info) {
if (!shortcut_info.extension_id.empty()) {
return web_app::GenerateApplicationNameFromExtensionId(
shortcut_info.extension_id);
} else {
return web_app::GenerateApplicationNameFromURL(
shortcut_info.url);
}
}
std::string GenerateApplicationNameFromURL(const GURL& url) {
std::string t;
t.append(url.host());
t.append("_");
t.append(url.path());
return t;
}
std::string GenerateApplicationNameFromExtensionId(const std::string& id) {
std::string t(web_app::kCrxAppPrefix);
t.append(id);
return t;
}
std::string GetExtensionIdFromApplicationName(const std::string& app_name) {
std::string prefix(kCrxAppPrefix);
if (app_name.substr(0, prefix.length()) != prefix)
return std::string();
return app_name.substr(prefix.length());
}
void CreateShortcuts(
const ShellIntegration::ShortcutInfo& shortcut_info,
const ShellIntegration::ShortcutLocations& creation_locations,
ShortcutCreationReason creation_reason) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(base::IgnoreResult(&CreateShortcutsOnFileThread),
shortcut_info, creation_locations, creation_reason));
}
void DeleteAllShortcuts(const ShellIntegration::ShortcutInfo& shortcut_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&DeleteShortcutsOnFileThread, shortcut_info));
}
void UpdateAllShortcuts(const base::string16& old_app_title,
const ShellIntegration::ShortcutInfo& shortcut_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(&UpdateShortcutsOnFileThread, old_app_title, shortcut_info));
}
bool CreateShortcutsOnFileThread(
const ShellIntegration::ShortcutInfo& shortcut_info,
const ShellIntegration::ShortcutLocations& creation_locations,
ShortcutCreationReason creation_reason) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath shortcut_data_dir = GetWebAppDataDirectory(
shortcut_info.profile_path, shortcut_info.extension_id,
shortcut_info.url);
return internals::CreatePlatformShortcuts(shortcut_data_dir, shortcut_info,
creation_locations,
creation_reason);
}
bool IsValidUrl(const GURL& url) {
static const char* const kValidUrlSchemes[] = {
content::kFileScheme,
content::kFileSystemScheme,
content::kFtpScheme,
content::kHttpScheme,
content::kHttpsScheme,
extensions::kExtensionScheme,
};
for (size_t i = 0; i < arraysize(kValidUrlSchemes); ++i) {
if (url.SchemeIs(kValidUrlSchemes[i]))
return true;
}
return false;
}
#if defined(TOOLKIT_VIEWS)
void GetIconsInfo(const WebApplicationInfo& app_info,
IconInfoList* icons) {
DCHECK(icons);
icons->clear();
for (size_t i = 0; i < app_info.icons.size(); ++i) {
if (app_info.icons[i].width == app_info.icons[i].height) {
icons->push_back(app_info.icons[i]);
}
}
std::sort(icons->begin(), icons->end(), &IconPrecedes);
}
#endif
#if defined(OS_LINUX)
std::string GetWMClassFromAppName(std::string app_name) {
file_util::ReplaceIllegalCharactersInPath(&app_name, '_');
base::TrimString(app_name, "_", &app_name);
return app_name;
}
#endif
}