Index: chrome/browser/android/webapk/webapk_builder.cc |
diff --git a/chrome/browser/android/webapk/webapk_builder.cc b/chrome/browser/android/webapk/webapk_builder.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..afd41194095ecfb36fd2eed82b3453284c858f62 |
--- /dev/null |
+++ b/chrome/browser/android/webapk/webapk_builder.cc |
@@ -0,0 +1,235 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/android/webapk/webapk_builder.h" |
+ |
+#include "base/android/build_info.h" |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_string.h" |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/md5.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "chrome/browser/android/webapk/webapk.pb.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/common/chrome_switches.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "jni/WebApkBuilder_jni.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "ui/gfx/codec/png_codec.h" |
+#include "url/gurl.h" |
+ |
+namespace { |
+ |
+// The default WebAPK server URL. |
+const char kDefaultWebApkServerUrl[] = "http://www.awesomeserver.appspot.com"; |
+ |
+// The MIME type of the POST data sent to the server. |
+const char kProtoMimeType[] = "application/octet-stream"; |
+ |
+// The number of milliseconds to wait for a response from the server. |
+const int kTimeoutMs = 4000; |
+ |
+// Returns the scope from |info| if it is specified. Otherwise, returns the |
+// default scope. |
+GURL GetScope(const ShortcutInfo& info) { |
+ return (info.scope.is_valid()) ? info.scope : info.url.GetOrigin(); |
+} |
+ |
+// Computes a MD5 hash of |bitmap|'s PNG encoded bytes. |
+std::string ComputeBitmapHash(const SkBitmap& bitmap) { |
+ std::vector<unsigned char> png_bytes; |
+ gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_bytes); |
+ base::MD5Digest digest; |
+ base::MD5Sum(&png_bytes.front(), png_bytes.size(), &digest); |
+ return base::MD5DigestToBase16(digest); |
+} |
+ |
+} // anonymous namespace |
+ |
+WebApkBuilder::WebApkBuilder(content::BrowserContext* browser_context, |
+ const ShortcutInfo& shortcut_info, |
+ const SkBitmap& shortcut_icon) |
+ : browser_context_(browser_context), |
+ shortcut_info_(shortcut_info), |
+ shortcut_icon_(shortcut_icon) { |
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
+ server_url_ = |
+ GURL(command_line->HasSwitch(switches::kWebApkServerUrl) |
+ ? command_line->GetSwitchValueASCII(switches::kWebApkServerUrl) |
+ : kDefaultWebApkServerUrl); |
+} |
+ |
+WebApkBuilder::~WebApkBuilder() {} |
+ |
+// static |
+bool WebApkBuilder::Register(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |
+ |
+// static |
+std::string WebApkBuilder::DisplayToString(blink::WebDisplayMode display) { |
+ switch (display) { |
+ case blink::WebDisplayModeUndefined: |
+ return ""; |
+ case blink::WebDisplayModeBrowser: |
+ return "browser"; |
+ case blink::WebDisplayModeMinimalUi: |
+ return "minimal-ui"; |
+ case blink::WebDisplayModeStandalone: |
+ return "standalone"; |
+ case blink::WebDisplayModeFullscreen: |
+ return "fullscreen"; |
+ } |
+} |
+ |
+// static |
+std::string WebApkBuilder::OrientationToString( |
+ blink::WebScreenOrientationLockType orientation) { |
+ switch (orientation) { |
+ case blink::WebScreenOrientationLockDefault: |
+ return ""; |
+ case blink::WebScreenOrientationLockPortraitPrimary: |
+ return "portrait-primary"; |
+ case blink::WebScreenOrientationLockPortraitSecondary: |
+ return "portrait-secondary"; |
+ case blink::WebScreenOrientationLockLandscapePrimary: |
+ return "landscape-primary"; |
+ case blink::WebScreenOrientationLockLandscapeSecondary: |
+ return "landscape-secondary"; |
+ case blink::WebScreenOrientationLockAny: |
+ return "any"; |
+ case blink::WebScreenOrientationLockLandscape: |
+ return "landscape"; |
+ case blink::WebScreenOrientationLockPortrait: |
+ return "portrait"; |
+ case blink::WebScreenOrientationLockNatural: |
+ return "natural"; |
+ } |
+} |
+ |
+void WebApkBuilder::BuildAsync(const FinishCallback& finish_callback) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ finish_callback_ = finish_callback; |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&WebApkBuilder::InitializeRequestContextGetterOnUIThread, |
+ base::Unretained(this))); |
+} |
+ |
+void WebApkBuilder::OnURLFetchComplete(const net::URLFetcher* source) { |
+ timer_.Stop(); |
Yaron
2016/07/13 00:24:01
DCHECK ui thread (can you add remaining DCHECKs on
pkotwicz
2016/07/13 02:15:31
Done.
|
+ |
+ if (!source->GetStatus().is_success() || source->GetResponseCode() != 200) { |
+ OnFailure(); |
+ return; |
+ } |
+ |
+ std::string response_string; |
+ source->GetResponseAsString(&response_string); |
+ |
+ std::unique_ptr<webapk::CreateWebApkResponse> response( |
+ new webapk::CreateWebApkResponse); |
+ if (!response->ParseFromString(response_string)) { |
+ OnFailure(); |
+ return; |
+ } |
+ |
+ if (response->signed_market_url().empty()) { |
+ OnFailure(); |
+ return; |
+ } |
+ OnGotMarketUrl(response->signed_market_url()); |
+} |
+ |
+void WebApkBuilder::InitializeRequestContextGetterOnUIThread() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ // Must be called on UI thread. |
+ request_context_getter_ = |
+ Profile::FromBrowserContext(browser_context_)->GetRequestContext(); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&WebApkBuilder::SendCreateWebApkRequest, |
+ base::Unretained(this))); |
+} |
+ |
+void WebApkBuilder::SendCreateWebApkRequest() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ std::unique_ptr<webapk::CreateWebApkRequest> request = |
+ MakeCreateWebApkRequest(); |
+ |
+ timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeoutMs), |
+ base::Bind(&WebApkBuilder::OnFailure, base::Unretained(this))); |
+ |
+ url_fetcher_ = |
+ net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this); |
+ url_fetcher_->SetRequestContext(request_context_getter_); |
+ std::string serialized_request; |
+ request->SerializeToString(&serialized_request); |
+ url_fetcher_->SetUploadData(kProtoMimeType, serialized_request); |
+ url_fetcher_->Start(); |
+} |
+ |
+void WebApkBuilder::OnGotMarketUrl(const std::string& signed_market_url) { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ base::android::ScopedJavaLocalRef<jstring> java_signed_market_url = |
+ base::android::ConvertUTF8ToJavaString(env, signed_market_url); |
+ bool success = Java_WebApkBuilder_downloadAndInstallAsync( |
+ env, java_signed_market_url.obj()); |
+ if (success) |
+ OnSuccess(); |
+ else |
+ OnFailure(); |
+} |
+ |
+std::unique_ptr<webapk::CreateWebApkRequest> |
+WebApkBuilder::MakeCreateWebApkRequest() { |
Yaron
2016/07/13 00:24:01
Nit: WebApkBuilder::BuildCreate.. ? (since it's a
pkotwicz
2016/07/13 02:15:31
BuildCreate it is
|
+ std::unique_ptr<webapk::CreateWebApkRequest> request( |
+ new webapk::CreateWebApkRequest); |
+ |
+ webapk::WebApk* webapk = request->mutable_webapk(); |
+ webapk->set_manifest_url(shortcut_info_.manifest_url.spec()); |
+ webapk->set_requester_application_package( |
+ base::android::BuildInfo::GetInstance()->package_name()); |
+ |
+ webapk::WebAppManifest* web_app_manifest = webapk->mutable_manifest(); |
+ web_app_manifest->set_name(base::UTF16ToUTF8(shortcut_info_.name)); |
+ web_app_manifest->set_short_name( |
+ base::UTF16ToUTF8(shortcut_info_.short_name)); |
+ web_app_manifest->set_start_url(shortcut_info_.url.spec()); |
+ web_app_manifest->set_orientation( |
+ OrientationToString(shortcut_info_.orientation)); |
+ web_app_manifest->set_display_mode(DisplayToString(shortcut_info_.display)); |
+ web_app_manifest->set_background_color(shortcut_info_.background_color); |
+ web_app_manifest->set_theme_color(shortcut_info_.theme_color); |
+ |
+ std::string* scope = web_app_manifest->add_scope(); |
+ scope->assign(GetScope(shortcut_info_).spec()); |
+ webapk::Image* image = web_app_manifest->add_icon(); |
+ image->set_src(shortcut_info_.icon_url.spec()); |
+ // TODO(pkotwicz): Get MD5 hash of untransformed icon's bytes (with no |
+ // encoding/decoding). |
+ image->set_hash(ComputeBitmapHash(shortcut_icon_)); |
+ std::vector<unsigned char> png_bytes; |
+ gfx::PNGCodec::EncodeBGRASkBitmap(shortcut_icon_, false, &png_bytes); |
+ image->set_image_data(&png_bytes.front(), png_bytes.size()); |
+ |
+ return request; |
+} |
+ |
+void WebApkBuilder::OnTimeout() { |
+ OnFailure(); |
Yaron
2016/07/13 00:24:01
what if we have an in-flight request? shouldn't it
pkotwicz
2016/07/13 02:15:31
Yes, I am relying on the destructor to cancel the
|
+} |
+ |
+void WebApkBuilder::OnSuccess() { |
+ finish_callback_.Run(true); |
+ delete this; |
+} |
+ |
+void WebApkBuilder::OnFailure() { |
+ finish_callback_.Run(false); |
+ delete this; |
+} |