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

Unified Diff: chrome/browser/android/webapk/manifest_upgrade_detector.cc

Issue 2124513002: Introduce ManifestUpgradeDetector for WebAPK to detect web manifest changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Get start_url from WebAPK's metadata and nits. 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/manifest_upgrade_detector.cc
diff --git a/chrome/browser/android/webapk/manifest_upgrade_detector.cc b/chrome/browser/android/webapk/manifest_upgrade_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..646c658baa14670ef17b78700495277563aed744
--- /dev/null
+++ b/chrome/browser/android/webapk/manifest_upgrade_detector.cc
@@ -0,0 +1,149 @@
+// 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/manifest_upgrade_detector.h"
+
+#include <jni.h>
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "chrome/browser/android/shortcut_info.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/common/manifest.h"
+#include "jni/ManifestUpgradeDetector_jni.h"
+#include "url/gurl.h"
+
+namespace {
+
+// Returns whether the given |url| is within the scope of the |scope| url.
+bool IsInScope(const GURL& url, const GURL& scope) {
+ return base::StartsWith(url.spec(), scope.spec(),
+ base::CompareCase::SENSITIVE);
+}
+
+} // namespace
+
+jlong Initialize(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ const JavaParamRef<jobject>& java_web_contents,
+ const JavaParamRef<jstring>& java_scope,
+ const JavaParamRef<jstring>& java_start_url,
+ const JavaParamRef<jstring>& java_web_manifest_url) {
+ content::WebContents* web_contents =
+ content::WebContents::FromJavaWebContents(java_web_contents);
+ GURL scope(base::android::ConvertJavaStringToUTF8(env, java_scope));
+ GURL start_url(base::android::ConvertJavaStringToUTF8(env, java_start_url));
+ GURL web_manifest_url(base::android::ConvertJavaStringToUTF8(
+ env, java_web_manifest_url));
+ ManifestUpgradeDetector* manifest_upgrade_detector =
+ new ManifestUpgradeDetector(env, obj, web_contents, scope, start_url,
+ web_manifest_url);
+ return reinterpret_cast<intptr_t>(manifest_upgrade_detector);
+}
+
+ManifestUpgradeDetector::ManifestUpgradeDetector(
+ JNIEnv* env,
+ jobject obj,
+ content::WebContents* web_contents,
+ const GURL& scope,
+ const GURL& start_url,
+ const GURL& web_manifest_url)
+ : content::WebContentsObserver(web_contents),
+ started_(false),
+ scope_(scope),
+ start_url_(start_url),
Yaron 2016/07/20 02:27:59 where is this used?
Xi Han 2016/07/20 18:55:00 Thanks for catching this. I forgot why I initially
+ web_manifest_url_(web_manifest_url),
+ weak_ptr_factory_(this) {
+ java_ref_.Reset(env, obj);
+}
+
+ManifestUpgradeDetector::~ManifestUpgradeDetector() {
+}
+
+// static
+bool ManifestUpgradeDetector::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+void ManifestUpgradeDetector::ReplaceWebContents(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ const JavaParamRef<jobject>& jweb_contents) {
+ content::WebContents* web_contents =
+ content::WebContents::FromJavaWebContents(jweb_contents);
+ content::WebContentsObserver::Observe(web_contents);
+}
+
+void ManifestUpgradeDetector::Destroy(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ delete this;
+}
+
+void ManifestUpgradeDetector::Start(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ started_ = true;
+}
+
+void ManifestUpgradeDetector::DidFinishLoad(
+ content::RenderFrameHost* render_frame_host,
+ const GURL& validated_url) {
+ if (render_frame_host->GetParent())
+ return;
+ if (!started_ || !IsInScope(validated_url, scope_))
+ return;
+
+ content::WebContents* web_contents =
+ content::WebContentsObserver::web_contents();
pkotwicz 2016/07/19 18:02:39 Nit: Remove lines 97 - 98 and replace line 99 with
Xi Han 2016/07/20 18:55:00 Done.
+ web_contents->GetManifest(
+ base::Bind(&ManifestUpgradeDetector::OnDidGetManifest,
+ weak_ptr_factory_.GetWeakPtr()));
pkotwicz 2016/07/19 18:02:39 Nit: indentation looks off. (Not sure though)
Xi Han 2016/07/20 18:55:00 Done.
+}
+
+void ManifestUpgradeDetector::OnDidGetManifest(
+ const GURL& manifest_url,
+ const content::Manifest& manifest) {
+ // If the manifest is empty, it means the current WebContents doesn't
+ // associate with a Web Manifest. In such case, we ignore the empty manifest
+ // and continue observing the WebContents's loading until we find a page that
Yaron 2016/07/20 02:27:59 i'm a bit confused by this. A page should only hav
Xi Han 2016/07/20 18:55:00 Yes, it aims to handle multiple pages navigation w
+ // links to the Web Manifest that we are looking for.
+ // If the manifest URL is different from the current one, we will continue
+ // observing too. It is based on our assumption that it is invalid for
+ // web developers to cahnge the Web Manifest location. When it does
pkotwicz 2016/07/19 18:02:39 cahnge -> change
Xi Han 2016/07/20 18:55:00 Done.
+ // change, we will treat the new Web Manifest as the one of another WebAPK.
pkotwicz 2016/07/19 18:02:39 This logic should probably be unit tested
Xi Han 2016/07/20 18:55:00 Done.
+ if (manifest.IsEmpty() || web_manifest_url_ != manifest_url)
+ return;
+
+ started_ = false;
+
+ ShortcutInfo info(GURL::EmptyGURL());
+ info.UpdateFromManifest(manifest);
+ info.manifest_url = manifest_url;
+
+ OnDataAvailable(info);
+}
+
+void ManifestUpgradeDetector::OnDataAvailable(const ShortcutInfo& info) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+
+ ScopedJavaLocalRef<jstring> java_url =
+ base::android::ConvertUTF8ToJavaString(env, info.url.spec());
+ ScopedJavaLocalRef<jstring> java_scope =
+ base::android::ConvertUTF8ToJavaString(env, info.scope.spec());
+ ScopedJavaLocalRef<jstring> java_name =
+ base::android::ConvertUTF16ToJavaString(env, info.name);
+ ScopedJavaLocalRef<jstring> java_short_name =
+ base::android::ConvertUTF16ToJavaString(env, info.short_name);
+
+ Java_ManifestUpgradeDetector_onDataAvailable(
pkotwicz 2016/07/19 18:02:39 Nit: 4 spaces indentation
Xi Han 2016/07/20 18:55:00 Done.
+ env, java_ref_.obj(),
+ java_url.obj(),
+ java_scope.obj(),
+ java_name.obj(),
+ java_short_name.obj(),
+ info.display,
+ info.orientation,
+ info.theme_color,
+ info.background_color);
+}

Powered by Google App Engine
This is Rietveld 408576698