• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/extensions/convert_web_app.h"
6 
7 #include <cmath>
8 #include <limits>
9 #include <string>
10 #include <vector>
11 
12 #include "base/base64.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.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/path_service.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/time/time.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/web_application_info.h"
25 #include "crypto/sha2.h"
26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/file_util.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "third_party/skia/include/core/SkBitmap.h"
31 #include "ui/gfx/codec/png_codec.h"
32 #include "url/gurl.h"
33 
34 namespace extensions {
35 
36 namespace keys = manifest_keys;
37 
38 using base::Time;
39 
40 namespace {
41 
42 const char kIconsDirName[] = "icons";
43 
44 // Create the public key for the converted web app.
45 //
46 // Web apps are not signed, but the public key for an extension doubles as
47 // its unique identity, and we need one of those. A web app's unique identity
48 // is its manifest URL, so we hash that to create a public key. There will be
49 // no corresponding private key, which means that these extensions cannot be
50 // auto-updated using ExtensionUpdater.
GenerateKey(const GURL & app_url)51 std::string GenerateKey(const GURL& app_url) {
52   char raw[crypto::kSHA256Length] = {0};
53   std::string key;
54   crypto::SHA256HashString(app_url.spec().c_str(), raw,
55                            crypto::kSHA256Length);
56   base::Base64Encode(std::string(raw, crypto::kSHA256Length), &key);
57   return key;
58 }
59 
60 }  // namespace
61 
62 // Generates a version for the converted app using the current date. This isn't
63 // really needed, but it seems like useful information.
ConvertTimeToExtensionVersion(const Time & create_time)64 std::string ConvertTimeToExtensionVersion(const Time& create_time) {
65   Time::Exploded create_time_exploded;
66   create_time.UTCExplode(&create_time_exploded);
67 
68   double micros = static_cast<double>(
69       (create_time_exploded.millisecond * Time::kMicrosecondsPerMillisecond) +
70       (create_time_exploded.second * Time::kMicrosecondsPerSecond) +
71       (create_time_exploded.minute * Time::kMicrosecondsPerMinute) +
72       (create_time_exploded.hour * Time::kMicrosecondsPerHour));
73   double day_fraction = micros / Time::kMicrosecondsPerDay;
74   double stamp = day_fraction * std::numeric_limits<uint16>::max();
75 
76   // Ghetto-round, since VC++ doesn't have round().
77   stamp = stamp >= (floor(stamp) + 0.5) ? (stamp + 1) : stamp;
78 
79   return base::StringPrintf("%i.%i.%i.%i",
80                             create_time_exploded.year,
81                             create_time_exploded.month,
82                             create_time_exploded.day_of_month,
83                             static_cast<uint16>(stamp));
84 }
85 
ConvertWebAppToExtension(const WebApplicationInfo & web_app,const Time & create_time,const base::FilePath & extensions_dir)86 scoped_refptr<Extension> ConvertWebAppToExtension(
87     const WebApplicationInfo& web_app,
88     const Time& create_time,
89     const base::FilePath& extensions_dir) {
90   base::FilePath install_temp_dir =
91       file_util::GetInstallTempDir(extensions_dir);
92   if (install_temp_dir.empty()) {
93     LOG(ERROR) << "Could not get path to profile temporary directory.";
94     return NULL;
95   }
96 
97   base::ScopedTempDir temp_dir;
98   if (!temp_dir.CreateUniqueTempDirUnderPath(install_temp_dir)) {
99     LOG(ERROR) << "Could not create temporary directory.";
100     return NULL;
101   }
102 
103   // Create the manifest
104   scoped_ptr<base::DictionaryValue> root(new base::DictionaryValue);
105   root->SetString(keys::kPublicKey, GenerateKey(web_app.app_url));
106   root->SetString(keys::kName, base::UTF16ToUTF8(web_app.title));
107   root->SetString(keys::kVersion, ConvertTimeToExtensionVersion(create_time));
108   root->SetString(keys::kDescription, base::UTF16ToUTF8(web_app.description));
109   root->SetString(keys::kLaunchWebURL, web_app.app_url.spec());
110 
111   // Add the icons.
112   base::DictionaryValue* icons = new base::DictionaryValue();
113   root->Set(keys::kIcons, icons);
114   for (size_t i = 0; i < web_app.icons.size(); ++i) {
115     std::string size = base::StringPrintf("%i", web_app.icons[i].width);
116     std::string icon_path = base::StringPrintf("%s/%s.png", kIconsDirName,
117                                                size.c_str());
118     icons->SetString(size, icon_path);
119   }
120 
121   // Write the manifest.
122   base::FilePath manifest_path = temp_dir.path().Append(kManifestFilename);
123   JSONFileValueSerializer serializer(manifest_path);
124   if (!serializer.Serialize(*root)) {
125     LOG(ERROR) << "Could not serialize manifest.";
126     return NULL;
127   }
128 
129   // Write the icon files.
130   base::FilePath icons_dir = temp_dir.path().AppendASCII(kIconsDirName);
131   if (!base::CreateDirectory(icons_dir)) {
132     LOG(ERROR) << "Could not create icons directory.";
133     return NULL;
134   }
135   for (size_t i = 0; i < web_app.icons.size(); ++i) {
136     // Skip unfetched bitmaps.
137     if (web_app.icons[i].data.config() == SkBitmap::kNo_Config)
138       continue;
139 
140     base::FilePath icon_file = icons_dir.AppendASCII(
141         base::StringPrintf("%i.png", web_app.icons[i].width));
142     std::vector<unsigned char> image_data;
143     if (!gfx::PNGCodec::EncodeBGRASkBitmap(web_app.icons[i].data,
144                                            false,
145                                            &image_data)) {
146       LOG(ERROR) << "Could not create icon file.";
147       return NULL;
148     }
149 
150     const char* image_data_ptr = reinterpret_cast<const char*>(&image_data[0]);
151     int size = base::checked_cast<int>(image_data.size());
152     if (base::WriteFile(icon_file, image_data_ptr, size) != size) {
153       LOG(ERROR) << "Could not write icon file.";
154       return NULL;
155     }
156   }
157 
158   // Finally, create the extension object to represent the unpacked directory.
159   std::string error;
160   scoped_refptr<Extension> extension = Extension::Create(
161       temp_dir.path(),
162       Manifest::INTERNAL,
163       *root,
164       Extension::FROM_BOOKMARK,
165       &error);
166   if (!extension.get()) {
167     LOG(ERROR) << error;
168     return NULL;
169   }
170 
171   temp_dir.Take();  // The caller takes ownership of the directory.
172   return extension;
173 }
174 
175 }  // namespace extensions
176