OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/webapk/webapk_installer.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/WebApkInstallerBridge_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[] = |
| 35 "https://webapk.googleapis.com/v1alpha/webApks?alt=proto"; |
| 36 |
| 37 // The MIME type of the POST data sent to the server. |
| 38 const char kProtoMimeType[] = "application/x-protobuf"; |
| 39 |
| 40 // The number of milliseconds to wait for the WebAPK download URL from the |
| 41 // WebAPK server. |
| 42 const int kWebApkDownloadUrlTimeoutMs = 4000; |
| 43 |
| 44 // The number of milliseconds to wait for the WebAPK download to complete. |
| 45 const int kDownloadTimeoutMs = 20000; |
| 46 |
| 47 // Returns the scope from |info| if it is specified. Otherwise, returns the |
| 48 // default scope. |
| 49 GURL GetScope(const ShortcutInfo& info) { |
| 50 return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin(); |
| 51 } |
| 52 |
| 53 // Computes a MD5 hash of |bitmap|'s PNG encoded bytes. |
| 54 std::string ComputeBitmapHash(const SkBitmap& bitmap) { |
| 55 std::vector<unsigned char> png_bytes; |
| 56 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); |
| 57 base::MD5Digest digest; |
| 58 base::MD5Sum(&png_bytes.front(), png_bytes.size(), &digest); |
| 59 return base::MD5DigestToBase16(digest); |
| 60 } |
| 61 |
| 62 // Converts a color from the format specified in content::Manifest to a CSS |
| 63 // string. |
| 64 std::string ColorToString(int64_t color) { |
| 65 if (color == content::Manifest::kInvalidOrMissingColor) |
| 66 return ""; |
| 67 |
| 68 SkColor sk_color = reinterpret_cast<uint32_t&>(color); |
| 69 int r = SkColorGetR(sk_color); |
| 70 int g = SkColorGetG(sk_color); |
| 71 int b = SkColorGetB(sk_color); |
| 72 double a = SkColorGetA(sk_color) / 255.0; |
| 73 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); |
| 74 } |
| 75 |
| 76 } // anonymous namespace |
| 77 |
| 78 WebApkInstaller::WebApkInstaller(content::BrowserContext* browser_context, |
| 79 const ShortcutInfo& shortcut_info, |
| 80 const SkBitmap& shortcut_icon) |
| 81 : browser_context_(browser_context), |
| 82 shortcut_info_(shortcut_info), |
| 83 shortcut_icon_(shortcut_icon), |
| 84 io_weak_ptr_factory_(this) { |
| 85 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 86 server_url_ = |
| 87 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) |
| 88 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) |
| 89 : kDefaultWebApkServerUrl); |
| 90 } |
| 91 |
| 92 WebApkInstaller::~WebApkInstaller() {} |
| 93 |
| 94 // static |
| 95 bool WebApkInstaller::Register(JNIEnv* env) { |
| 96 return RegisterNativesImpl(env); |
| 97 } |
| 98 |
| 99 void WebApkInstaller::InstallAsync(const FinishCallback& finish_callback) { |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 101 finish_callback_ = finish_callback; |
| 102 // base::Unretained() is safe because WebApkInstaller owns itself and does not |
| 103 // start the timeout timer till after |
| 104 // InitializeRequestContextGetterOnUIThread() is called. |
| 105 content::BrowserThread::PostTask( |
| 106 content::BrowserThread::UI, FROM_HERE, |
| 107 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread, |
| 108 base::Unretained(this))); |
| 109 } |
| 110 |
| 111 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 113 timer_.Stop(); |
| 114 |
| 115 if (!source->GetStatus().is_success() || |
| 116 source->GetResponseCode() != net::HTTP_OK) { |
| 117 OnFailure(); |
| 118 return; |
| 119 } |
| 120 |
| 121 std::string response_string; |
| 122 source->GetResponseAsString(&response_string); |
| 123 |
| 124 std::unique_ptr<webapk::CreateWebApkResponse> response( |
| 125 new webapk::CreateWebApkResponse); |
| 126 if (!response->ParseFromString(response_string)) { |
| 127 OnFailure(); |
| 128 return; |
| 129 } |
| 130 |
| 131 if (response->signed_download_url().empty() || |
| 132 response->webapk_package_name().empty()) { |
| 133 OnFailure(); |
| 134 return; |
| 135 } |
| 136 OnGotWebApkDownloadUrl(response->signed_download_url(), |
| 137 response->webapk_package_name()); |
| 138 } |
| 139 |
| 140 void WebApkInstaller::InitializeRequestContextGetterOnUIThread() { |
| 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 142 // Must be called on UI thread. |
| 143 request_context_getter_ = |
| 144 Profile::FromBrowserContext(browser_context_)->GetRequestContext(); |
| 145 |
| 146 content::BrowserThread::PostTask( |
| 147 content::BrowserThread::IO, FROM_HERE, |
| 148 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, |
| 149 io_weak_ptr_factory_.GetWeakPtr())); |
| 150 } |
| 151 |
| 152 void WebApkInstaller::SendCreateWebApkRequest() { |
| 153 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 154 std::unique_ptr<webapk::CreateWebApkRequest> request = |
| 155 BuildCreateWebApkRequest(); |
| 156 |
| 157 timer_.Start(FROM_HERE, |
| 158 base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs), |
| 159 base::Bind(&WebApkInstaller::OnTimeout, |
| 160 io_weak_ptr_factory_.GetWeakPtr())); |
| 161 |
| 162 url_fetcher_ = |
| 163 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); |
| 164 url_fetcher_->SetRequestContext(request_context_getter_); |
| 165 std::string serialized_request; |
| 166 request->SerializeToString(&serialized_request); |
| 167 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
| 168 url_fetcher_->Start(); |
| 169 } |
| 170 |
| 171 void WebApkInstaller::OnGotWebApkDownloadUrl(const std::string& download_url, |
| 172 const std::string& package_name) { |
| 173 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 174 |
| 175 base::FilePath output_dir; |
| 176 base::android::GetCacheDirectory(&output_dir); |
| 177 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory |
| 178 // directory. |
| 179 |
| 180 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kDownloadTimeoutMs), |
| 181 base::Bind(&WebApkInstaller::OnTimeout, |
| 182 io_weak_ptr_factory_.GetWeakPtr())); |
| 183 |
| 184 base::FilePath output_path = output_dir.AppendASCII(package_name); |
| 185 downloader_.reset(new FileDownloader( |
| 186 GURL(download_url), output_path, true, request_context_getter_, |
| 187 base::Bind(&WebApkInstaller::OnWebApkDownloaded, |
| 188 io_weak_ptr_factory_.GetWeakPtr(), output_path, |
| 189 package_name))); |
| 190 } |
| 191 |
| 192 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, |
| 193 const std::string& package_name, |
| 194 FileDownloader::Result result) { |
| 195 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 196 |
| 197 timer_.Stop(); |
| 198 |
| 199 if (result != FileDownloader::DOWNLOADED) { |
| 200 OnFailure(); |
| 201 return; |
| 202 } |
| 203 |
| 204 JNIEnv* env = base::android::AttachCurrentThread(); |
| 205 base::android::ScopedJavaLocalRef<jstring> java_file_path = |
| 206 base::android::ConvertUTF8ToJavaString(env, file_path.value()); |
| 207 base::android::ScopedJavaLocalRef<jstring> java_package_name = |
| 208 base::android::ConvertUTF8ToJavaString(env, package_name); |
| 209 bool success = Java_WebApkInstallerBridge_installAsync( |
| 210 env, java_file_path.obj(), java_package_name.obj()); |
| 211 if (success) |
| 212 OnSuccess(); |
| 213 else |
| 214 OnFailure(); |
| 215 } |
| 216 |
| 217 std::unique_ptr<webapk::CreateWebApkRequest> |
| 218 WebApkInstaller::BuildCreateWebApkRequest() { |
| 219 std::unique_ptr<webapk::CreateWebApkRequest> request( |
| 220 new webapk::CreateWebApkRequest); |
| 221 |
| 222 webapk::WebApk* webapk = request->mutable_webapk(); |
| 223 webapk->set_manifest_url(shortcut_info_.manifest_url.spec()); |
| 224 webapk->set_requester_application_package( |
| 225 base::android::BuildInfo::GetInstance()->package_name()); |
| 226 webapk->set_requester_application_version(version_info::GetVersionNumber()); |
| 227 |
| 228 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
| 229 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name)); |
| 230 web_app_manifest->set_short_name( |
| 231 base::UTF16ToUTF8(shortcut_info_.short_name)); |
| 232 web_app_manifest->set_start_url(shortcut_info_.url.spec()); |
| 233 web_app_manifest->set_orientation( |
| 234 content::WebScreenOrientationLockTypeToString( |
| 235 shortcut_info_.orientation)); |
| 236 web_app_manifest->set_display_mode( |
| 237 content::WebDisplayModeToString(shortcut_info_.display)); |
| 238 web_app_manifest->set_background_color( |
| 239 ColorToString(shortcut_info_.background_color)); |
| 240 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color)); |
| 241 |
| 242 std::string* scope = web_app_manifest->add_scopes(); |
| 243 scope->assign(GetScope(shortcut_info_).spec()); |
| 244 webapk::Image* image = web_app_manifest->add_icons(); |
| 245 image->set_src(shortcut_info_.icon_url.spec()); |
| 246 // TODO(pkotwicz): Get MD5 hash of untransformed icon's bytes (with no |
| 247 // encoding/decoding). |
| 248 image->set_hash(ComputeBitmapHash(shortcut_icon_)); |
| 249 std::vector<unsigned char> png_bytes; |
| 250 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes); |
| 251 image->set_image_data(&png_bytes.front(), png_bytes.size()); |
| 252 |
| 253 return request; |
| 254 } |
| 255 |
| 256 void WebApkInstaller::OnTimeout() { |
| 257 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 258 OnFailure(); |
| 259 } |
| 260 |
| 261 void WebApkInstaller::OnSuccess() { |
| 262 finish_callback_.Run(true); |
| 263 delete this; |
| 264 } |
| 265 |
| 266 void WebApkInstaller::OnFailure() { |
| 267 finish_callback_.Run(false); |
| 268 delete this; |
| 269 } |
OLD | NEW |