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..e5ddee38c172e067f942312d3d4826669b9a7d47 |
--- /dev/null |
+++ b/chrome/browser/android/webapk/webapk_builder.cc |
@@ -0,0 +1,258 @@ |
+// 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/stringprintf.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 "components/version_info/version_info.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "jni/WebApkBuilder_jni.h" |
+#include "net/http/http_status_code.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"; |
ScottK
2016/07/22 15:24:27
this is going to be "https://webapk.googleapis.com
|
+ |
+// The MIME type of the POST data sent to the server. |
+const char kProtoMimeType[] = "application/octet-stream"; |
ScottK
2016/07/22 15:24:27
Turns out this needs to be: "application/x-protobu
|
+ |
+// 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), |
+ weak_ptr_factory_(this) { |
+ 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"; |
+ } |
+} |
+ |
+// static |
+std::string WebApkBuilder::ColorToString(int64_t color) { |
+ if (color == content::Manifest::kInvalidOrMissingColor) |
+ return ""; |
+ |
+ SkColor sk_color = reinterpret_cast<uint32_t&>(color); |
+ int r = SkColorGetR(sk_color); |
+ int g = SkColorGetG(sk_color); |
+ int b = SkColorGetB(sk_color); |
+ double a = SkColorGetA(sk_color) / 255.0; |
+ return base::StringPrintf("rgba(%d,%d,%d,%.2f)", r, g, b, a); |
+} |
+ |
+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, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void WebApkBuilder::OnURLFetchComplete(const net::URLFetcher* source) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ timer_.Stop(); |
+ |
+ if (!source->GetStatus().is_success() || |
+ source->GetResponseCode() != net::HTTP_OK) { |
+ 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, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void WebApkBuilder::SendCreateWebApkRequest() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ std::unique_ptr<webapk::CreateWebApkRequest> request = |
+ BuildCreateWebApkRequest(); |
+ |
+ timer_.Start( |
+ FROM_HERE, base::TimeDelta::FromMilliseconds(kTimeoutMs), |
+ base::Bind(&WebApkBuilder::OnFailure, weak_ptr_factory_.GetWeakPtr())); |
+ |
+ 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::BuildCreateWebApkRequest() { |
+ 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->set_requester_application_version(version_info::GetVersionNumber()); |
+ |
+ 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( |
+ ColorToString(shortcut_info_.background_color)); |
+ web_app_manifest->set_theme_color(ColorToString(shortcut_info_.theme_color)); |
+ |
+ std::string* scope = web_app_manifest->add_scopes(); |
+ scope->assign(GetScope(shortcut_info_).spec()); |
+ webapk::Image* image = web_app_manifest->add_icons(); |
+ 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() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ OnFailure(); |
Yaron
2016/07/20 13:13:50
should this be posted to ui thread? I thought succ
pkotwicz
2016/07/23 00:11:04
WebApkBuilder is created and started on the IO thr
|
+} |
+ |
+void WebApkBuilder::OnSuccess() { |
+ finish_callback_.Run(true); |
+ delete this; |
+} |
+ |
+void WebApkBuilder::OnFailure() { |
+ finish_callback_.Run(false); |
+ delete this; |
+} |