Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
|
dominickn
2017/06/14 00:14:23
#include <utility> for std::move
#include <memory>
pkotwicz
2017/06/15 02:25:20
Added include for <utility>
webapk_installer.h al
| |
| 7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/android/path_utils.h" | 10 #include "base/android/path_utils.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 101 |
| 102 // Populates the webapk::Image::image_data field of |image| with |icon|. | 102 // Populates the webapk::Image::image_data field of |image| with |icon|. |
| 103 void SetImageData(webapk::Image* image, const SkBitmap& icon) { | 103 void SetImageData(webapk::Image* image, const SkBitmap& icon) { |
| 104 std::vector<unsigned char> png_bytes; | 104 std::vector<unsigned char> png_bytes; |
| 105 gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &png_bytes); | 105 gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &png_bytes); |
| 106 image->set_image_data(&png_bytes.front(), png_bytes.size()); | 106 image->set_image_data(&png_bytes.front(), png_bytes.size()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Populates webapk::WebApk and returns it. | 109 // Populates webapk::WebApk and returns it. |
| 110 // Must be called on a worker thread because it encodes an SkBitmap. | 110 // Must be called on a worker thread because it encodes an SkBitmap. |
| 111 std::unique_ptr<webapk::WebApk> BuildProtoInBackground( | 111 std::unique_ptr<std::vector<uint8_t>> BuildProtoInBackground( |
| 112 const ShortcutInfo& shortcut_info, | 112 const ShortcutInfo& shortcut_info, |
| 113 const SkBitmap& primary_icon, | 113 const SkBitmap& primary_icon, |
| 114 const SkBitmap& badge_icon, | 114 const SkBitmap& badge_icon, |
| 115 const std::string& package_name, | 115 const std::string& package_name, |
| 116 const std::string& version, | 116 const std::string& version, |
| 117 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 117 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 118 bool is_manifest_stale) { | 118 bool is_manifest_stale) { |
| 119 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); | 119 std::unique_ptr<webapk::WebApk> webapk(new webapk::WebApk); |
| 120 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); | 120 webapk->set_manifest_url(shortcut_info.manifest_url.spec()); |
| 121 webapk->set_requester_application_package( | 121 webapk->set_requester_application_package( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 if (shortcut_info.best_badge_icon_url != | 164 if (shortcut_info.best_badge_icon_url != |
| 165 shortcut_info.best_primary_icon_url) { | 165 shortcut_info.best_primary_icon_url) { |
| 166 SetImageData(image, badge_icon); | 166 SetImageData(image, badge_icon); |
| 167 } | 167 } |
| 168 image->add_usages(webapk::Image::BADGE_ICON); | 168 image->add_usages(webapk::Image::BADGE_ICON); |
| 169 } | 169 } |
| 170 image->set_src(entry.first); | 170 image->set_src(entry.first); |
| 171 image->set_hash(entry.second); | 171 image->set_hash(entry.second); |
| 172 } | 172 } |
| 173 | 173 |
| 174 return webapk; | 174 size_t serialized_size = webapk->ByteSize(); |
| 175 std::unique_ptr<std::vector<uint8_t>> serialized_proto( | |
| 176 new std::vector<uint8_t>); | |
|
dominickn
2017/06/14 00:14:23
Use base::MakeUnique from base/memory/ptr_utils.h
| |
| 177 serialized_proto->resize(serialized_size); | |
| 178 webapk->SerializeToArray(serialized_proto->data(), serialized_size); | |
| 179 return std::move(serialized_proto); | |
| 175 } | 180 } |
| 176 | 181 |
| 177 // Returns task runner for running background tasks. | 182 // Returns task runner for running background tasks. |
| 178 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { | 183 scoped_refptr<base::TaskRunner> GetBackgroundTaskRunner() { |
| 179 return base::CreateTaskRunnerWithTraits( | 184 return base::CreateTaskRunnerWithTraits( |
| 180 {base::MayBlock(), base::TaskPriority::BACKGROUND, | 185 {base::MayBlock(), base::TaskPriority::BACKGROUND, |
| 181 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); | 186 base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
| 182 } | 187 } |
| 183 | 188 |
| 184 } // anonymous namespace | 189 } // anonymous namespace |
| 185 | 190 |
| 186 WebApkInstaller::~WebApkInstaller() { | 191 WebApkInstaller::~WebApkInstaller() { |
| 187 JNIEnv* env = base::android::AttachCurrentThread(); | 192 JNIEnv* env = base::android::AttachCurrentThread(); |
| 188 Java_WebApkInstaller_destroy(env, java_ref_); | 193 Java_WebApkInstaller_destroy(env, java_ref_); |
| 189 java_ref_.Reset(); | 194 java_ref_.Reset(); |
| 190 } | 195 } |
| 191 | 196 |
| 192 // static | 197 // static |
| 193 void WebApkInstaller::InstallAsync(content::BrowserContext* context, | 198 void WebApkInstaller::InstallAsync(content::BrowserContext* context, |
| 194 const ShortcutInfo& shortcut_info, | 199 const ShortcutInfo& shortcut_info, |
| 195 const SkBitmap& primary_icon, | 200 const SkBitmap& primary_icon, |
| 196 const SkBitmap& badge_icon, | 201 const SkBitmap& badge_icon, |
| 197 const FinishCallback& finish_callback) { | 202 const FinishCallback& finish_callback) { |
| 198 // The installer will delete itself when it is done. | 203 // The installer will delete itself when it is done. |
| 199 WebApkInstaller* installer = | 204 WebApkInstaller* installer = new WebApkInstaller(context); |
| 200 new WebApkInstaller(context, shortcut_info, primary_icon, badge_icon); | 205 installer->InstallAsync(shortcut_info, primary_icon, badge_icon, |
| 201 installer->InstallAsync(finish_callback); | 206 finish_callback); |
| 202 } | 207 } |
| 203 | 208 |
| 204 // static | 209 // static |
| 205 void WebApkInstaller::UpdateAsync( | 210 void WebApkInstaller::UpdateAsync( |
| 206 content::BrowserContext* context, | 211 content::BrowserContext* context, |
| 207 const ShortcutInfo& shortcut_info, | |
| 208 const SkBitmap& primary_icon, | |
| 209 const std::string& webapk_package, | 212 const std::string& webapk_package, |
| 210 int webapk_version, | 213 const GURL& start_url, |
| 211 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 214 const base::string16& short_name, |
| 212 bool is_manifest_stale, | 215 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
| 213 const FinishCallback& finish_callback) { | 216 const FinishCallback& finish_callback) { |
| 214 // The installer will delete itself when it is done. | 217 // The installer will delete itself when it is done. |
| 215 WebApkInstaller* installer = new WebApkInstaller( | 218 WebApkInstaller* installer = new WebApkInstaller(context); |
| 216 context, shortcut_info, primary_icon, SkBitmap()); | 219 installer->UpdateAsync(webapk_package, start_url, short_name, |
| 217 installer->UpdateAsync(webapk_package, webapk_version, | 220 std::move(serialized_proto), finish_callback); |
| 218 icon_url_to_murmur2_hash, is_manifest_stale, | |
| 219 finish_callback); | |
| 220 } | 221 } |
| 221 | 222 |
| 222 // staic | 223 // staic |
|
dominickn
2017/06/14 00:14:23
Minor nit: "static"
| |
| 223 void WebApkInstaller::InstallAsyncForTesting( | 224 void WebApkInstaller::InstallAsyncForTesting(WebApkInstaller* installer, |
| 224 WebApkInstaller* installer, | 225 const ShortcutInfo& shortcut_info, |
| 225 const FinishCallback& finish_callback) { | 226 const SkBitmap& primary_icon, |
| 226 installer->InstallAsync(finish_callback); | 227 const SkBitmap& badge_icon, |
| 228 const FinishCallback& callback) { | |
| 229 installer->InstallAsync(shortcut_info, primary_icon, badge_icon, callback); | |
| 227 } | 230 } |
| 228 | 231 |
| 229 // static | 232 // static |
| 230 void WebApkInstaller::UpdateAsyncForTesting( | 233 void WebApkInstaller::UpdateAsyncForTesting( |
| 231 WebApkInstaller* installer, | 234 WebApkInstaller* installer, |
| 232 const std::string& webapk_package, | 235 const std::string& webapk_package, |
| 233 int webapk_version, | 236 const GURL& start_url, |
| 234 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 237 const base::string16& short_name, |
| 235 bool is_manifest_stale, | 238 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
| 236 const FinishCallback& finish_callback) { | 239 const FinishCallback& finish_callback) { |
| 237 installer->UpdateAsync(webapk_package, webapk_version, | 240 installer->UpdateAsync(webapk_package, start_url, short_name, |
| 238 icon_url_to_murmur2_hash, is_manifest_stale, | 241 std::move(serialized_proto), finish_callback); |
| 239 finish_callback); | |
| 240 } | 242 } |
| 241 | 243 |
| 242 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { | 244 void WebApkInstaller::SetTimeoutMs(int timeout_ms) { |
| 243 webapk_server_timeout_ms_ = timeout_ms; | 245 webapk_server_timeout_ms_ = timeout_ms; |
| 244 } | 246 } |
| 245 | 247 |
| 246 void WebApkInstaller::OnInstallFinished( | 248 void WebApkInstaller::OnInstallFinished( |
| 247 JNIEnv* env, | 249 JNIEnv* env, |
| 248 const base::android::JavaParamRef<jobject>& obj, | 250 const base::android::JavaParamRef<jobject>& obj, |
| 249 jint result) { | 251 jint result) { |
| 250 OnResult(static_cast<WebApkInstallResult>(result)); | 252 OnResult(static_cast<WebApkInstallResult>(result)); |
| 251 } | 253 } |
| 252 | 254 |
| 253 // static | 255 // static |
| 254 void WebApkInstaller::BuildProto( | 256 void WebApkInstaller::BuildProto( |
| 255 const ShortcutInfo& shortcut_info, | 257 const ShortcutInfo& shortcut_info, |
| 256 const SkBitmap& primary_icon, | 258 const SkBitmap& primary_icon, |
| 257 const SkBitmap& badge_icon, | 259 const SkBitmap& badge_icon, |
| 258 const std::string& package_name, | 260 const std::string& package_name, |
| 259 const std::string& version, | 261 const std::string& version, |
| 260 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 262 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, |
| 261 bool is_manifest_stale, | 263 bool is_manifest_stale, |
| 262 const base::Callback<void(std::unique_ptr<webapk::WebApk>)>& callback) { | 264 const base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>& |
| 265 callback) { | |
| 263 base::PostTaskAndReplyWithResult( | 266 base::PostTaskAndReplyWithResult( |
| 264 GetBackgroundTaskRunner().get(), FROM_HERE, | 267 GetBackgroundTaskRunner().get(), FROM_HERE, |
| 265 base::Bind(&BuildProtoInBackground, shortcut_info, primary_icon, | 268 base::Bind(&BuildProtoInBackground, shortcut_info, primary_icon, |
| 266 badge_icon, package_name, version, icon_url_to_murmur2_hash, | 269 badge_icon, package_name, version, icon_url_to_murmur2_hash, |
| 267 is_manifest_stale), | 270 is_manifest_stale), |
| 268 callback); | 271 callback); |
| 269 } | 272 } |
| 270 | 273 |
| 271 // static | 274 // static |
| 272 bool WebApkInstaller::Register(JNIEnv* env) { | 275 bool WebApkInstaller::Register(JNIEnv* env) { |
| 273 return RegisterNativesImpl(env); | 276 return RegisterNativesImpl(env); |
| 274 } | 277 } |
| 275 | 278 |
| 276 void WebApkInstaller::InstallOrUpdateWebApk(const std::string& package_name, | 279 void WebApkInstaller::InstallOrUpdateWebApk(const std::string& package_name, |
| 277 int version, | 280 int version, |
| 278 const std::string& token) { | 281 const std::string& token) { |
| 279 webapk_package_ = package_name; | 282 webapk_package_ = package_name; |
| 280 | 283 |
| 281 JNIEnv* env = base::android::AttachCurrentThread(); | 284 JNIEnv* env = base::android::AttachCurrentThread(); |
| 282 base::android::ScopedJavaLocalRef<jstring> java_webapk_package = | 285 base::android::ScopedJavaLocalRef<jstring> java_webapk_package = |
| 283 base::android::ConvertUTF8ToJavaString(env, webapk_package_); | 286 base::android::ConvertUTF8ToJavaString(env, webapk_package_); |
| 284 base::android::ScopedJavaLocalRef<jstring> java_title = | 287 base::android::ScopedJavaLocalRef<jstring> java_title = |
| 285 base::android::ConvertUTF16ToJavaString(env, shortcut_info_.user_title); | 288 base::android::ConvertUTF16ToJavaString(env, short_name_); |
| 286 base::android::ScopedJavaLocalRef<jstring> java_token = | 289 base::android::ScopedJavaLocalRef<jstring> java_token = |
| 287 base::android::ConvertUTF8ToJavaString(env, token); | 290 base::android::ConvertUTF8ToJavaString(env, token); |
| 288 base::android::ScopedJavaLocalRef<jstring> java_url = | 291 base::android::ScopedJavaLocalRef<jstring> java_url = |
| 289 base::android::ConvertUTF8ToJavaString(env, shortcut_info_.url.spec()); | 292 base::android::ConvertUTF8ToJavaString(env, start_url_.spec()); |
| 290 | 293 |
| 291 if (task_type_ == WebApkInstaller::INSTALL) { | 294 if (task_type_ == WebApkInstaller::INSTALL) { |
| 292 webapk::TrackRequestTokenDuration(install_duration_timer_->Elapsed()); | 295 webapk::TrackRequestTokenDuration(install_duration_timer_->Elapsed()); |
| 293 Java_WebApkInstaller_installWebApkAsync(env, java_ref_, java_webapk_package, | 296 Java_WebApkInstaller_installWebApkAsync( |
| 294 version, java_title, java_token, | 297 env, java_ref_, java_webapk_package, version, java_title, java_token, |
| 295 java_url, shortcut_info_.source); | 298 java_url, install_shortcut_info_->source); |
| 296 } else { | 299 } else { |
| 297 Java_WebApkInstaller_updateAsync(env, java_ref_, java_webapk_package, | 300 Java_WebApkInstaller_updateAsync(env, java_ref_, java_webapk_package, |
| 298 version, java_title, java_token, java_url); | 301 version, java_title, java_token, java_url); |
| 299 } | 302 } |
| 300 } | 303 } |
| 301 | 304 |
| 302 void WebApkInstaller::OnResult(WebApkInstallResult result) { | 305 void WebApkInstaller::OnResult(WebApkInstallResult result) { |
| 303 weak_ptr_factory_.InvalidateWeakPtrs(); | 306 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 304 finish_callback_.Run(result, relax_updates_, webapk_package_); | 307 finish_callback_.Run(result, relax_updates_, webapk_package_); |
| 305 | 308 |
| 306 if (task_type_ == WebApkInstaller::INSTALL) { | 309 if (task_type_ == WebApkInstaller::INSTALL) { |
| 307 if (result == WebApkInstallResult::SUCCESS) { | 310 if (result == WebApkInstallResult::SUCCESS) { |
| 308 webapk::TrackInstallDuration(install_duration_timer_->Elapsed()); | 311 webapk::TrackInstallDuration(install_duration_timer_->Elapsed()); |
| 309 webapk::TrackInstallEvent(webapk::INSTALL_COMPLETED); | 312 webapk::TrackInstallEvent(webapk::INSTALL_COMPLETED); |
| 310 } else { | 313 } else { |
| 311 DVLOG(1) << "The WebAPK installation failed."; | 314 DVLOG(1) << "The WebAPK installation failed."; |
| 312 webapk::TrackInstallEvent(webapk::INSTALL_FAILED); | 315 webapk::TrackInstallEvent(webapk::INSTALL_FAILED); |
| 313 } | 316 } |
| 314 } | 317 } |
| 315 | 318 |
| 316 delete this; | 319 delete this; |
| 317 } | 320 } |
| 318 | 321 |
| 319 WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context, | 322 WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context) |
| 320 const ShortcutInfo& shortcut_info, | |
| 321 const SkBitmap& primary_icon, | |
| 322 const SkBitmap& badge_icon) | |
| 323 : request_context_getter_( | 323 : request_context_getter_( |
| 324 Profile::FromBrowserContext(browser_context)->GetRequestContext()), | 324 Profile::FromBrowserContext(browser_context)->GetRequestContext()), |
| 325 shortcut_info_(shortcut_info), | |
| 326 primary_icon_(primary_icon), | |
| 327 badge_icon_(badge_icon), | |
| 328 server_url_(GetServerUrl()), | 325 server_url_(GetServerUrl()), |
| 329 webapk_server_timeout_ms_(kWebApkDownloadUrlTimeoutMs), | 326 webapk_server_timeout_ms_(kWebApkDownloadUrlTimeoutMs), |
| 330 relax_updates_(false), | 327 relax_updates_(false), |
| 331 task_type_(UNDEFINED), | 328 task_type_(UNDEFINED), |
| 332 weak_ptr_factory_(this) { | 329 weak_ptr_factory_(this) { |
| 333 CreateJavaRef(); | 330 CreateJavaRef(); |
| 334 } | 331 } |
| 335 | 332 |
| 336 void WebApkInstaller::CreateJavaRef() { | 333 void WebApkInstaller::CreateJavaRef() { |
| 337 JNIEnv* env = base::android::AttachCurrentThread(); | 334 JNIEnv* env = base::android::AttachCurrentThread(); |
| 338 java_ref_.Reset( | 335 java_ref_.Reset( |
| 339 Java_WebApkInstaller_create(env, reinterpret_cast<intptr_t>(this))); | 336 Java_WebApkInstaller_create(env, reinterpret_cast<intptr_t>(this))); |
| 340 } | 337 } |
| 341 | 338 |
| 342 void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) { | 339 void WebApkInstaller::InstallAsync(const ShortcutInfo& shortcut_info, |
| 340 const SkBitmap& primary_icon, | |
| 341 const SkBitmap& badge_icon, | |
| 342 const FinishCallback& finish_callback) { | |
| 343 install_duration_timer_.reset(new base::ElapsedTimer()); | 343 install_duration_timer_.reset(new base::ElapsedTimer()); |
| 344 | 344 |
| 345 install_shortcut_info_.reset(new ShortcutInfo(shortcut_info)); | |
| 346 install_primary_icon_ = primary_icon; | |
| 347 install_badge_icon_ = badge_icon; | |
| 348 start_url_ = shortcut_info.url; | |
| 349 short_name_ = shortcut_info.short_name; | |
| 345 finish_callback_ = finish_callback; | 350 finish_callback_ = finish_callback; |
| 346 task_type_ = INSTALL; | 351 task_type_ = INSTALL; |
| 347 | 352 |
| 348 // We need to take the hash of the bitmap at the icon URL prior to any | 353 // We need to take the hash of the bitmap at the icon URL prior to any |
| 349 // transformations being applied to the bitmap (such as encoding/decoding | 354 // transformations being applied to the bitmap (such as encoding/decoding |
| 350 // the bitmap). The icon hash is used to determine whether the icon that | 355 // the bitmap). The icon hash is used to determine whether the icon that |
| 351 // the user sees matches the icon of a WebAPK that the WebAPK server | 356 // the user sees matches the icon of a WebAPK that the WebAPK server |
| 352 // generated for another user. (The icon can be dynamically generated.) | 357 // generated for another user. (The icon can be dynamically generated.) |
| 353 // | 358 // |
| 354 // We redownload the icon in order to take the Murmur2 hash. The redownload | 359 // We redownload the icon in order to take the Murmur2 hash. The redownload |
| 355 // should be fast because the icon should be in the HTTP cache. | 360 // should be fast because the icon should be in the HTTP cache. |
| 356 WebApkIconHasher::DownloadAndComputeMurmur2Hash( | 361 WebApkIconHasher::DownloadAndComputeMurmur2Hash( |
| 357 request_context_getter_, shortcut_info_.best_primary_icon_url, | 362 request_context_getter_, install_shortcut_info_->best_primary_icon_url, |
| 358 base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash, | 363 base::Bind(&WebApkInstaller::OnGotPrimaryIconMurmur2Hash, |
| 359 weak_ptr_factory_.GetWeakPtr())); | 364 weak_ptr_factory_.GetWeakPtr())); |
| 360 } | 365 } |
| 361 | 366 |
| 362 void WebApkInstaller::UpdateAsync( | 367 void WebApkInstaller::UpdateAsync( |
| 363 const std::string& webapk_package, | 368 const std::string& webapk_package, |
| 364 int webapk_version, | 369 const GURL& start_url, |
| 365 const std::map<std::string, std::string>& icon_url_to_murmur2_hash, | 370 const base::string16& short_name, |
| 366 bool is_manifest_stale, | 371 std::unique_ptr<std::vector<uint8_t>> serialized_proto, |
| 367 const FinishCallback& finish_callback) { | 372 const FinishCallback& finish_callback) { |
| 368 webapk_package_ = webapk_package; | 373 webapk_package_ = webapk_package; |
| 374 start_url_ = start_url; | |
| 375 short_name_ = short_name; | |
| 369 finish_callback_ = finish_callback; | 376 finish_callback_ = finish_callback; |
| 370 task_type_ = UPDATE; | 377 task_type_ = UPDATE; |
| 371 | 378 |
| 372 BuildProto(shortcut_info_, primary_icon_, badge_icon_, webapk_package_, | 379 if (!serialized_proto || serialized_proto->empty()) { |
| 373 std::to_string(webapk_version), icon_url_to_murmur2_hash, | 380 OnResult(WebApkInstallResult::FAILURE); |
| 374 is_manifest_stale, | 381 return; |
| 375 base::Bind(&WebApkInstaller::SendRequest, | 382 } |
| 376 weak_ptr_factory_.GetWeakPtr())); | 383 |
| 384 SendRequest(std::move(serialized_proto)); | |
| 377 } | 385 } |
| 378 | 386 |
| 379 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { | 387 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
| 380 timer_.Stop(); | 388 timer_.Stop(); |
| 381 | 389 |
| 382 if (!source->GetStatus().is_success() || | 390 if (!source->GetStatus().is_success() || |
| 383 source->GetResponseCode() != net::HTTP_OK) { | 391 source->GetResponseCode() != net::HTTP_OK) { |
| 384 LOG(WARNING) << base::StringPrintf( | 392 LOG(WARNING) << base::StringPrintf( |
| 385 "WebAPK server returned response code %d.", source->GetResponseCode()); | 393 "WebAPK server returned response code %d.", source->GetResponseCode()); |
| 386 OnResult(WebApkInstallResult::FAILURE); | 394 OnResult(WebApkInstallResult::FAILURE); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 } | 426 } |
| 419 | 427 |
| 420 void WebApkInstaller::OnGotPrimaryIconMurmur2Hash( | 428 void WebApkInstaller::OnGotPrimaryIconMurmur2Hash( |
| 421 const std::string& primary_icon_hash) { | 429 const std::string& primary_icon_hash) { |
| 422 // An empty hash indicates an error during hash calculation. | 430 // An empty hash indicates an error during hash calculation. |
| 423 if (primary_icon_hash.empty()) { | 431 if (primary_icon_hash.empty()) { |
| 424 OnResult(WebApkInstallResult::FAILURE); | 432 OnResult(WebApkInstallResult::FAILURE); |
| 425 return; | 433 return; |
| 426 } | 434 } |
| 427 | 435 |
| 428 if (!shortcut_info_.best_badge_icon_url.is_empty() && | 436 if (!install_shortcut_info_->best_badge_icon_url.is_empty() && |
| 429 shortcut_info_.best_badge_icon_url != | 437 install_shortcut_info_->best_badge_icon_url != |
| 430 shortcut_info_.best_primary_icon_url) { | 438 install_shortcut_info_->best_primary_icon_url) { |
| 431 WebApkIconHasher::DownloadAndComputeMurmur2Hash( | 439 WebApkIconHasher::DownloadAndComputeMurmur2Hash( |
| 432 request_context_getter_, shortcut_info_.best_badge_icon_url, | 440 request_context_getter_, install_shortcut_info_->best_badge_icon_url, |
| 433 base::Bind(&WebApkInstaller::OnGotBadgeIconMurmur2Hash, | 441 base::Bind(&WebApkInstaller::OnGotBadgeIconMurmur2Hash, |
| 434 weak_ptr_factory_.GetWeakPtr(), true, primary_icon_hash)); | 442 weak_ptr_factory_.GetWeakPtr(), true, primary_icon_hash)); |
| 435 } else { | 443 } else { |
| 436 OnGotBadgeIconMurmur2Hash(false, primary_icon_hash, ""); | 444 OnGotBadgeIconMurmur2Hash(false, primary_icon_hash, ""); |
| 437 } | 445 } |
| 438 } | 446 } |
| 439 | 447 |
| 440 void WebApkInstaller::OnGotBadgeIconMurmur2Hash( | 448 void WebApkInstaller::OnGotBadgeIconMurmur2Hash( |
| 441 bool did_fetch_badge_icon, | 449 bool did_fetch_badge_icon, |
| 442 const std::string& primary_icon_hash, | 450 const std::string& primary_icon_hash, |
| 443 const std::string& badge_icon_hash) { | 451 const std::string& badge_icon_hash) { |
| 444 // An empty hash indicates an error during hash calculation. | 452 // An empty hash indicates an error during hash calculation. |
| 445 if (did_fetch_badge_icon && badge_icon_hash.empty()) { | 453 if (did_fetch_badge_icon && badge_icon_hash.empty()) { |
| 446 OnResult(WebApkInstallResult::FAILURE); | 454 OnResult(WebApkInstallResult::FAILURE); |
| 447 return; | 455 return; |
| 448 } | 456 } |
| 449 | 457 |
| 450 // Maps icon URLs to Murmur2 hashes. | 458 // Maps icon URLs to Murmur2 hashes. |
| 451 std::map<std::string, std::string> icon_url_to_murmur2_hash; | 459 std::map<std::string, std::string> icon_url_to_murmur2_hash; |
| 452 for (const std::string& icon_url : shortcut_info_.icon_urls) { | 460 for (const std::string& icon_url : install_shortcut_info_->icon_urls) { |
| 453 if (icon_url == shortcut_info_.best_primary_icon_url.spec()) | 461 if (icon_url == install_shortcut_info_->best_primary_icon_url.spec()) |
| 454 icon_url_to_murmur2_hash[icon_url] = primary_icon_hash; | 462 icon_url_to_murmur2_hash[icon_url] = primary_icon_hash; |
| 455 else if (icon_url == shortcut_info_.best_badge_icon_url.spec()) | 463 else if (icon_url == install_shortcut_info_->best_badge_icon_url.spec()) |
| 456 icon_url_to_murmur2_hash[icon_url] = badge_icon_hash; | 464 icon_url_to_murmur2_hash[icon_url] = badge_icon_hash; |
| 457 else | 465 else |
| 458 icon_url_to_murmur2_hash[icon_url] = ""; | 466 icon_url_to_murmur2_hash[icon_url] = ""; |
| 459 } | 467 } |
| 460 | 468 |
| 461 BuildProto(shortcut_info_, primary_icon_, badge_icon_, "" /* package_name */, | 469 BuildProto(*install_shortcut_info_, install_primary_icon_, |
| 462 "" /* version */, icon_url_to_murmur2_hash, | 470 install_badge_icon_, "" /* package_name */, "" /* version */, |
| 463 false /* is_manifest_stale */, | 471 icon_url_to_murmur2_hash, false /* is_manifest_stale */, |
| 464 base::Bind(&WebApkInstaller::SendRequest, | 472 base::Bind(&WebApkInstaller::SendRequest, |
| 465 weak_ptr_factory_.GetWeakPtr())); | 473 weak_ptr_factory_.GetWeakPtr())); |
| 466 } | 474 } |
| 467 | 475 |
| 468 void WebApkInstaller::SendRequest( | 476 void WebApkInstaller::SendRequest( |
| 469 std::unique_ptr<webapk::WebApk> request_proto) { | 477 std::unique_ptr<std::vector<uint8_t>> serialized_proto) { |
| 470 timer_.Start( | 478 timer_.Start( |
| 471 FROM_HERE, base::TimeDelta::FromMilliseconds(webapk_server_timeout_ms_), | 479 FROM_HERE, base::TimeDelta::FromMilliseconds(webapk_server_timeout_ms_), |
| 472 base::Bind(&WebApkInstaller::OnResult, weak_ptr_factory_.GetWeakPtr(), | 480 base::Bind(&WebApkInstaller::OnResult, weak_ptr_factory_.GetWeakPtr(), |
| 473 WebApkInstallResult::FAILURE)); | 481 WebApkInstallResult::FAILURE)); |
| 474 | 482 |
| 483 std::string serialized_proto_string(serialized_proto->begin(), | |
| 484 serialized_proto->end()); | |
| 485 | |
| 475 url_fetcher_ = | 486 url_fetcher_ = |
| 476 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); | 487 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); |
| 477 url_fetcher_->SetRequestContext(request_context_getter_); | 488 url_fetcher_->SetRequestContext(request_context_getter_); |
| 478 std::string serialized_request; | 489 url_fetcher_->SetUploadData(kProtoMimeType, serialized_proto_string); |
| 479 request_proto->SerializeToString(&serialized_request); | |
| 480 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | |
| 481 url_fetcher_->SetLoadFlags( | 490 url_fetcher_->SetLoadFlags( |
| 482 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES | | 491 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES | |
| 483 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA); | 492 net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_AUTH_DATA); |
| 484 url_fetcher_->Start(); | 493 url_fetcher_->Start(); |
| 485 } | 494 } |
| OLD | NEW |