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

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

Issue 2138973002: Initial CL for talking to the WebAPK server to generate WebAPK (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into webapk_builder_impl2 Created 4 years, 4 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
(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/shortcut_helper.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/WebApkInstaller_jni.h"
26 #include "net/http/http_status_code.h"
27 #include "net/url_request/url_fetcher.h"
28 #include "third_party/smhasher/src/MurmurHash2.h"
29 #include "ui/gfx/codec/png_codec.h"
30 #include "url/gurl.h"
31
32 namespace {
33
34 // The default WebAPK server URL.
35 const char kDefaultWebApkServerUrl[] =
36 "https://webapk.googleapis.com/v1alpha/webApks?alt=proto";
37
38 // The MIME type of the POST data sent to the server.
39 const char kProtoMimeType[] = "application/x-protobuf";
40
41 // The seed to use the murmur2 hash of the app icon.
42 const uint32_t kMurmur2HashSeed = 0;
43
44 // The number of milliseconds to wait for the WebAPK download URL from the
45 // WebAPK server.
46 const int kWebApkDownloadUrlTimeoutMs = 4000;
47
48 // The number of milliseconds to wait for the WebAPK download to complete.
49 const int kDownloadTimeoutMs = 20000;
50
51 // Returns the scope from |info| if it is specified. Otherwise, returns the
52 // default scope.
53 GURL GetScope(const ShortcutInfo& info) {
54 return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin();
55 }
56
57 // Computes a murmur2 hash of |bitmap|'s PNG encoded bytes.
58 uint64_t ComputeBitmapHash(const SkBitmap& bitmap) {
59 std::vector<unsigned char> png_bytes;
60 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes);
61 return MurmurHash64B(&png_bytes.front(), png_bytes.size(), kMurmur2HashSeed);
62 }
63
64 // Converts a color from the format specified in content::Manifest to a CSS
65 // string.
66 std::string ColorToString(int64_t color) {
67 if (color == content::Manifest::kInvalidOrMissingColor)
68 return "";
69
70 SkColor sk_color = reinterpret_cast<uint32_t&>(color);
71 int r = SkColorGetR(sk_color);
72 int g = SkColorGetG(sk_color);
73 int b = SkColorGetB(sk_color);
74 double a = SkColorGetA(sk_color) / 255.0;
75 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a);
76 }
77
78 } // anonymous namespace
79
80 WebApkInstaller::WebApkInstaller(const ShortcutInfo& shortcut_info,
81 const SkBitmap& shortcut_icon)
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(content::BrowserContext* browser_context,
100 const FinishCallback& finish_callback) {
101 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
102 finish_callback_ = finish_callback;
103 // base::Unretained() is safe because WebApkInstaller owns itself and does not
104 // start the timeout timer till after
105 // InitializeRequestContextGetterOnUIThread() is called.
106 content::BrowserThread::PostTask(
107 content::BrowserThread::UI, FROM_HERE,
108 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread,
109 base::Unretained(this), browser_context));
110 }
111
112 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter(
113 net::URLRequestContextGetter* request_context_getter,
114 const FinishCallback& finish_callback) {
115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
116 request_context_getter_ = request_context_getter;
117 finish_callback_ = finish_callback;
118
119 SendCreateWebApkRequest();
120 }
121
122 bool WebApkInstaller::StartDownloadedWebApkInstall(
123 JNIEnv* env,
124 const base::android::ScopedJavaLocalRef<jstring>& java_file_path,
125 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) {
126 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path.obj(),
127 java_package_name.obj());
128 }
129
130 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
131 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
132 timer_.Stop();
133
134 if (!source->GetStatus().is_success() ||
135 source->GetResponseCode() != net::HTTP_OK) {
136 OnFailure();
137 return;
138 }
139
140 std::string response_string;
141 source->GetResponseAsString(&response_string);
142
143 std::unique_ptr<webapk::CreateWebApkResponse> response(
144 new webapk::CreateWebApkResponse);
145 if (!response->ParseFromString(response_string)) {
146 OnFailure();
147 return;
148 }
149
150 if (response->signed_download_url().empty() ||
151 response->webapk_package_name().empty()) {
152 OnFailure();
153 return;
154 }
155 OnGotWebApkDownloadUrl(response->signed_download_url(),
156 response->webapk_package_name());
157 }
158
159 void WebApkInstaller::InitializeRequestContextGetterOnUIThread(
160 content::BrowserContext* browser_context) {
161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
162 // Profile::GetRequestContext() must be called on UI thread.
163 request_context_getter_ =
164 Profile::FromBrowserContext(browser_context)->GetRequestContext();
165
166 content::BrowserThread::PostTask(
167 content::BrowserThread::IO, FROM_HERE,
168 base::Bind(&WebApkInstaller::SendCreateWebApkRequest,
169 io_weak_ptr_factory_.GetWeakPtr()));
170 }
171
172 void WebApkInstaller::SendCreateWebApkRequest() {
173 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
174 std::unique_ptr<webapk::CreateWebApkRequest> request =
175 BuildCreateWebApkRequest();
176
177 timer_.Start(FROM_HERE,
178 base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs),
179 base::Bind(&WebApkInstaller::OnTimeout,
180 io_weak_ptr_factory_.GetWeakPtr()));
181
182 url_fetcher_ =
183 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this);
184 url_fetcher_->SetRequestContext(request_context_getter_);
185 std::string serialized_request;
186 request->SerializeToString(&serialized_request);
187 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request);
188 url_fetcher_->Start();
189 }
190
191 void WebApkInstaller::OnGotWebApkDownloadUrl(const std::string& download_url,
192 const std::string& package_name) {
193 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
194
195 base::FilePath output_dir;
196 base::android::GetCacheDirectory(&output_dir);
197 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory
198 // directory.
199 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted.
200
201 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kDownloadTimeoutMs),
202 base::Bind(&WebApkInstaller::OnTimeout,
203 io_weak_ptr_factory_.GetWeakPtr()));
204
205 base::FilePath output_path = output_dir.AppendASCII(package_name);
206 downloader_.reset(new FileDownloader(
207 GURL(download_url), output_path, true, request_context_getter_,
208 base::Bind(&WebApkInstaller::OnWebApkDownloaded,
209 io_weak_ptr_factory_.GetWeakPtr(), output_path,
210 package_name)));
211 }
212
213 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path,
214 const std::string& package_name,
215 FileDownloader::Result result) {
216 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
217
218 timer_.Stop();
219
220 if (result != FileDownloader::DOWNLOADED) {
221 OnFailure();
222 return;
223 }
224
225 JNIEnv* env = base::android::AttachCurrentThread();
226 base::android::ScopedJavaLocalRef<jstring> java_file_path =
227 base::android::ConvertUTF8ToJavaString(env, file_path.value());
228 base::android::ScopedJavaLocalRef<jstring> java_package_name =
229 base::android::ConvertUTF8ToJavaString(env, package_name);
230 bool success =
231 StartDownloadedWebApkInstall(env, java_file_path, java_package_name);
232 if (success)
233 OnSuccess();
234 else
235 OnFailure();
236 }
237
238 std::unique_ptr<webapk::CreateWebApkRequest>
239 WebApkInstaller::BuildCreateWebApkRequest() {
240 std::unique_ptr<webapk::CreateWebApkRequest> request(
241 new webapk::CreateWebApkRequest);
242
243 webapk::WebApk* webapk = request->mutable_webapk();
244 webapk->set_manifest_url(shortcut_info_.manifest_url.spec());
245 webapk->set_requester_application_package(
246 base::android::BuildInfo::GetInstance()->package_name());
247 webapk->set_requester_application_version(version_info::GetVersionNumber());
248
249 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest();
250 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name));
251 web_app_manifest->set_short_name(
252 base::UTF16ToUTF8(shortcut_info_.short_name));
253 web_app_manifest->set_start_url(shortcut_info_.url.spec());
254 web_app_manifest->set_orientation(
255 content::WebScreenOrientationLockTypeToString(
256 shortcut_info_.orientation));
257 web_app_manifest->set_display_mode(
258 content::WebDisplayModeToString(shortcut_info_.display));
259 web_app_manifest->set_background_color(
260 ColorToString(shortcut_info_.background_color));
261 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color));
262
263 std::string* scope = web_app_manifest->add_scopes();
264 scope->assign(GetScope(shortcut_info_).spec());
265 webapk::Image* image = web_app_manifest->add_icons();
266 image->set_src(shortcut_info_.icon_url.spec());
267 // TODO(pkotwicz): Get Murmur2 hash of untransformed icon's bytes (with no
268 // encoding/decoding).
269 image->set_hash(ComputeBitmapHash(shortcut_icon_));
270 std::vector<unsigned char> png_bytes;
271 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes);
272 image->set_image_data(&png_bytes.front(), png_bytes.size());
273
274 return request;
275 }
276
277 void WebApkInstaller::OnTimeout() {
278 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
279 OnFailure();
280 }
281
282 void WebApkInstaller::OnSuccess() {
283 finish_callback_.Run(true);
284 delete this;
285 }
286
287 void WebApkInstaller::OnFailure() {
288 finish_callback_.Run(false);
289 delete this;
290 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698