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

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_impl22_no_content 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 "jni/WebApkInstaller_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;
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 void WebApkInstaller::InstallAsync(content::BrowserContext* browser_context,
94 const FinishCallback& finish_callback) {
95 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
96 finish_callback_ = finish_callback;
97 // base::Unretained() is safe because WebApkInstaller owns itself and does not
98 // start the timeout timer till after
99 // InitializeRequestContextGetterOnUIThread() is called.
100 content::BrowserThread::PostTask(
101 content::BrowserThread::UI, FROM_HERE,
102 base::Bind(&WebApkInstaller::InitializeRequestContextGetterOnUIThread,
103 base::Unretained(this), browser_context));
104 }
105
106 void WebApkInstaller::InstallAsyncWithURLRequestContextGetter(
107 net::URLRequestContextGetter* request_context_getter,
108 const FinishCallback& finish_callback) {
109 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
110 request_context_getter_ = request_context_getter;
111 finish_callback_ = finish_callback;
112
113 SendCreateWebApkRequest();
114 }
115
116 bool WebApkInstaller::StartDownloadedWebApkInstall(
117 JNIEnv* env,
118 const base::android::ScopedJavaLocalRef<jstring>& java_file_path,
119 const base::android::ScopedJavaLocalRef<jstring>& java_package_name) {
120 return Java_WebApkInstaller_installAsyncFromNative(env, java_file_path.obj(),
121 java_package_name.obj());
122 }
123
124 void WebApkInstaller::OnURLFetchComplete(const net::URLFetcher* source) {
125 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
126 timer_.Stop();
127
128 if (!source->GetStatus().is_success() ||
129 source->GetResponseCode() != net::HTTP_OK) {
130 OnFailure();
131 return;
132 }
133
134 std::string response_string;
135 source->GetResponseAsString(&response_string);
136
137 std::unique_ptr<webapk::CreateWebApkResponse> response(
138 new webapk::CreateWebApkResponse);
139 if (!response->ParseFromString(response_string)) {
140 OnFailure();
141 return;
142 }
143
144 if (response->signed_download_url().empty() ||
145 response->webapk_package_name().empty()) {
146 OnFailure();
147 return;
148 }
149 OnGotWebApkDownloadUrl(response->signed_download_url(),
150 response->webapk_package_name());
151 }
152
153 void WebApkInstaller::InitializeRequestContextGetterOnUIThread(
154 content::BrowserContext* browser_context) {
155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
156 // Profile::GetRequestContext() must be called on UI thread.
157 request_context_getter_ =
158 Profile::FromBrowserContext(browser_context)->GetRequestContext();
159
160 content::BrowserThread::PostTask(
161 content::BrowserThread::IO, FROM_HERE,
162 base::Bind(&WebApkInstaller::SendCreateWebApkRequest,
163 io_weak_ptr_factory_.GetWeakPtr()));
164 }
165
166 void WebApkInstaller::SendCreateWebApkRequest() {
167 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
168 std::unique_ptr<webapk::CreateWebApkRequest> request =
169 BuildCreateWebApkRequest();
170
171 timer_.Start(FROM_HERE,
172 base::TimeDelta::FromMilliseconds(kWebApkDownloadUrlTimeoutMs),
173 base::Bind(&WebApkInstaller::OnTimeout,
174 io_weak_ptr_factory_.GetWeakPtr()));
175
176 url_fetcher_ =
177 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this);
178 url_fetcher_->SetRequestContext(request_context_getter_);
179 std::string serialized_request;
180 request->SerializeToString(&serialized_request);
181 url_fetcher_->SetUploadData(kProtoMimeType, serialized_request);
182 url_fetcher_->Start();
183 }
184
185 void WebApkInstaller::OnGotWebApkDownloadUrl(const std::string& download_url,
186 const std::string& package_name) {
187 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
188
189 base::FilePath output_dir;
190 base::android::GetCacheDirectory(&output_dir);
191 // TODO(pkotwicz): Download WebAPKs into WebAPK-specific subdirectory
192 // directory.
193 // TODO(pkotwicz): Figure out when downloaded WebAPK should be deleted.
194
195 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kDownloadTimeoutMs),
196 base::Bind(&WebApkInstaller::OnTimeout,
197 io_weak_ptr_factory_.GetWeakPtr()));
198
199 base::FilePath output_path = output_dir.AppendASCII(package_name);
200 downloader_.reset(new FileDownloader(
201 GURL(download_url), output_path, true, request_context_getter_,
202 base::Bind(&WebApkInstaller::OnWebApkDownloaded,
203 io_weak_ptr_factory_.GetWeakPtr(), output_path,
204 package_name)));
205 }
206
207 void WebApkInstaller::OnWebApkDownloaded(const base::FilePath& file_path,
208 const std::string& package_name,
209 FileDownloader::Result result) {
210 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
211
212 timer_.Stop();
213
214 if (result != FileDownloader::DOWNLOADED) {
215 OnFailure();
216 return;
217 }
218
219 JNIEnv* env = base::android::AttachCurrentThread();
220 base::android::ScopedJavaLocalRef<jstring> java_file_path =
221 base::android::ConvertUTF8ToJavaString(env, file_path.value());
222 base::android::ScopedJavaLocalRef<jstring> java_package_name =
223 base::android::ConvertUTF8ToJavaString(env, package_name);
224 bool success =
225 StartDownloadedWebApkInstall(env, java_file_path, java_package_name);
226 if (success)
227 OnSuccess();
228 else
229 OnFailure();
230 }
231
232 std::unique_ptr<webapk::CreateWebApkRequest>
233 WebApkInstaller::BuildCreateWebApkRequest() {
234 std::unique_ptr<webapk::CreateWebApkRequest> request(
235 new webapk::CreateWebApkRequest);
236
237 webapk::WebApk* webapk = request->mutable_webapk();
238 webapk->set_manifest_url(shortcut_info_.manifest_url.spec());
239 webapk->set_requester_application_package(
240 base::android::BuildInfo::GetInstance()->package_name());
241 webapk->set_requester_application_version(version_info::GetVersionNumber());
242
243 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest();
244 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name));
245 web_app_manifest->set_short_name(
246 base::UTF16ToUTF8(shortcut_info_.short_name));
247 web_app_manifest->set_start_url(shortcut_info_.url.spec());
248 // TODO(pkotwicz): Add "display mode" and "orientation" to proto.
249 web_app_manifest->set_background_color(
250 ColorToString(shortcut_info_.background_color));
251 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color));
252
253 std::string* scope = web_app_manifest->add_scopes();
254 scope->assign(GetScope(shortcut_info_).spec());
255 webapk::Image* image = web_app_manifest->add_icons();
256 image->set_src(shortcut_info_.icon_url.spec());
257 // TODO(pkotwicz): Get Murmur2 hash of untransformed icon's bytes (with no
258 // encoding/decoding).
259 image->set_hash(ComputeBitmapHash(shortcut_icon_));
260 std::vector<unsigned char> png_bytes;
261 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes);
262 image->set_image_data(&png_bytes.front(), png_bytes.size());
263
264 return request;
265 }
266
267 void WebApkInstaller::OnTimeout() {
268 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
269 OnFailure();
270 }
271
272 void WebApkInstaller::OnSuccess() {
273 finish_callback_.Run(true);
274 delete this;
275 }
276
277 void WebApkInstaller::OnFailure() {
278 finish_callback_.Run(false);
279 delete this;
280 }
OLDNEW
« no previous file with comments | « chrome/browser/android/webapk/webapk_installer.h ('k') | chrome/browser/android/webapk/webapk_installer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698