Chromium Code Reviews| 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/webapps/manifest_upgrade_detector.h" | |
| 6 | |
| 7 #include <jni.h> | |
| 8 | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/android/jni_array.h" | |
| 11 #include "base/android/jni_string.h" | |
| 12 #include "chrome/browser/android/shortcut_helper.h" | |
| 13 #include "chrome/browser/banners/app_banner_manager.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/render_frame_host.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/common/manifest.h" | |
| 18 #include "jni/ManifestUpgradeDetector_jni.h" | |
| 19 #include "ui/gfx/android/java_bitmap.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 using content::Manifest; | |
| 23 | |
| 24 jlong Initialize(JNIEnv* env, | |
| 25 const JavaParamRef<jobject>& obj, | |
| 26 const JavaParamRef<jobject>& java_web_contents) { | |
| 27 content::WebContents* web_contents = | |
| 28 content::WebContents::FromJavaWebContents(java_web_contents); | |
| 29 ManifestUpgradeDetector* manifest_upgrade_detector = | |
| 30 new ManifestUpgradeDetector(env, obj, web_contents); | |
| 31 return reinterpret_cast<intptr_t>(manifest_upgrade_detector); | |
| 32 } | |
| 33 | |
| 34 ManifestUpgradeDetector::ManifestUpgradeDetector( | |
| 35 JNIEnv* env, | |
| 36 jobject obj, | |
| 37 content::WebContents* web_contents) | |
| 38 : content::WebContentsObserver(web_contents), | |
| 39 start_fetching_(false), | |
| 40 data_fetcher_(nullptr) { | |
| 41 java_ref_.Reset(env, obj); | |
| 42 } | |
| 43 | |
| 44 ManifestUpgradeDetector::~ManifestUpgradeDetector() { | |
| 45 data_fetcher_->set_weak_observer(nullptr); | |
| 46 data_fetcher_ = nullptr; | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 bool ManifestUpgradeDetector::Register(JNIEnv* env) { | |
| 51 return RegisterNativesImpl(env); | |
| 52 } | |
| 53 | |
| 54 void ManifestUpgradeDetector::DidFinishLoad( | |
| 55 content::RenderFrameHost* render_frame_host, | |
| 56 const GURL& validated_url) { | |
|
pkotwicz
2016/07/12 01:23:49
Random thought: I wonder how this works with 301 H
Xi Han
2016/07/12 20:53:41
If both urls link to the same manifest, no matter
pkotwicz
2016/07/12 21:04:28
My concern is that if the start URL is a 301 HTTP
| |
| 57 if (render_frame_host->GetParent()) | |
| 58 return; | |
| 59 if (start_fetching_) | |
| 60 RequestDetector(validated_url); | |
| 61 } | |
| 62 | |
| 63 void ManifestUpgradeDetector::ReplaceWebContents( | |
| 64 JNIEnv* env, | |
| 65 const JavaParamRef<jobject>& obj, | |
| 66 const JavaParamRef<jobject>& jweb_contents) { | |
| 67 content::WebContents* web_contents = | |
| 68 content::WebContents::FromJavaWebContents(jweb_contents); | |
| 69 content::WebContentsObserver::Observe(web_contents); | |
| 70 if (data_fetcher_.get()) | |
| 71 data_fetcher_.get()->ReplaceWebContents(web_contents); | |
| 72 } | |
| 73 | |
| 74 void ManifestUpgradeDetector::RequestDetector(const GURL& validated_url) { | |
| 75 if (data_fetcher_.get() && banners::AppBannerManager::URLsAreForTheSamePage( | |
| 76 data_fetcher_->shortcut_info().url, validated_url)) | |
| 77 return; | |
| 78 | |
| 79 if (data_fetcher_.get()) | |
| 80 data_fetcher_->set_weak_observer(nullptr); | |
| 81 data_fetcher_ = CreateDataFetcher(); | |
| 82 } | |
| 83 | |
| 84 AddToHomescreenDataFetcher* ManifestUpgradeDetector::CreateDataFetcher() { | |
| 85 return new AddToHomescreenDataFetcher(web_contents(), | |
| 86 ShortcutHelper::GetIdealHomescreenIconSizeInDp(), | |
| 87 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), | |
| 88 ShortcutHelper::GetIdealSplashImageSizeInDp(), | |
| 89 ShortcutHelper::GetMinimumSplashImageSizeInDp(), | |
| 90 true, this); | |
|
pkotwicz
2016/07/12 01:23:49
For the sake of landing something this week, I thi
Xi Han
2016/07/12 20:53:41
That is definitely a good idea.
| |
| 91 } | |
| 92 | |
| 93 void ManifestUpgradeDetector::OnDidHasManifest(bool has_manifest) { | |
| 94 start_fetching_ = !has_manifest; | |
| 95 } | |
| 96 | |
| 97 void ManifestUpgradeDetector::OnDataAvailable( | |
| 98 const ShortcutInfo& info, | |
| 99 const SkBitmap& icon, | |
| 100 const std::vector<std::string>& icons) { | |
| 101 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 102 | |
| 103 start_fetching_ = false; | |
| 104 ScopedJavaLocalRef<jstring> java_url = | |
| 105 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | |
| 106 ScopedJavaLocalRef<jstring> java_user_title = | |
| 107 base::android::ConvertUTF16ToJavaString(env, info.user_title); | |
| 108 ScopedJavaLocalRef<jstring> java_name = | |
| 109 base::android::ConvertUTF16ToJavaString(env, info.name); | |
| 110 ScopedJavaLocalRef<jstring> java_short_name = | |
| 111 base::android::ConvertUTF16ToJavaString(env, info.short_name); | |
| 112 ScopedJavaLocalRef<jobject> java_bitmap; | |
| 113 if (icon.getSize()) | |
| 114 java_bitmap = gfx::ConvertToJavaBitmap(&icon); | |
| 115 ScopedJavaLocalRef<jobjectArray> java_icons = | |
| 116 base::android::ToJavaArrayOfStrings(env, icons); | |
| 117 | |
|
pkotwicz
2016/07/12 01:23:49
We should check whether the manifest URL is the sa
Xi Han
2016/07/12 20:53:41
That was what I thought. Create a CL(https://coder
| |
| 118 Java_ManifestUpgradeDetector_onDataAvailable( | |
| 119 env, java_ref_.obj(), | |
| 120 java_url.obj(), | |
| 121 java_name.obj(), | |
| 122 java_short_name.obj(), | |
| 123 java_bitmap.obj(), | |
| 124 java_icons.obj(), | |
| 125 info.display, | |
| 126 info.orientation, | |
| 127 info.theme_color, | |
| 128 info.background_color); | |
| 129 } | |
| 130 | |
| 131 SkBitmap ManifestUpgradeDetector::FinalizeLauncherIcon( | |
| 132 const SkBitmap& bitmap, | |
| 133 const GURL& url, | |
| 134 bool* is_generated) { | |
| 135 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 136 return SkBitmap(); | |
| 137 } | |
| 138 | |
| 139 void ManifestUpgradeDetector::Destroy(JNIEnv* env, | |
| 140 const JavaParamRef<jobject>& obj) { | |
| 141 delete this; | |
| 142 } | |
| 143 | |
| 144 void ManifestUpgradeDetector::Start(JNIEnv* env, | |
| 145 const JavaParamRef<jobject>& obj) { | |
| 146 start_fetching_ = true; | |
|
pkotwicz
2016/07/12 01:23:49
Nit: Rename to |started_|
Xi Han
2016/07/12 20:53:41
Done.
| |
| 147 } | |
| OLD | NEW |