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/extensions/convert_web_app.h"
12 #include "base/base64.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/logging.h"
18 #include "base/numerics/safe_conversions.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/web_application_info.h"
24 #include "crypto/sha2.h"
25 #include "extensions/common/constants.h"
26 #include "extensions/common/extension.h"
27 #include "extensions/common/file_util.h"
28 #include "extensions/common/manifest_constants.h"
29 #include "third_party/skia/include/core/SkBitmap.h"
30 #include "ui/gfx/codec/png_codec.h"
33 namespace extensions {
35 namespace keys = manifest_keys;
41 const char kIconsDirName[] = "icons";
43 // Create the public key for the converted web app.
45 // Web apps are not signed, but the public key for an extension doubles as
46 // its unique identity, and we need one of those. A web app's unique identity
47 // is its manifest URL, so we hash that to create a public key. There will be
48 // no corresponding private key, which means that these extensions cannot be
49 // auto-updated using ExtensionUpdater.
50 std::string GenerateKey(const GURL& app_url) {
51 char raw[crypto::kSHA256Length] = {0};
53 crypto::SHA256HashString(app_url.spec().c_str(), raw,
54 crypto::kSHA256Length);
55 base::Base64Encode(std::string(raw, crypto::kSHA256Length), &key);
61 // Generates a version for the converted app using the current date. This isn't
62 // really needed, but it seems like useful information.
63 std::string ConvertTimeToExtensionVersion(const Time& create_time) {
64 Time::Exploded create_time_exploded;
65 create_time.UTCExplode(&create_time_exploded);
67 double micros = static_cast<double>(
68 (create_time_exploded.millisecond * Time::kMicrosecondsPerMillisecond) +
69 (create_time_exploded.second * Time::kMicrosecondsPerSecond) +
70 (create_time_exploded.minute * Time::kMicrosecondsPerMinute) +
71 (create_time_exploded.hour * Time::kMicrosecondsPerHour));
72 double day_fraction = micros / Time::kMicrosecondsPerDay;
73 double stamp = day_fraction * std::numeric_limits<uint16>::max();
75 // Ghetto-round, since VC++ doesn't have round().
76 stamp = stamp >= (floor(stamp) + 0.5) ? (stamp + 1) : stamp;
78 return base::StringPrintf("%i.%i.%i.%i",
79 create_time_exploded.year,
80 create_time_exploded.month,
81 create_time_exploded.day_of_month,
82 static_cast<uint16>(stamp));
85 scoped_refptr<Extension> ConvertWebAppToExtension(
86 const WebApplicationInfo& web_app,
87 const Time& create_time,
88 const base::FilePath& extensions_dir) {
89 base::FilePath install_temp_dir =
90 file_util::GetInstallTempDir(extensions_dir);
91 if (install_temp_dir.empty()) {
92 LOG(ERROR) << "Could not get path to profile temporary directory.";
96 base::ScopedTempDir temp_dir;
97 if (!temp_dir.CreateUniqueTempDirUnderPath(install_temp_dir)) {
98 LOG(ERROR) << "Could not create temporary directory.";
102 // Create the manifest
103 scoped_ptr<base::DictionaryValue> root(new base::DictionaryValue);
104 root->SetString(keys::kPublicKey, GenerateKey(web_app.app_url));
105 root->SetString(keys::kName, base::UTF16ToUTF8(web_app.title));
106 root->SetString(keys::kVersion, ConvertTimeToExtensionVersion(create_time));
107 root->SetString(keys::kDescription, base::UTF16ToUTF8(web_app.description));
108 root->SetString(keys::kLaunchWebURL, web_app.app_url.spec());
111 base::DictionaryValue* icons = new base::DictionaryValue();
112 root->Set(keys::kIcons, icons);
113 for (size_t i = 0; i < web_app.icons.size(); ++i) {
114 std::string size = base::StringPrintf("%i", web_app.icons[i].width);
115 std::string icon_path = base::StringPrintf("%s/%s.png", kIconsDirName,
117 icons->SetString(size, icon_path);
120 // Write the manifest.
121 base::FilePath manifest_path = temp_dir.path().Append(kManifestFilename);
122 JSONFileValueSerializer serializer(manifest_path);
123 if (!serializer.Serialize(*root)) {
124 LOG(ERROR) << "Could not serialize manifest.";
128 // Write the icon files.
129 base::FilePath icons_dir = temp_dir.path().AppendASCII(kIconsDirName);
130 if (!base::CreateDirectory(icons_dir)) {
131 LOG(ERROR) << "Could not create icons directory.";
134 for (size_t i = 0; i < web_app.icons.size(); ++i) {
135 // Skip unfetched bitmaps.
136 if (web_app.icons[i].data.colorType() == kUnknown_SkColorType)
139 base::FilePath icon_file = icons_dir.AppendASCII(
140 base::StringPrintf("%i.png", web_app.icons[i].width));
141 std::vector<unsigned char> image_data;
142 if (!gfx::PNGCodec::EncodeBGRASkBitmap(web_app.icons[i].data,
145 LOG(ERROR) << "Could not create icon file.";
149 const char* image_data_ptr = reinterpret_cast<const char*>(&image_data[0]);
150 int size = base::checked_cast<int>(image_data.size());
151 if (base::WriteFile(icon_file, image_data_ptr, size) != size) {
152 LOG(ERROR) << "Could not write icon file.";
157 // Finally, create the extension object to represent the unpacked directory.
159 scoped_refptr<Extension> extension = Extension::Create(
163 Extension::FROM_BOOKMARK,
165 if (!extension.get()) {
170 temp_dir.Take(); // The caller takes ownership of the directory.
174 } // namespace extensions