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

Unified Diff: chrome/browser/android/webapk/webapk_builder.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 'webapk_builder_impl0' into webapk_builder_impl2 Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
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..b62b0829e4683570ea5fcb03b9c1a54045d58644
--- /dev/null
+++ b/chrome/browser/android/webapk/webapk_builder.cc
@@ -0,0 +1,252 @@
+// 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 "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";
+ }
+}
+
+// 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);
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
+}
+
+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)));
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
+}
+
+void WebApkBuilder::OnURLFetchComplete(const net::URLFetcher* source) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
+ timer_.Stop();
+
+ if (!source->GetStatus().is_success() || source->GetResponseCode() != 200) {
Xi Han 2016/07/14 14:53:16 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,
+ base::Unretained(this)));
Xi Han 2016/07/14 14:53:16 Same here.
+}
+
+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, 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::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::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();
+}
+
+void WebApkBuilder::OnSuccess() {
+ finish_callback_.Run(true);
+ delete this;
+}
+
+void WebApkBuilder::OnFailure() {
+ finish_callback_.Run(false);
+ delete this;
+}

Powered by Google App Engine
This is Rietveld 408576698