summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-18 02:31:04 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-18 02:31:04 +0000
commit5ba5dab9db36b81d2a808313967233d56a4d4b0c (patch)
treee35c41787d1c1ef340c6f0bf2e2a111acbd9cf55
parentb27a4adfce4e9dd35eb6d361e2c6b509fab85f56 (diff)
downloadchromium_src-5ba5dab9db36b81d2a808313967233d56a4d4b0c.zip
chromium_src-5ba5dab9db36b81d2a808313967233d56a4d4b0c.tar.gz
chromium_src-5ba5dab9db36b81d2a808313967233d56a4d4b0c.tar.bz2
Reland http://codereview.chromium.org/4139008, with a different
approach to creating version numbers. The old one had troubles with timezones. This one is simpler and better. BUG=49233 TEST=Covered by unit tests Review URL: http://codereview.chromium.org/4471001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66582 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/time_unittest.cc13
-rw-r--r--chrome/browser/extensions/convert_user_script.h2
-rw-r--r--chrome/browser/extensions/convert_web_app.cc172
-rw-r--r--chrome/browser/extensions/convert_web_app.h43
-rw-r--r--chrome/browser/extensions/convert_web_app_unittest.cc174
-rw-r--r--chrome/chrome_browser.gypi2
-rw-r--r--chrome/chrome_tests.gypi1
-rw-r--r--chrome/common/extensions/extension_constants.h1
8 files changed, 407 insertions, 1 deletions
diff --git a/base/time_unittest.cc b/base/time_unittest.cc
index 21e6f89..6ddf4d3 100644
--- a/base/time_unittest.cc
+++ b/base/time_unittest.cc
@@ -52,6 +52,19 @@ TEST(Time, TimeT) {
EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
}
+TEST(Time, FromExplodedWithMilliseconds) {
+ // Some platform implementations of FromExploded are liable to drop
+ // milliseconds if we aren't careful.
+ Time now = Time::NowFromSystemTime();
+ Time::Exploded exploded1 = {0};
+ now.UTCExplode(&exploded1);
+ exploded1.millisecond = 500;
+ Time time = Time::FromUTCExploded(exploded1);
+ Time::Exploded exploded2 = {0};
+ time.UTCExplode(&exploded2);
+ EXPECT_EQ(exploded1.millisecond, exploded2.millisecond);
+}
+
TEST(Time, ZeroIsSymmetric) {
Time zero_time(Time::FromTimeT(0));
EXPECT_EQ(0, zero_time.ToTimeT());
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..47a9143
--- /dev/null
+++ b/chrome/browser/extensions/convert_web_app.cc
@@ -0,0 +1,172 @@
+// 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 <cmath>
+#include <limits>
+#include <string>
+#include <vector>
+
+#include "base/base64.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/logging.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 "chrome/common/web_apps.h"
+#include "gfx/codec/png_codec.h"
+#include "googleurl/src/gurl.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace keys = extension_manifest_keys;
+
+using base::Time;
+
+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 Time& create_time) {
+ Time::Exploded create_time_exploded;
+ create_time.UTCExplode(&create_time_exploded);
+
+ double micros = static_cast<double>(
+ (create_time_exploded.millisecond * Time::kMicrosecondsPerMillisecond) +
+ (create_time_exploded.second * Time::kMicrosecondsPerSecond) +
+ (create_time_exploded.minute * Time::kMicrosecondsPerMinute) +
+ (create_time_exploded.hour * Time::kMicrosecondsPerHour));
+ double day_fraction = micros / Time::kMicrosecondsPerDay;
+ double stamp = day_fraction * std::numeric_limits<uint16>::max();
+
+ // Ghetto-round, since VC++ doesn't have round().
+ stamp = stamp >= (floor(stamp) + 0.5) ? (stamp + 1) : stamp;
+
+ return base::StringPrintf("%i.%i.%i.%i",
+ create_time_exploded.year,
+ create_time_exploded.month,
+ create_time_exploded.day_of_month,
+ static_cast<uint16>(stamp));
+}
+
+scoped_refptr<Extension> ConvertWebAppToExtension(
+ const WebApplicationInfo& web_app,
+ const 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..4037265
--- /dev/null
+++ b/chrome/browser/extensions/convert_web_app.h
@@ -0,0 +1,43 @@
+// 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;
+}
+
+struct WebApplicationInfo;
+
+// Generates a version number for an extension from a time. The goal is to make
+// use of the version number to communicate the date in a human readable form,
+// while maintaining high enough resolution to change each time an app is
+// reinstalled. The version that is returned has the format:
+//
+// <year>.<month>.<day>.<fraction>
+//
+// fraction is represented as a number between 0 and 2^16-1. Each unit is
+// ~1.32 seconds.
+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 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..fc10347
--- /dev/null
+++ b/chrome/browser/extensions/convert_web_app_unittest.cc
@@ -0,0 +1,174 @@
+// 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 "chrome/common/web_apps.h"
+#include "gfx/codec/png_codec.h"
+#include "googleurl/src/gurl.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webkit/glue/image_decoder.h"
+
+namespace {
+
+// Returns an icon info corresponding to a canned icon.
+WebApplicationInfo::IconInfo GetIconInfo(const GURL& url, int size) {
+ 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, int hour, int minute,
+ int second, int millisecond) {
+ base::Time::Exploded exploded = {0};
+ exploded.year = year;
+ exploded.month = month;
+ exploded.day_of_month = day;
+ exploded.hour = hour;
+ exploded.minute = minute;
+ exploded.second = second;
+ exploded.millisecond = millisecond;
+ return base::Time::FromUTCExploded(exploded);
+}
+
+} // namespace
+
+
+TEST(ExtensionFromWebApp, GenerateVersion) {
+ EXPECT_EQ("2010.1.1.0",
+ ConvertTimeToExtensionVersion(
+ GetTestTime(2010, 1, 1, 0, 0, 0, 0)));
+ EXPECT_EQ("2010.12.31.22111",
+ ConvertTimeToExtensionVersion(
+ GetTestTime(2010, 12, 31, 8, 5, 50, 500)));
+ EXPECT_EQ("2010.10.1.65535",
+ ConvertTimeToExtensionVersion(
+ GetTestTime(2010, 10, 1, 23, 59, 59, 999)));
+}
+
+TEST(ExtensionFromWebApp, Basic) {
+ 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, 0, 0, 0, 0));
+ 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.12.11.0", 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) {
+ 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, 0, 0, 0, 0));
+ 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.12.11.0", 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 2a81da5..7f1fc4b 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -1443,6 +1443,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 67890d6..dd85cdd 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -1264,6 +1264,7 @@
'browser/encoding_menu_controller_unittest.cc',
'browser/enumerate_modules_model_unittest_win.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 51e2299..8c3bee2 100644
--- a/chrome/common/extensions/extension_constants.h
+++ b/chrome/common/extensions/extension_constants.h
@@ -68,7 +68,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