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_builder.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/bind.h" | |
11 #include "base/command_line.h" | |
12 #include "base/md5.h" | |
13 #include "base/strings/string_util.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "chrome/browser/android/webapk/webapk.pb.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "jni/WebApkBuilder_jni.h" | |
21 #include "net/url_request/url_fetcher.h" | |
22 #include "ui/gfx/codec/png_codec.h" | |
23 #include "url/gurl.h" | |
24 | |
25 namespace { | |
26 | |
27 // The default WebAPK server URL. | |
28 const char kDefaultWebApkServerUrl[] = "http://www.awesomeserver.appspot.com"; | |
29 | |
30 // The MIME type of the POST data sent to the server. | |
31 const char kProtoMimeType[] = "application/octet-stream"; | |
32 | |
33 // The number of milliseconds to wait for a response from the server. | |
34 const int kTimeoutMs = 4000; | |
35 | |
36 // Returns the scope from |info| if it is specified. Otherwise, returns the | |
37 // default scope. | |
38 GURL GetScope(const ShortcutInfo& info) { | |
39 return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin(); | |
40 } | |
41 | |
42 // Computes a MD5 hash of |bitmap|'s PNG encoded bytes. | |
43 std::string ComputeBitmapHash(const SkBitmap& bitmap) { | |
44 std::vector<unsigned char> png_bytes; | |
45 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); | |
46 base::MD5Digest digest; | |
47 base::MD5Sum(&png_bytes.front(), png_bytes.size(), &digest); | |
48 return base::MD5DigestToBase16(digest); | |
49 } | |
50 | |
51 } // anonymous namespace | |
52 | |
53 WebApkBuilder::WebApkBuilder(content::BrowserContext* browser_context, | |
54 const ShortcutInfo& shortcut_info, | |
55 const SkBitmap& shortcut_icon) | |
56 : browser_context_(browser_context), | |
57 shortcut_info_(shortcut_info), | |
58 shortcut_icon_(shortcut_icon) { | |
59 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
60 server_url_ = | |
61 GURL(command_line->HasSwitch(switches::kWebApkServerUrl) | |
62 ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) | |
63 : kDefaultWebApkServerUrl); | |
64 } | |
65 | |
66 WebApkBuilder::~WebApkBuilder() {} | |
67 | |
68 // static | |
69 bool WebApkBuilder::Register(JNIEnv* env) { | |
70 return RegisterNativesImpl(env); | |
71 } | |
72 | |
73 // static | |
74 std::string WebApkBuilder::DisplayToString(blink::WebDisplayMode display) { | |
75 switch (display) { | |
76 case blink::WebDisplayModeUndefined: | |
77 return ""; | |
78 case blink::WebDisplayModeBrowser: | |
79 return "browser"; | |
80 case blink::WebDisplayModeMinimalUi: | |
81 return "minimal-ui"; | |
82 case blink::WebDisplayModeStandalone: | |
83 return "standalone"; | |
84 case blink::WebDisplayModeFullscreen: | |
85 return "fullscreen"; | |
86 } | |
87 } | |
88 | |
89 // static | |
90 std::string WebApkBuilder::OrientationToString( | |
91 blink::WebScreenOrientationLockType orientation) { | |
92 switch (orientation) { | |
93 case blink::WebScreenOrientationLockDefault: | |
94 return ""; | |
95 case blink::WebScreenOrientationLockPortraitPrimary: | |
96 return "portrait-primary"; | |
97 case blink::WebScreenOrientationLockPortraitSecondary: | |
98 return "portrait-secondary"; | |
99 case blink::WebScreenOrientationLockLandscapePrimary: | |
100 return "landscape-primary"; | |
101 case blink::WebScreenOrientationLockLandscapeSecondary: | |
102 return "landscape-secondary"; | |
103 case blink::WebScreenOrientationLockAny: | |
104 return "any"; | |
105 case blink::WebScreenOrientationLockLandscape: | |
106 return "landscape"; | |
107 case blink::WebScreenOrientationLockPortrait: | |
108 return "portrait"; | |
109 case blink::WebScreenOrientationLockNatural: | |
110 return "natural"; | |
111 } | |
112 } | |
113 | |
114 // static | |
115 std::string WebApkBuilder::ColorToString(int64_t color) { | |
116 if (color == content::Manifest::kInvalidOrMissingColor) | |
117 return ""; | |
118 | |
119 SkColor sk_color = reinterpret_cast<uint32_t&>(color); | |
120 int r = SkColorGetR(sk_color); | |
121 int g = SkColorGetG(sk_color); | |
122 int b = SkColorGetB(sk_color); | |
123 double a = SkColorGetA(sk_color) / 255.0; | |
124 return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); | |
scottkirkwood
2016/07/19 17:59:14
Glenn's parser supports rgb(), not rgba()
pkotwicz
2016/07/20 05:13:41
I looked at the source code to Glenn's parser and
scottkirkwood
2016/07/20 13:16:05
Yes, would be easy for Glenn to add and would make
| |
125 } | |
126 | |
127 void WebApkBuilder::BuildAsync(const FinishCallback& finish_callback) { | |
128 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
129 finish_callback_ = finish_callback; | |
130 content::BrowserThread::PostTask( | |
131 content::BrowserThread::UI, FROM_HERE, | |
132 base::Bind(&WebApkBuilder::InitializeRequestContextGetterOnUIThread, | |
133 base::Unretained(this))); | |
Xi Han
2016/07/14 14:53:16
Please document here why base::Unretatined(this) w
pkotwicz
2016/07/20 05:13:41
base::Unretained() is safe wherever it is used in
| |
134 } | |
135 | |
136 void WebApkBuilder::OnURLFetchComplete(const net::URLFetcher* source) { | |
137 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
138 timer_.Stop(); | |
139 | |
140 if (!source->GetStatus().is_success() || source->GetResponseCode() != 200) { | |
Xi Han
2016/07/14 14:53:16
net::HTTP_OK?
| |
141 OnFailure(); | |
142 return; | |
143 } | |
144 | |
145 std::string response_string; | |
146 source->GetResponseAsString(&response_string); | |
147 | |
148 std::unique_ptr<webapk::CreateWebApkResponse> response( | |
149 new webapk::CreateWebApkResponse); | |
150 if (!response->ParseFromString(response_string)) { | |
151 OnFailure(); | |
152 return; | |
153 } | |
154 | |
155 if (response->signed_market_url().empty()) { | |
156 OnFailure(); | |
157 return; | |
158 } | |
159 OnGotMarketUrl(response->signed_market_url()); | |
160 } | |
161 | |
162 void WebApkBuilder::InitializeRequestContextGetterOnUIThread() { | |
163 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
164 // Must be called on UI thread. | |
165 request_context_getter_ = | |
166 Profile::FromBrowserContext(browser_context_)->GetRequestContext(); | |
167 | |
168 content::BrowserThread::PostTask( | |
169 content::BrowserThread::IO, FROM_HERE, | |
170 base::Bind(&WebApkBuilder::SendCreateWebApkRequest, | |
171 base::Unretained(this))); | |
Xi Han
2016/07/14 14:53:16
Same here.
| |
172 } | |
173 | |
174 void WebApkBuilder::SendCreateWebApkRequest() { | |
175 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
176 std::unique_ptr<webapk::CreateWebApkRequest> request = | |
177 BuildCreateWebApkRequest(); | |
178 | |
179 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeoutMs), | |
180 base::Bind(&WebApkBuilder::OnFailure, base::Unretained(this))); | |
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 WebApkBuilder::OnGotMarketUrl(const std::string& signed_market_url) { | |
192 JNIEnv* env = base::android::AttachCurrentThread(); | |
193 base::android::ScopedJavaLocalRef<jstring> java_signed_market_url = | |
194 base::android::ConvertUTF8ToJavaString(env, signed_market_url); | |
195 bool success = Java_WebApkBuilder_downloadAndInstallAsync( | |
196 env, java_signed_market_url.obj()); | |
197 if (success) | |
198 OnSuccess(); | |
199 else | |
200 OnFailure(); | |
201 } | |
202 | |
203 std::unique_ptr<webapk::CreateWebApkRequest> | |
204 WebApkBuilder::BuildCreateWebApkRequest() { | |
205 std::unique_ptr<webapk::CreateWebApkRequest> request( | |
206 new webapk::CreateWebApkRequest); | |
207 | |
208 webapk::WebApk* webapk = request->mutable_webapk(); | |
209 webapk->set_manifest_url(shortcut_info_.manifest_url.spec()); | |
210 webapk->set_requester_application_package( | |
211 base::android::BuildInfo::GetInstance()->package_name()); | |
212 | |
213 webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); | |
214 web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name)); | |
215 web_app_manifest->set_short_name( | |
216 base::UTF16ToUTF8(shortcut_info_.short_name)); | |
217 web_app_manifest->set_start_url(shortcut_info_.url.spec()); | |
218 web_app_manifest->set_orientation( | |
219 OrientationToString(shortcut_info_.orientation)); | |
220 web_app_manifest->set_display_mode(DisplayToString(shortcut_info_.display)); | |
221 web_app_manifest->set_background_color( | |
222 ColorToString(shortcut_info_.background_color)); | |
223 web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color)); | |
224 | |
225 std::string* scope = web_app_manifest->add_scopes(); | |
226 scope->assign(GetScope(shortcut_info_).spec()); | |
227 webapk::Image* image = web_app_manifest->add_icons(); | |
228 image->set_src(shortcut_info_.icon_url.spec()); | |
229 // TODO(pkotwicz): Get MD5 hash of untransformed icon's bytes (with no | |
230 // encoding/decoding). | |
231 image->set_hash(ComputeBitmapHash(shortcut_icon_)); | |
232 std::vector<unsigned char> png_bytes; | |
233 gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes); | |
234 image->set_image_data(&png_bytes.front(), png_bytes.size()); | |
235 | |
236 return request; | |
237 } | |
238 | |
239 void WebApkBuilder::OnTimeout() { | |
240 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
241 OnFailure(); | |
242 } | |
243 | |
244 void WebApkBuilder::OnSuccess() { | |
245 finish_callback_.Run(true); | |
246 delete this; | |
247 } | |
248 | |
249 void WebApkBuilder::OnFailure() { | |
250 finish_callback_.Run(false); | |
251 delete this; | |
252 } | |
OLD | NEW |