diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-02 23:24:15 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-02 23:24:15 +0000 |
commit | 97310d6ea1ceec457157847c2f8b9b06b0d4581e (patch) | |
tree | 00b9615da74b1d14cf18a82091d920c49c69c62b /chrome | |
parent | e986b89dc90b2848e8c8b711917b880911847ee8 (diff) | |
download | chromium_src-97310d6ea1ceec457157847c2f8b9b06b0d4581e.zip chromium_src-97310d6ea1ceec457157847c2f8b9b06b0d4581e.tar.gz chromium_src-97310d6ea1ceec457157847c2f8b9b06b0d4581e.tar.bz2 |
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
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64845 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/extensions/convert_user_script.h | 2 | ||||
-rw-r--r-- | chrome/browser/extensions/convert_web_app.cc | 161 | ||||
-rw-r--r-- | chrome/browser/extensions/convert_web_app.h | 40 | ||||
-rw-r--r-- | chrome/browser/extensions/convert_web_app_unittest.cc | 164 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 | ||||
-rw-r--r-- | chrome/common/extensions/extension_constants.h | 1 |
7 files changed, 370 insertions, 1 deletions
diff --git a/chrome/browser/extensions/convert_user_script.h b/chrome/browser/extensions/convert_user_script.h index d779de0..678c6eb 100644 --- a/chrome/browser/extensions/convert_user_script.h +++ b/chrome/browser/extensions/convert_user_script.h @@ -19,6 +19,8 @@ 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); diff --git a/chrome/browser/extensions/convert_web_app.cc b/chrome/browser/extensions/convert_web_app.cc new file mode 100644 index 0000000..c0ced47 --- /dev/null +++ b/chrome/browser/extensions/convert_web_app.cc @@ -0,0 +1,161 @@ +// 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; +} diff --git a/chrome/browser/extensions/convert_web_app.h b/chrome/browser/extensions/convert_web_app.h new file mode 100644 index 0000000..676413f --- /dev/null +++ b/chrome/browser/extensions/convert_web_app.h @@ -0,0 +1,40 @@ +// 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_ diff --git a/chrome/browser/extensions/convert_web_app_unittest.cc b/chrome/browser/extensions/convert_web_app_unittest.cc new file mode 100644 index 0000000..8dcab42 --- /dev/null +++ b/chrome/browser/extensions/convert_web_app_unittest.cc @@ -0,0 +1,164 @@ +// 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()); +} diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index e749f9e..ff446e0 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1448,6 +1448,8 @@ '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', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e165f41..c9a9a95 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1259,6 +1259,7 @@ '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', diff --git a/chrome/common/extensions/extension_constants.h b/chrome/common/extensions/extension_constants.h index 332f386..27a2c58 100644 --- a/chrome/common/extensions/extension_constants.h +++ b/chrome/common/extensions/extension_constants.h @@ -67,7 +67,6 @@ 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 |