Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: chrome/browser/android/webapk/webapk_builder.cc

Issue 2138973002: Initial CL for talking to the WebAPK server to generate WebAPK (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into webapk_builder_impl2 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
Robert Sesek 2016/07/25 23:01:33 What about unittests for this file, to cover the v
pkotwicz 2016/07/26 17:54:46 I am confused as to what you want unittested. Do
Robert Sesek 2016/07/26 21:15:50 This class should have tests was the larger point
pkotwicz 2016/07/27 02:50:22 The bigger issue is: "After a WebAPK is installed,
Robert Sesek 2016/07/27 15:37:09 This class can finish its operation in four unique
pkotwicz 2016/07/28 18:29:51 I have added integration tests in webapk_installer
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/android/webapk/webapk_builder.h"
6
7 #include "base/android/build_info.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/path_utils.h"
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/md5.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "chrome/browser/android/webapk/webapk.pb.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "components/version_info/version_info.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/common/manifest_util.h"
25 #include "jni/WebApkBuilder_jni.h"
26 #include "net/http/http_status_code.h"
27 #include "net/url_request/url_fetcher.h"
28 #include "ui/gfx/codec/png_codec.h"
29 #include "url/gurl.h"
30
31 namespace {
32
33 // The default WebAPK server URL.
34 const char kDefaultWebApkServerUrl[] = "https://webapk.googleapis.com/v1alpha/we bApks?alt=proto";
Robert Sesek 2016/07/25 23:01:33 nit: 80 cols, break after =
pkotwicz 2016/07/26 17:54:46 Done.
35
36 // The MIME type of the POST data sent to the server.
37 const char kProtoMimeType[] = "application/x-protobuf";
38
39 // The number of milliseconds to wait for the WebAPK download URL from the
40 // WebAPK server.
41 const int kWebApkDownloadUrlTimeoutMs = 4000;
42
43 // The number of milliseconds to wait for the WebAPK download to complete.
44 const int kDownloadTimeoutMs = 20000;
45
46 // Returns the scope from |info| if it is specified. Otherwise, returns the
47 // default scope.
48 GURL GetScope(const ShortcutInfo& info) {
49 return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin();
50 }
51
52 // Computes a MD5 hash of |bitmap|'s PNG encoded bytes.
53 std::string ComputeBitmapHash(const SkBitmap& bitmap) {
54 std::vector<unsigned char> png_bytes;
55 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes);
56 base::MD5Digest digest;
Robert Sesek 2016/07/25 23:01:33 Why MD5?
pkotwicz 2016/07/26 17:54:46 I chose MD5 because we use the hash in many places
Robert Sesek 2016/07/26 22:33:26 MD5 is compromised and there are several attacks a
Robert Sesek 2016/07/26 22:37:30 More background: https://goto.google.com/rtmye
pkotwicz 2016/07/27 02:50:22 Sorry, my answer was incomplete The WebAPK server
Robert Sesek 2016/07/27 15:37:09 With MD5 in this code, you're currently using a cr
pkotwicz 2016/07/28 18:29:51 Switched to Murmur2 instead
57 base::MD5Sum(&png_bytes.front(), png_bytes.size(), &digest);
58 return base::MD5DigestToBase16(digest);
59 }
60
61 // Converts a color from the format specified in content::Manifest to a CSS
62 // string.
63 std::string ColorToString(int64_t color) {
Robert Sesek 2016/07/25 23:01:33 I'm surprised there isn't a function already to do
pkotwicz 2016/07/26 17:54:46 I don't see anything in ui/gfx/color_utils.h or Sk
64 if (color == content::Manifest::kInvalidOrMissingColor)
65 return "";
66
67 SkColor sk_color = reinterpret_cast<uint32_t&>(color);
Robert Sesek 2016/07/25 23:01:33 Why reinterpret this as a reference?
pkotwicz 2016/07/26 17:54:46 I believe this the only way of using reinterpret c
Robert Sesek 2016/07/26 21:15:51 You can just mask the lower 32 bits if you want a
pkotwicz 2016/07/27 02:50:22 How would I convert from a signed 32 bit integer t
Robert Sesek 2016/07/27 15:37:09 I see. Why put this in a signed int32 at all then?
pkotwicz 2016/07/28 18:29:51 I am unsure what you are suggesting. Are you sugge
Robert Sesek 2016/07/29 17:38:05 I'm trying to understand why a signed int32 was us
pkotwicz 2016/07/29 17:51:39 I am not sure why this decision was made. I suspec
68 int r = SkColorGetR(sk_color);
69 int g = SkColorGetG(sk_color);
70 int b = SkColorGetB(sk_color);
71 double a = SkColorGetA(sk_color) / 255.0;
72 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a);
73 }
74
75 } // anonymous namespace
76
77 WebApkBuilder::WebApkBuilder(content::BrowserContext* browser_context,
78 const ShortcutInfo& shortcut_info,
79 const SkBitmap& shortcut_icon)
80 : browser_context_(browser_context),
81 shortcut_info_(shortcut_info),
82 shortcut_icon_(shortcut_icon),
83 io_weak_ptr_factory_(this) {
84 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
85 server_url_ =
86 GURL(command_line->HasSwitch(switches::kWebApkServerUrl)
87 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl)
88 : kDefaultWebApkServerUrl);
89 }
90
91 WebApkBuilder::~WebApkBuilder() {}
92
93 // static
94 bool WebApkBuilder::Register(JNIEnv* env) {
95 return RegisterNativesImpl(env);
96 }
97
98 void WebApkBuilder::BuildAsync(const FinishCallback& finish_callback) {
99 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
100 finish_callback_ = finish_callback;
101 // base::Unretained() is safe because WebApkBuilder owns itself and does not
102 // start the timeout timer till after
103 // InitializeRequestContextGetterOnUIThread() is called.
104 content::BrowserThread::PostTask(
105 content::BrowserThread::UI, FROM_HERE,
106 base::Bind(&WebApkBuilder::InitializeRequestContextGetterOnUIThread,
107 base::Unretained(this)));
108 }
109
110 void WebApkBuilder::OnURLFetchComplete(const net::URLFetcher* source) {
111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
112 timer_.Stop();
113
114 if (!source->GetStatus().is_success() ||
115 source->GetResponseCode() != net::HTTP_OK) {
116 OnFailure();
117 return;
118 }
119
120 std::string response_string;
121 source->GetResponseAsString(&response_string);
122
123 std::unique_ptr<webapk::CreateWebApkResponse> response(
124 new webapk::CreateWebApkResponse);
125 if (!response->ParseFromString(response_string)) {
126 OnFailure();
127 return;
128 }
129
130 if (response->signed_download_url().empty() ||
131 response->webapk_package_name().empty()) {
132 OnFailure();
133 return;
134 }
135 OnGotWebApkDownloadUrl(response->signed_download_url(),
136 response->webapk_package_name());
137 }
138
139 void WebApkBuilder::InitializeRequestContextGetterOnUIThread() {
140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
141 // Must be called on UI thread.
142 request_context_getter_ =
143 Profile::FromBrowserContext(browser_context_)->GetRequestContext();
144
145 content::BrowserThread::PostTask(
146 content::BrowserThread::IO, FROM_HERE,
147 base::Bind(&WebApkBuilder::SendCreateWebApkRequest,
148 io_weak_ptr_factory_.GetWeakPtr()));
149 }
150
151 void WebApkBuilder::SendCreateWebApkRequest() {
152 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
153 std::unique_ptr<webapk::CreateWebApkRequest> request =
154 BuildCreateWebApkRequest();
155
156 timer_.Start(
157 FROM_HERE, base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs),
158 base::Bind(&WebApkBuilder::OnTimeout, io_weak_ptr_factory_.GetWeakPtr()));
159
160 url_fetcher_ =
161 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this);
162 url_fetcher_->SetRequestContext(request_context_getter_);
163 std::string serialized_request;
164 request->SerializeToString(&serialized_request);
165 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request);
166 url_fetcher_->Start();
167 }
168
169 void WebApkBuilder::OnGotWebApkDownloadUrl(const std::string& download_url,
170 const std::string& package_name) {
171 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
172
173 base::FilePath output_dir;
174 base::android::GetCacheDirectory(&output_dir);
175 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory
176 // directory.
177
178 timer_.Start(
179 FROM_HERE, base::TimeDelta::FromMilliseconds(kDownloadTimeoutMs),
180 base::Bind(&WebApkBuilder::OnTimeout, io_weak_ptr_factory_.GetWeakPtr()));
181
182 base::FilePath output_path = output_dir.AppendASCII(package_name);
183 downloader_.reset(new FileDownloader(
184 GURL(download_url), output_path, true, request_context_getter_,
185 base::Bind(&WebApkBuilder::OnWebApkDownloaded,
186 io_weak_ptr_factory_.GetWeakPtr(), output_path,
187 package_name)));
188 }
189
190 void WebApkBuilder::OnWebApkDownloaded(const base::FilePath& file_path,
191 const std::string& package_name,
192 FileDownloader::Result result) {
193 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
194
195 timer_.Stop();
196
197 if (result != FileDownloader::DOWNLOADED) {
198 OnFailure();
199 return;
200 }
201
202 JNIEnv* env = base::android::AttachCurrentThread();
203 base::android::ScopedJavaLocalRef<jstring> java_file_path =
204 base::android::ConvertUTF8ToJavaString(env, file_path.value());
205 base::android::ScopedJavaLocalRef<jstring> java_package_name =
206 base::android::ConvertUTF8ToJavaString(env, package_name);
207 bool success = Java_WebApkBuilder_installAsync(env, java_file_path.obj(),
208 java_package_name.obj());
209 if (success)
210 OnSuccess();
211 else
212 OnFailure();
213 }
214
215 std::unique_ptr<webapk::CreateWebApkRequest>
216 WebApkBuilder::BuildCreateWebApkRequest() {
217 std::unique_ptr<webapk::CreateWebApkRequest> request(
218 new webapk::CreateWebApkRequest);
219
220 webapk::WebApk* webapk = request->mutable_webapk();
221 webapk->set_manifest_url(shortcut_info_.manifest_url.spec());
222 webapk->set_requester_application_package(
223 base::android::BuildInfo::GetInstance()->package_name());
224 webapk->set_requester_application_version(version_info::GetVersionNumber());
225
226 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest();
227 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name));
228 web_app_manifest->set_short_name(
229 base::UTF16ToUTF8(shortcut_info_.short_name));
230 web_app_manifest->set_start_url(shortcut_info_.url.spec());
231 web_app_manifest->set_orientation(
232 content::WebScreenOrientationLockTypeToString(
233 shortcut_info_.orientation));
234 web_app_manifest->set_display_mode(
235 content::WebDisplayModeToString(shortcut_info_.display));
236 web_app_manifest->set_background_color(
237 ColorToString(shortcut_info_.background_color));
238 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color));
239
240 std::string* scope = web_app_manifest->add_scopes();
241 scope->assign(GetScope(shortcut_info_).spec());
242 webapk::Image* image = web_app_manifest->add_icons();
243 image->set_src(shortcut_info_.icon_url.spec());
244 // TODO(pkotwicz): Get MD5 hash of untransformed icon's bytes (with no
245 // encoding/decoding).
246 image->set_hash(ComputeBitmapHash(shortcut_icon_));
247 std::vector<unsigned char> png_bytes;
248 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes);
249 image->set_image_data(&png_bytes.front(), png_bytes.size());
250
251 return request;
252 }
253
254 void WebApkBuilder::OnTimeout() {
255 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
256 OnFailure();
257 }
258
259 void WebApkBuilder::OnSuccess() {
260 finish_callback_.Run(true);
261 delete this;
262 }
263
264 void WebApkBuilder::OnFailure() {
265 finish_callback_.Run(false);
266 delete this;
267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698