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