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

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

Issue 2943913002: [Android WebAPK] Make webapk_installer.cc return proto as base64 string
Patch Set: Merge branch 'background_updates0_5' into background_updates00 Created 3 years, 6 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/webapk/webapk_installer.h" 5 #include "chrome/browser/android/webapk/webapk_installer.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector>
8 9
9 #include "base/android/build_info.h" 10 #include "base/android/build_info.h"
10 #include "base/android/jni_android.h" 11 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
12 #include "base/android/path_utils.h" 13 #include "base/android/path_utils.h"
14 #include "base/base64.h"
13 #include "base/bind.h" 15 #include "base/bind.h"
14 #include "base/command_line.h" 16 #include "base/command_line.h"
15 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
17 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
21 #include "base/task_runner_util.h" 23 #include "base/task_runner_util.h"
22 #include "base/task_scheduler/post_task.h" 24 #include "base/task_scheduler/post_task.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 106
105 // Populates the webapk::Image::image_data field of |image| with |icon|. 107 // Populates the webapk::Image::image_data field of |image| with |icon|.
106 void SetImageData(webapk::Image* image, const SkBitmap& icon) { 108 void SetImageData(webapk::Image* image, const SkBitmap& icon) {
107 std::vector<unsigned char> png_bytes; 109 std::vector<unsigned char> png_bytes;
108 gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &png_bytes); 110 gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &png_bytes);
109 image->set_image_data(&png_bytes.front(), png_bytes.size()); 111 image->set_image_data(&png_bytes.front(), png_bytes.size());
110 } 112 }
111 113
112 // Populates webapk::WebApk and returns it. 114 // Populates webapk::WebApk and returns it.
113 // Must be called on a worker thread because it encodes an SkBitmap. 115 // Must be called on a worker thread because it encodes an SkBitmap.
114 std::unique_ptr<std::vector<uint8_t>> BuildProtoInBackground( 116 std::unique_ptr<std::string> BuildProtoInBackground(
115 const ShortcutInfo& shortcut_info, 117 const ShortcutInfo& shortcut_info,
116 const SkBitmap& primary_icon, 118 const SkBitmap& primary_icon,
117 const SkBitmap& badge_icon, 119 const SkBitmap& badge_icon,
118 const std::string& package_name, 120 const std::string& package_name,
119 const std::string& version, 121 const std::string& version,
120 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 122 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
121 bool is_manifest_stale) { 123 bool is_manifest_stale) {
122 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); 124 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk);
123 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); 125 webapk->set_manifest_url(shortcut_info.manifest_url.spec());
124 webapk->set_requester_application_package( 126 webapk->set_requester_application_package(
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 if (shortcut_info.best_badge_icon_url != 169 if (shortcut_info.best_badge_icon_url !=
168 shortcut_info.best_primary_icon_url) { 170 shortcut_info.best_primary_icon_url) {
169 SetImageData(image, badge_icon); 171 SetImageData(image, badge_icon);
170 } 172 }
171 image->add_usages(webapk::Image::BADGE_ICON); 173 image->add_usages(webapk::Image::BADGE_ICON);
172 } 174 }
173 image->set_src(entry.first); 175 image->set_src(entry.first);
174 image->set_hash(entry.second); 176 image->set_hash(entry.second);
175 } 177 }
176 178
177 size_t serialized_size = webapk->ByteSize(); 179 std::string serialized_proto;
dominickn 2017/06/20 01:07:21 Base64Encode can be run in place, which will save
pkotwicz 2017/06/20 22:18:26 I can do this change. However, this does not decre
dominickn 2017/06/21 04:13:49 I meant that you'll save one of |serialized_proto|
178 std::unique_ptr<std::vector<uint8_t>> serialized_proto = 180 webapk->SerializeToString(&serialized_proto);
179 base::MakeUnique<std::vector<uint8_t>>(); 181 std::unique_ptr<std::string> base64_serialized_proto =
180 serialized_proto->resize(serialized_size); 182 base::MakeUnique<std::string>();
181 webapk->SerializeToArray(serialized_proto->data(), serialized_size); 183 base::Base64Encode(serialized_proto, base64_serialized_proto.get());
182 return serialized_proto; 184 return base64_serialized_proto;
183 } 185 }
184 186
185 // Returns task runner for running background tasks. 187 // Returns task runner for running background tasks.
186 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { 188 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() {
187 return base::CreateTaskRunnerWithTraits( 189 return base::CreateTaskRunnerWithTraits(
188 {base::MayBlock(), base::TaskPriority::BACKGROUND, 190 {base::MayBlock(), base::TaskPriority::BACKGROUND,
189 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); 191 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
190 } 192 }
191 193
192 } // anonymous namespace 194 } // anonymous namespace
(...skipping 10 matching lines...) Expand all
203 const SkBitmap& primary_icon, 205 const SkBitmap& primary_icon,
204 const SkBitmap& badge_icon, 206 const SkBitmap& badge_icon,
205 const FinishCallback& finish_callback) { 207 const FinishCallback& finish_callback) {
206 // The installer will delete itself when it is done. 208 // The installer will delete itself when it is done.
207 WebApkInstaller* installer = new WebApkInstaller(context); 209 WebApkInstaller* installer = new WebApkInstaller(context);
208 installer->InstallAsync(shortcut_info, primary_icon, badge_icon, 210 installer->InstallAsync(shortcut_info, primary_icon, badge_icon,
209 finish_callback); 211 finish_callback);
210 } 212 }
211 213
212 // static 214 // static
213 void WebApkInstaller::UpdateAsync( 215 void WebApkInstaller::UpdateAsync(content::BrowserContext* context,
214 content::BrowserContext* context, 216 const std::string& webapk_package,
215 const std::string& webapk_package, 217 const GURL& start_url,
216 const GURL& start_url, 218 const base::string16& short_name,
217 const base::string16& short_name, 219 std::unique_ptr<std::string> serialized_proto,
218 std::unique_ptr<std::vector<uint8_t>> serialized_proto, 220 const FinishCallback& finish_callback) {
219 const FinishCallback& finish_callback) {
220 // The installer will delete itself when it is done. 221 // The installer will delete itself when it is done.
221 WebApkInstaller* installer = new WebApkInstaller(context); 222 WebApkInstaller* installer = new WebApkInstaller(context);
222 installer->UpdateAsync(webapk_package, start_url, short_name, 223 installer->UpdateAsync(webapk_package, start_url, short_name,
223 std::move(serialized_proto), finish_callback); 224 std::move(serialized_proto), finish_callback);
224 } 225 }
225 226
226 // static 227 // static
227 void WebApkInstaller::InstallAsyncForTesting(WebApkInstaller* installer, 228 void WebApkInstaller::InstallAsyncForTesting(WebApkInstaller* installer,
228 const ShortcutInfo& shortcut_info, 229 const ShortcutInfo& shortcut_info,
229 const SkBitmap& primary_icon, 230 const SkBitmap& primary_icon,
230 const SkBitmap& badge_icon, 231 const SkBitmap& badge_icon,
231 const FinishCallback& callback) { 232 const FinishCallback& callback) {
232 installer->InstallAsync(shortcut_info, primary_icon, badge_icon, callback); 233 installer->InstallAsync(shortcut_info, primary_icon, badge_icon, callback);
233 } 234 }
234 235
235 // static 236 // static
236 void WebApkInstaller::UpdateAsyncForTesting( 237 void WebApkInstaller::UpdateAsyncForTesting(
237 WebApkInstaller* installer, 238 WebApkInstaller* installer,
238 const std::string& webapk_package, 239 const std::string& webapk_package,
239 const GURL& start_url, 240 const GURL& start_url,
240 const base::string16& short_name, 241 const base::string16& short_name,
241 std::unique_ptr<std::vector<uint8_t>> serialized_proto, 242 std::unique_ptr<std::string> serialized_proto,
242 const FinishCallback& finish_callback) { 243 const FinishCallback& finish_callback) {
243 installer->UpdateAsync(webapk_package, start_url, short_name, 244 installer->UpdateAsync(webapk_package, start_url, short_name,
244 std::move(serialized_proto), finish_callback); 245 std::move(serialized_proto), finish_callback);
245 } 246 }
246 247
247 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { 248 void WebApkInstaller::SetTimeoutMs(int timeout_ms) {
248 webapk_server_timeout_ms_ = timeout_ms; 249 webapk_server_timeout_ms_ = timeout_ms;
249 } 250 }
250 251
251 void WebApkInstaller::OnInstallFinished( 252 void WebApkInstaller::OnInstallFinished(
252 JNIEnv* env, 253 JNIEnv* env,
253 const base::android::JavaParamRef<jobject>& obj, 254 const base::android::JavaParamRef<jobject>& obj,
254 jint result) { 255 jint result) {
255 OnResult(static_cast<WebApkInstallResult>(result)); 256 OnResult(static_cast<WebApkInstallResult>(result));
256 } 257 }
257 258
258 // static 259 // static
259 void WebApkInstaller::BuildProto( 260 void WebApkInstaller::BuildProto(
260 const ShortcutInfo& shortcut_info, 261 const ShortcutInfo& shortcut_info,
261 const SkBitmap& primary_icon, 262 const SkBitmap& primary_icon,
262 const SkBitmap& badge_icon, 263 const SkBitmap& badge_icon,
263 const std::string& package_name, 264 const std::string& package_name,
264 const std::string& version, 265 const std::string& version,
265 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, 266 const std::map<std::string, std::string>& icon_url_to_murmur2_hash,
266 bool is_manifest_stale, 267 bool is_manifest_stale,
267 const base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>& 268 const base::Callback<void(std::unique_ptr<std::string>)>& callback) {
268 callback) {
269 base::PostTaskAndReplyWithResult( 269 base::PostTaskAndReplyWithResult(
270 GetBackgroundTaskRunner().get(), FROM_HERE, 270 GetBackgroundTaskRunner().get(), FROM_HERE,
271 base::Bind(&BuildProtoInBackground, shortcut_info, primary_icon, 271 base::Bind(&BuildProtoInBackground, shortcut_info, primary_icon,
272 badge_icon, package_name, version, icon_url_to_murmur2_hash, 272 badge_icon, package_name, version, icon_url_to_murmur2_hash,
273 is_manifest_stale), 273 is_manifest_stale),
274 callback); 274 callback);
275 } 275 }
276 276
277 // static 277 // static
278 bool WebApkInstaller::Register(JNIEnv* env) { 278 bool WebApkInstaller::Register(JNIEnv* env) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 // generated for another user. (The icon can be dynamically generated.) 360 // generated for another user. (The icon can be dynamically generated.)
361 // 361 //
362 // We redownload the icon in order to take the Murmur2 hash. The redownload 362 // We redownload the icon in order to take the Murmur2 hash. The redownload
363 // should be fast because the icon should be in the HTTP cache. 363 // should be fast because the icon should be in the HTTP cache.
364 WebApkIconHasher::DownloadAndComputeMurmur2Hash( 364 WebApkIconHasher::DownloadAndComputeMurmur2Hash(
365 request_context_getter_, install_shortcut_info_->best_primary_icon_url, 365 request_context_getter_, install_shortcut_info_->best_primary_icon_url,
366 base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash, 366 base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash,
367 weak_ptr_factory_.GetWeakPtr())); 367 weak_ptr_factory_.GetWeakPtr()));
368 } 368 }
369 369
370 void WebApkInstaller::UpdateAsync( 370 void WebApkInstaller::UpdateAsync(const std::string& webapk_package,
371 const std::string& webapk_package, 371 const GURL& start_url,
372 const GURL& start_url, 372 const base::string16& short_name,
373 const base::string16& short_name, 373 std::unique_ptr<std::string> serialized_proto,
374 std::unique_ptr<std::vector<uint8_t>> serialized_proto, 374 const FinishCallback& finish_callback) {
375 const FinishCallback& finish_callback) {
376 webapk_package_ = webapk_package; 375 webapk_package_ = webapk_package;
377 start_url_ = start_url; 376 start_url_ = start_url;
378 short_name_ = short_name; 377 short_name_ = short_name;
379 finish_callback_ = finish_callback; 378 finish_callback_ = finish_callback;
380 task_type_ = UPDATE; 379 task_type_ = UPDATE;
381 380
382 if (!serialized_proto || serialized_proto->empty()) { 381 if (!serialized_proto || serialized_proto->empty()) {
383 OnResult(WebApkInstallResult::FAILURE); 382 OnResult(WebApkInstallResult::FAILURE);
384 return; 383 return;
385 } 384 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 } 469 }
471 470
472 BuildProto(*install_shortcut_info_, install_primary_icon_, 471 BuildProto(*install_shortcut_info_, install_primary_icon_,
473 install_badge_icon_, "" /* package_name */, "" /* version */, 472 install_badge_icon_, "" /* package_name */, "" /* version */,
474 icon_url_to_murmur2_hash, false /* is_manifest_stale */, 473 icon_url_to_murmur2_hash, false /* is_manifest_stale */,
475 base::Bind(&WebApkInstaller::SendRequest, 474 base::Bind(&WebApkInstaller::SendRequest,
476 weak_ptr_factory_.GetWeakPtr())); 475 weak_ptr_factory_.GetWeakPtr()));
477 } 476 }
478 477
479 void WebApkInstaller::SendRequest( 478 void WebApkInstaller::SendRequest(
480 std::unique_ptr<std::vector<uint8_t>> serialized_proto) { 479 std::unique_ptr<std::string> serialized_proto) {
481 timer_.Start( 480 timer_.Start(
482 FROM_HERE, base::TimeDelta::FromMilliseconds(webapk_server_timeout_ms_), 481 FROM_HERE, base::TimeDelta::FromMilliseconds(webapk_server_timeout_ms_),
483 base::Bind(&WebApkInstaller::OnResult, weak_ptr_factory_.GetWeakPtr(), 482 base::Bind(&WebApkInstaller::OnResult, weak_ptr_factory_.GetWeakPtr(),
484 WebApkInstallResult::FAILURE)); 483 WebApkInstallResult::FAILURE));
485 484
486 std::string serialized_proto_string(serialized_proto->begin(), 485 std::string decoded_proto;
487 serialized_proto->end()); 486 base::Base64Decode(*serialized_proto, &decoded_proto);
488 487
489 url_fetcher_ = 488 url_fetcher_ =
490 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); 489 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this);
491 url_fetcher_->SetRequestContext(request_context_getter_); 490 url_fetcher_->SetRequestContext(request_context_getter_);
492 url_fetcher_->SetUploadData(kProtoMimeType, serialized_proto_string); 491 url_fetcher_->SetUploadData(kProtoMimeType, decoded_proto);
493 url_fetcher_->SetLoadFlags( 492 url_fetcher_->SetLoadFlags(
494 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES | 493 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES |
495 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA); 494 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA);
496 url_fetcher_->Start(); 495 url_fetcher_->Start();
497 } 496 }
OLDNEW
« no previous file with comments | « chrome/browser/android/webapk/webapk_installer.h ('k') | chrome/browser/android/webapk/webapk_update_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698