Skip to content
Snippets Groups Projects
Commit 78e5c2d1 authored by aa@chromium.org's avatar aa@chromium.org
Browse files

Revert 64845 - Add convert_web_app.*, a utility to convert web apps declared

in pure HTML into the extension-based web apps used internally
by Chrome.

BUG=49233
TEST=unit_tests --gtest_filter=ExtensionFromWebApp.*

Review URL: http://codereview.chromium.org/4139008

TBR=aa@chromium.org
Review URL: http://codereview.chromium.org/4326002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64851 0039d316-1c4b-4281-b951-d872f2087c98
parent 4d2f5afc
No related branches found
No related tags found
No related merge requests found
......@@ -19,8 +19,6 @@ class GURL;
// should take ownership on success, or NULL and |error| on failure.
//
// NOTE: This function does file IO and should not be called on the UI thread.
// NOTE: The caller takes ownership of the directory at extension->path() on the
// returned object.
scoped_refptr<Extension> ConvertUserScriptToExtension(
const FilePath& user_script, const GURL& original_url, std::string* error);
......
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/convert_web_app.h"
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/sha2.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/json_value_serializer.h"
#include "gfx/codec/png_codec.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "webkit/glue/dom_operations.h"
namespace keys = extension_manifest_keys;
namespace {
const char kIconsDirName[] = "_icons";
// Create the public key for the converted web app.
//
// Web apps are not signed, but the public key for an extension doubles as
// its unique identity, and we need one of those. A web app's unique identity
// is its manifest URL, so we hash that to create a public key. There will be
// no corresponding private key, which means that these extensions cannot be
// auto-updated using ExtensionUpdater. But Chrome does notice updates to the
// manifest and regenerates these extensions.
std::string GenerateKey(const GURL& manifest_url) {
char raw[base::SHA256_LENGTH] = {0};
std::string key;
base::SHA256HashString(manifest_url.spec().c_str(),
raw,
base::SHA256_LENGTH);
base::Base64Encode(std::string(raw, base::SHA256_LENGTH), &key);
return key;
}
}
// Generates a version for the converted app using the current date. This isn't
// really needed, but it seems like useful information.
std::string ConvertTimeToExtensionVersion(const base::Time& create_time) {
base::Time::Exploded create_time_exploded;
create_time.LocalExplode(&create_time_exploded);
uint32 timestamp = static_cast<uint>(create_time.ToDoubleT());
uint16 timestamp_lower = static_cast<uint16>(timestamp);
uint16 timestamp_upper = static_cast<uint16>(timestamp >> 16);
return base::StringPrintf("%i.%i%.2i.%i.%i",
create_time_exploded.year,
create_time_exploded.month,
create_time_exploded.day_of_month,
timestamp_upper,
timestamp_lower);
}
scoped_refptr<Extension> ConvertWebAppToExtension(
const webkit_glue::WebApplicationInfo& web_app,
const base::Time& create_time) {
FilePath user_data_temp_dir;
CHECK(PathService::Get(chrome::DIR_USER_DATA_TEMP, &user_data_temp_dir));
ScopedTempDir temp_dir;
if (!temp_dir.CreateUniqueTempDirUnderPath(user_data_temp_dir)) {
LOG(ERROR) << "Could not create temporary directory.";
return NULL;
}
// Create the manifest
scoped_ptr<DictionaryValue> root(new DictionaryValue);
root->SetString(keys::kPublicKey, GenerateKey(web_app.manifest_url));
root->SetString(keys::kName, UTF16ToUTF8(web_app.title));
root->SetString(keys::kVersion, ConvertTimeToExtensionVersion(create_time));
root->SetString(keys::kDescription, UTF16ToUTF8(web_app.description));
root->SetString(keys::kLaunchWebURL, web_app.app_url.spec());
// Add the icons.
DictionaryValue* icons = new DictionaryValue();
root->Set(keys::kIcons, icons);
for (size_t i = 0; i < web_app.icons.size(); ++i) {
std::string size = StringPrintf("%i", web_app.icons[i].width);
std::string icon_path = StringPrintf("%s/%s.png", kIconsDirName,
size.c_str());
icons->SetString(size, icon_path);
}
// Add the permissions.
ListValue* permissions = new ListValue();
root->Set(keys::kPermissions, permissions);
for (size_t i = 0; i < web_app.permissions.size(); ++i) {
permissions->Append(Value::CreateStringValue(web_app.permissions[i]));
}
// Add the URLs.
ListValue* urls = new ListValue();
root->Set(keys::kWebURLs, urls);
for (size_t i = 0; i < web_app.urls.size(); ++i) {
urls->Append(Value::CreateStringValue(web_app.urls[i].spec()));
}
// Write the manifest.
FilePath manifest_path = temp_dir.path().Append(
Extension::kManifestFilename);
JSONFileValueSerializer serializer(manifest_path);
if (!serializer.Serialize(*root)) {
LOG(ERROR) << "Could not serialize manifest.";
return NULL;
}
// Write the icon files.
FilePath icons_dir = temp_dir.path().AppendASCII(kIconsDirName);
if (!file_util::CreateDirectory(icons_dir)) {
LOG(ERROR) << "Could not create icons directory.";
return NULL;
}
for (size_t i = 0; i < web_app.icons.size(); ++i) {
FilePath icon_file = icons_dir.AppendASCII(
StringPrintf("%i.png", web_app.icons[i].width));
std::vector<unsigned char> image_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(web_app.icons[i].data,
false,
&image_data)) {
LOG(ERROR) << "Could not create icon file.";
return NULL;
}
const char* image_data_ptr = reinterpret_cast<const char*>(&image_data[0]);
if (!file_util::WriteFile(icon_file, image_data_ptr, image_data.size())) {
LOG(ERROR) << "Could not write icon file.";
return NULL;
}
}
// Finally, create the extension object to represent the unpacked directory.
std::string error;
scoped_refptr<Extension> extension = Extension::Create(
temp_dir.path(), Extension::INTERNAL, *root, false, &error);
if (!extension) {
LOG(ERROR) << error;
return NULL;
}
temp_dir.Take(); // The caller takes ownership of the directory.
return extension;
}
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_CONVERT_WEB_APP_H_
#define CHROME_BROWSER_EXTENSIONS_CONVERT_WEB_APP_H_
#pragma once
#include <string>
#include "base/ref_counted.h"
class Extension;
namespace base {
class Time;
}
namespace webkit_glue {
class WebApplicationInfo;
}
// Generates a version number for an extension from a time. The goal is to make
// use of the version number to communicate some useful information. The
// returned version has the format:
// <year>.<month><day>.<upper 16 bits of unix timestamp>.<lower 16 bits>
std::string ConvertTimeToExtensionVersion(const base::Time& time);
// Wraps the specified web app in an extension. The extension is created
// unpacked in the system temp dir. Returns a valid extension that the caller
// should take ownership on success, or NULL and |error| on failure.
//
// NOTE: This function does file IO and should not be called on the UI thread.
// NOTE: The caller takes ownership of the directory at extension->path() on the
// returned object.
scoped_refptr<Extension> ConvertWebAppToExtension(
const webkit_glue::WebApplicationInfo& web_app_info,
const base::Time& create_time);
#endif // CHROME_BROWSER_EXTENSIONS_CONVERT_WEB_APP_H_
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/convert_web_app.h"
#include <string>
#include <vector>
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/stringprintf.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "base/version.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/extensions/url_pattern.h"
#include "gfx/codec/png_codec.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/glue/dom_operations.h"
#include "webkit/glue/image_decoder.h"
namespace {
// Returns an icon info corresponding to a canned icon.
webkit_glue::WebApplicationInfo::IconInfo GetIconInfo(const GURL& url,
int size) {
webkit_glue::WebApplicationInfo::IconInfo result;
FilePath icon_file;
if (!PathService::Get(chrome::DIR_TEST_DATA, &icon_file)) {
ADD_FAILURE() << "Could not get test data directory.";
return result;
}
icon_file = icon_file.AppendASCII("extensions")
.AppendASCII("convert_web_app")
.AppendASCII(StringPrintf("%i.png", size));
result.url = url;
result.width = size;
result.height = size;
std::string icon_data;
if (!file_util::ReadFileToString(icon_file, &icon_data)) {
ADD_FAILURE() << "Could not read test icon.";
return result;
}
webkit_glue::ImageDecoder decoder;
result.data = decoder.Decode(
reinterpret_cast<const unsigned char*>(icon_data.c_str()),
icon_data.size());
EXPECT_FALSE(result.data.isNull()) << "Could not decode test icon.";
return result;
}
base::Time GetTestTime(int year, int month, int day) {
base::Time::Exploded exploded = {0};
exploded.year = year;
exploded.month = month;
exploded.day_of_month = day;
return base::Time::FromLocalExploded(exploded);
}
} // namespace
TEST(ExtensionFromWebApp, GenerateVersion) {
// If the month is less than two digits it isn't zero padded.
EXPECT_EQ("2010.101.19261.43904",
ConvertTimeToExtensionVersion(GetTestTime(2010, 1, 1)));
}
TEST(ExtensionFromWebApp, Basic) {
webkit_glue::WebApplicationInfo web_app;
web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
web_app.title = ASCIIToUTF16("Gearpad");
web_app.description = ASCIIToUTF16("The best text editor in the universe!");
web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
web_app.permissions.push_back("geolocation");
web_app.permissions.push_back("notifications");
web_app.urls.push_back(GURL("http://aaronboodman.com/gearpad/"));
const int sizes[] = {16, 48, 128};
for (size_t i = 0; i < arraysize(sizes); ++i) {
GURL icon_url(web_app.app_url.Resolve(StringPrintf("%i.png", sizes[i])));
web_app.icons.push_back(GetIconInfo(icon_url, sizes[i]));
}
scoped_refptr<Extension> extension = ConvertWebAppToExtension(
web_app, GetTestTime(1978, 12, 11));
ASSERT_TRUE(extension.get());
ScopedTempDir extension_dir;
extension_dir.Set(extension->path());
EXPECT_TRUE(extension->is_app());
EXPECT_TRUE(extension->is_hosted_app());
EXPECT_FALSE(extension->is_packaged_app());
EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
extension->public_key());
EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
EXPECT_EQ("1978.1211.4306.13184", extension->version()->GetString());
EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name());
EXPECT_EQ(UTF16ToUTF8(web_app.description), extension->description());
EXPECT_EQ(web_app.app_url, extension->GetFullLaunchURL());
EXPECT_EQ(2u, extension->api_permissions().size());
EXPECT_TRUE(extension->HasApiPermission("geolocation"));
EXPECT_TRUE(extension->HasApiPermission("notifications"));
ASSERT_EQ(1u, extension->web_extent().patterns().size());
EXPECT_EQ("http://aaronboodman.com/gearpad/*",
extension->web_extent().patterns()[0].GetAsString());
EXPECT_EQ(web_app.icons.size(), extension->icons().map().size());
for (size_t i = 0; i < web_app.icons.size(); ++i) {
EXPECT_EQ(StringPrintf("_icons/%i.png", web_app.icons[i].width),
extension->icons().Get(web_app.icons[i].width,
ExtensionIconSet::MATCH_EXACTLY));
ExtensionResource resource = extension->GetIconResource(
web_app.icons[i].width, ExtensionIconSet::MATCH_EXACTLY);
ASSERT_TRUE(!resource.empty());
EXPECT_TRUE(file_util::PathExists(resource.GetFilePath()));
}
}
TEST(ExtensionFromWebApp, Minimal) {
webkit_glue::WebApplicationInfo web_app;
web_app.manifest_url = GURL("http://aaronboodman.com/gearpad/manifest.json");
web_app.title = ASCIIToUTF16("Gearpad");
web_app.app_url = GURL("http://aaronboodman.com/gearpad/");
scoped_refptr<Extension> extension = ConvertWebAppToExtension(
web_app, GetTestTime(1978, 12, 11));
ASSERT_TRUE(extension.get());
ScopedTempDir extension_dir;
extension_dir.Set(extension->path());
EXPECT_TRUE(extension->is_app());
EXPECT_TRUE(extension->is_hosted_app());
EXPECT_FALSE(extension->is_packaged_app());
EXPECT_EQ("lJqm1+jncOHClAuwif1QxNJKfeV9Fbl9IBZx7FkNwkA=",
extension->public_key());
EXPECT_EQ("ncnbaadanljoanockmphfdkimpdedemj", extension->id());
EXPECT_EQ("1978.1211.4306.13184", extension->version()->GetString());
EXPECT_EQ(UTF16ToUTF8(web_app.title), extension->name());
EXPECT_EQ("", extension->description());
EXPECT_EQ(web_app.app_url, extension->GetFullLaunchURL());
EXPECT_EQ(0u, extension->icons().map().size());
EXPECT_EQ(0u, extension->api_permissions().size());
ASSERT_EQ(1u, extension->web_extent().patterns().size());
EXPECT_EQ("*://aaronboodman.com/*",
extension->web_extent().patterns()[0].GetAsString());
}
......@@ -1448,8 +1448,6 @@
'browser/encoding_menu_controller.h',
'browser/extensions/convert_user_script.cc',
'browser/extensions/convert_user_script.h',
'browser/extensions/convert_web_app.cc',
'browser/extensions/convert_web_app.h',
'browser/extensions/crashed_extension_infobar.cc',
'browser/extensions/crashed_extension_infobar.h',
'browser/extensions/crx_installer.cc',
......
......@@ -1259,7 +1259,6 @@
'browser/download/save_package_unittest.cc',
'browser/encoding_menu_controller_unittest.cc',
'browser/extensions/convert_user_script_unittest.cc',
'browser/extensions/convert_web_app_unittest.cc',
'browser/extensions/default_apps_unittest.cc',
'browser/extensions/extension_icon_manager_unittest.cc',
'browser/extensions/extension_info_map_unittest.cc',
......
......@@ -67,6 +67,7 @@ namespace extension_manifest_keys {
extern const char* kType;
extern const char* kUpdateURL;
extern const char* kVersion;
extern const char* kWebLaunchUrl;
extern const char* kWebURLs;
} // namespace extension_manifest_keys
......
......@@ -10,7 +10,6 @@
#include "gfx/size.h"
#include "googleurl/src/gurl.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace WebKit {
class WebDocument;
......@@ -65,32 +64,23 @@ struct WebApplicationInfo {
GURL url;
int width;
int height;
SkBitmap data;
};
// URL to a manifest that defines the application. If specified, all other
// attributes are derived from this manifest, and the manifest is the unique
// ID of the application.
GURL manifest_url;
// Title of the application.
// Title of the application. This is set from the meta tag whose name is
// 'application-name'.
string16 title;
// Description of the application.
// Description of the application. This is set from the meta tag whose name
// is 'description'.
string16 description;
// The launch URL for the app.
// URL for the app. This is set from the meta tag whose name is
// 'application-url'.
GURL app_url;
// Set of available icons.
// Set of available icons. This is set for all link tags whose rel=icon. Only
// icons that have a non-zero (width and/or height) are added.
std::vector<IconInfo> icons;
// The permissions the app requests. Only supported with manifest-based apps.
std::vector<std::string> permissions;
// Set of URLs that comprise the app. Only supported with manifest-based apps.
// All these must be of the same origin as manifest_url.
std::vector<GURL> urls;
};
// Parses the icon's size attribute as defined in the HTML 5 spec. Returns true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment