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/strings/string_util.h" | |
16 #include "base/strings/stringprintf.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 #include "chrome/browser/android/webapk/webapk.pb.h" | |
19 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "components/version_info/version_info.h" | |
22 #include "content/public/browser/browser_thread.h" | |
23 #include "content/public/common/manifest_util.h" | |
24 #include "jni/WebApkInstallerBridge_jni.h" | |
25 #include "net/http/http_status_code.h" | |
26 #include "net/url_request/url_fetcher.h" | |
27 #include "third_party/smhasher/src/MurmurHash2.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 seed to use the murmur2 hash of the app icon. | |
41 const uint32_t kMurmur2HashSeed = 0; | |
gone
2016/07/29 21:20:28
The seed is unimportant, I guess?
pkotwicz
2016/08/02 04:09:37
The seed has to be the same as the one used on the
| |
42 | |
43 // The number of milliseconds to wait for the WebAPK download URL from the | |
44 // WebAPK server. | |
45 const int kWebApkDownloadUrlTimeoutMs = 4000; | |
46 | |
47 // The number of milliseconds to wait for the WebAPK download to complete. | |
48 const int kDownloadTimeoutMs = 20000; | |
49 | |
50 // Returns the scope from |info| if it is specified. Otherwise, returns the | |
51 // default scope. | |
52 GURL GetScope(const ShortcutInfo& info) { | |
53 return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin(); | |
54 } | |
55 | |
56 // Computes a murmur2 hash of |bitmap|'s PNG encoded bytes. | |
57 uint64_t ComputeBitmapHash(const SkBitmap& bitmap) { | |
58 std::vector<unsigned char> png_bytes; | |
59 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); | |
60 return MurmurHash64B(&png_bytes.front(), png_bytes.size(), kMurmur2HashSeed); | |
61 } | |
62 | |
63 // Converts a color from the format specified in content::Manifest to a CSS | |
64 // string. | |
65 std::string ColorToString(int64_t color) { | |
66 if (color == content::Manifest::kInvalidOrMissingColor) | |
67 return ""; | |
68 | |
69 SkColor sk_color = reinterpret_cast<uint32_t&>(color); | |
70 int r = SkColorGetR(sk_color); | |
71 int g = SkColorGetG(sk_color); | |
72 int b = SkColorGetB(sk_color); | |
73 double a = SkColorGetA(sk_color) / 255.0; | |
74 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); | |
75 } | |
76 | |
77 } // anonymous namespace | |
78 | |
79 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info, | |
80 const SkBitmap& shortcut_icon) | |
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 WebApkInstaller::~WebApkInstaller() {} | |
92 | |
93 // static | |
94 bool WebApkInstaller::Register(JNIEnv* env) { | |
95 return RegisterNativesImpl(env); | |
96 } | |
97 | |
98 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context, | |
99 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), browser_context)); | |
109 } | |
110 | |
111 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter( | |
112 net::URLRequestContextGetter* request_context_getter, | |
113 const FinishCallback& finish_callback) { | |
114 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
115 request_context_getter_ = request_context_getter; | |
116 finish_callback_ = finish_callback; | |
117 | |
118 SendCreateWebApkRequest(); | |
119 } | |
120 | |
121 bool WebApkInstaller::StartDownloadedWebApkInstall( | |
122 JNIEnv* env, | |
123 const base::android::ScopedJavaLocalRef<jstring>& java_file_path, | |
124 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) { | |
125 return Java_WebApkInstallerBridge_installAsync(env, java_file_path.obj(), | |
126 java_package_name.obj()); | |
127 } | |
128 | |
129 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) { | |
130 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
131 timer_.Stop(); | |
132 | |
133 if (!source->GetStatus().is_success() || | |
134 source->GetResponseCode() != net::HTTP_OK) { | |
135 OnFailure(); | |
136 return; | |
137 } | |
138 | |
139 std::string response_string; | |
140 source->GetResponseAsString(&response_string); | |
141 | |
142 std::unique_ptr<webapk::CreateWebApkResponse> response( | |
143 new webapk::CreateWebApkResponse); | |
144 if (!response->ParseFromString(response_string)) { | |
145 OnFailure(); | |
146 return; | |
147 } | |
148 | |
149 if (response->signed_download_url().empty() || | |
150 response->webapk_package_name().empty()) { | |
151 OnFailure(); | |
152 return; | |
153 } | |
154 OnGotWebApkDownloadUrl(response->signed_download_url(), | |
155 response->webapk_package_name()); | |
156 } | |
157 | |
158 void WebApkInstaller::InitializeRequestContextGetterOnUIThread( | |
159 content::BrowserContext* browser_context) { | |
160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
161 // Must be called on UI thread. | |
gone
2016/07/29 21:20:28
Not sure if you get anything by having this commen
pkotwicz
2016/08/02 04:09:37
I have changed the comment to say that Profile::Ge
| |
162 request_context_getter_ = | |
163 Profile::FromBrowserContext(browser_context)->GetRequestContext(); | |
164 | |
165 content::BrowserThread::PostTask( | |
166 content::BrowserThread::IO, FROM_HERE, | |
167 base::Bind(&WebApkInstaller::SendCreateWebApkRequest, | |
168 io_weak_ptr_factory_.GetWeakPtr())); | |
169 } | |
170 | |
171 void WebApkInstaller::SendCreateWebApkRequest() { | |
172 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
173 std::unique_ptr<webapk::CreateWebApkRequest> request = | |
174 BuildCreateWebApkRequest(); | |
175 | |
176 timer_.Start(FROM_HERE, | |
177 base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs), | |
178 base::Bind(&WebApkInstaller::OnTimeout, | |
179 io_weak_ptr_factory_.GetWeakPtr())); | |
180 | |
181 url_fetcher_ = | |
182 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); | |
183 url_fetcher_->SetRequestContext(request_context_getter_); | |
184 std::string serialized_request; | |
185 request->SerializeToString(&serialized_request); | |
186 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); | |
187 url_fetcher_->Start(); | |
188 } | |
189 | |
190 void WebApkInstaller::OnGotWebApkDownloadUrl(const std::string& download_url, | |
191 const std::string& package_name) { | |
192 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
193 | |
194 base::FilePath output_dir; | |
195 base::android::GetCacheDirectory(&output_dir); | |
196 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory | |
197 // directory. | |
198 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted. | |
199 | |
200 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kDownloadTimeoutMs), | |
201 base::Bind(&WebApkInstaller::OnTimeout, | |
202 io_weak_ptr_factory_.GetWeakPtr())); | |
203 | |
204 base::FilePath output_path = output_dir.AppendASCII(package_name); | |
205 downloader_.reset(new FileDownloader( | |
206 GURL(download_url), output_path, true, request_context_getter_, | |
207 base::Bind(&WebApkInstaller::OnWebApkDownloaded, | |
208 io_weak_ptr_factory_.GetWeakPtr(), output_path, | |
209 package_name))); | |
210 } | |
211 | |
212 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path, | |
213 const std::string& package_name, | |
214 FileDownloader::Result result) { | |
215 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
216 | |
217 timer_.Stop(); | |
218 | |
219 if (result != FileDownloader::DOWNLOADED) { | |
220 OnFailure(); | |
221 return; | |
222 } | |
223 | |
224 JNIEnv* env = base::android::AttachCurrentThread(); | |
225 base::android::ScopedJavaLocalRef<jstring> java_file_path = | |
226 base::android::ConvertUTF8ToJavaString(env, file_path.value()); | |
227 base::android::ScopedJavaLocalRef<jstring> java_package_name = | |
228 base::android::ConvertUTF8ToJavaString(env, package_name); | |
229 bool success = | |
230 StartDownloadedWebApkInstall(env, java_file_path, java_package_name); | |
231 if (success) | |
232 OnSuccess(); | |
233 else | |
234 OnFailure(); | |
235 } | |
236 | |
237 std::unique_ptr<webapk::CreateWebApkRequest> | |
238 WebApkInstaller::BuildCreateWebApkRequest() { | |
239 std::unique_ptr<webapk::CreateWebApkRequest> request( | |
240 new webapk::CreateWebApkRequest); | |
241 | |
242 webapk::WebApk* webapk = request->mutable_webapk(); | |
243 webapk->set_manifest_url(shortcut_info_.manifest_url.spec()); | |
244 webapk->set_requester_application_package( | |
245 base::android::BuildInfo::GetInstance()->package_name()); | |
246 webapk->set_requester_application_version(version_info::GetVersionNumber()); | |
247 | |
248 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | |
249 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name)); | |
250 web_app_manifest->set_short_name( | |
251 base::UTF16ToUTF8(shortcut_info_.short_name)); | |
252 web_app_manifest->set_start_url(shortcut_info_.url.spec()); | |
253 web_app_manifest->set_orientation( | |
254 content::WebScreenOrientationLockTypeToString( | |
255 shortcut_info_.orientation)); | |
256 web_app_manifest->set_display_mode( | |
257 content::WebDisplayModeToString(shortcut_info_.display)); | |
258 web_app_manifest->set_background_color( | |
259 ColorToString(shortcut_info_.background_color)); | |
260 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color)); | |
261 | |
262 std::string* scope = web_app_manifest->add_scopes(); | |
263 scope->assign(GetScope(shortcut_info_).spec()); | |
264 webapk::Image* image = web_app_manifest->add_icons(); | |
265 image->set_src(shortcut_info_.icon_url.spec()); | |
266 // TODO(pkotwicz): Get MD5 hash of untransformed icon's bytes (with no | |
267 // encoding/decoding). | |
268 image->set_hash(ComputeBitmapHash(shortcut_icon_)); | |
269 std::vector<unsigned char> png_bytes; | |
270 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes); | |
271 image->set_image_data(&png_bytes.front(), png_bytes.size()); | |
272 | |
273 return request; | |
274 } | |
275 | |
276 void WebApkInstaller::OnTimeout() { | |
277 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
278 OnFailure(); | |
279 } | |
280 | |
281 void WebApkInstaller::OnSuccess() { | |
282 finish_callback_.Run(true); | |
283 delete this; | |
284 } | |
285 | |
286 void WebApkInstaller::OnFailure() { | |
287 finish_callback_.Run(false); | |
288 delete this; | |
289 } | |
OLD | NEW |