Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/banners/app_banner_manager.h" | 5 #include "chrome/browser/android/banners/app_banner_manager.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "chrome/browser/android/banners/app_banner_settings_helper.h" | 10 #include "chrome/browser/android/banners/app_banner_settings_helper.h" |
| 11 #include "chrome/browser/bitmap_fetcher.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 13 #include "content/public/browser/android/content_view_core.h" | 14 #include "content/public/browser/android/content_view_core.h" |
| 14 #include "content/public/browser/navigation_details.h" | 15 #include "content/public/browser/navigation_details.h" |
| 15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/frame_navigate_params.h" | 17 #include "content/public/common/frame_navigate_params.h" |
| 17 #include "jni/AppBannerManager_jni.h" | 18 #include "jni/AppBannerManager_jni.h" |
| 18 #include "ui/gfx/android/java_bitmap.h" | 19 #include "ui/gfx/android/java_bitmap.h" |
| 19 | 20 |
| 20 using base::android::ConvertJavaStringToUTF8; | 21 using base::android::ConvertJavaStringToUTF8; |
| 21 using base::android::ConvertUTF8ToJavaString; | 22 using base::android::ConvertUTF8ToJavaString; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 const char kBannerTag[] = "google-play-id"; | 25 const char kBannerTag[] = "google-play-id"; |
| 25 } | 26 } |
| 26 | 27 |
| 27 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) | 28 AppBannerManager::AppBannerManager(JNIEnv* env, jobject obj) |
| 28 : MetaTagObserver(kBannerTag), | 29 : MetaTagObserver(kBannerTag), |
| 29 weak_java_banner_view_manager_(env, obj) {} | 30 weak_java_banner_view_manager_(env, obj) {} |
| 30 | 31 |
| 31 AppBannerManager::~AppBannerManager() { | 32 AppBannerManager::~AppBannerManager() { |
| 32 } | 33 } |
| 33 | 34 |
| 34 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { | 35 void AppBannerManager::Destroy(JNIEnv* env, jobject obj) { |
| 35 delete this; | 36 delete this; |
| 36 } | 37 } |
| 37 | 38 |
| 39 void AppBannerManager::BlockBanner(JNIEnv* env, | |
| 40 jobject obj, | |
| 41 jstring jurl, | |
| 42 jstring jpackage) { | |
| 43 if (!web_contents()) | |
| 44 return; | |
| 45 | |
| 46 GURL url(ConvertJavaStringToUTF8(env, jurl)); | |
| 47 std::string package_name = ConvertJavaStringToUTF8(env, jpackage); | |
| 48 AppBannerSettingsHelper::Block(web_contents(), url, package_name); | |
| 49 } | |
| 50 | |
| 38 void AppBannerManager::ReplaceWebContents(JNIEnv* env, | 51 void AppBannerManager::ReplaceWebContents(JNIEnv* env, |
| 39 jobject obj, | 52 jobject obj, |
| 40 jobject jweb_contents) { | 53 jobject jweb_contents) { |
| 41 content::WebContents* web_contents = | 54 content::WebContents* web_contents = |
| 42 content::WebContents::FromJavaWebContents(jweb_contents); | 55 content::WebContents::FromJavaWebContents(jweb_contents); |
| 43 Observe(web_contents); | 56 Observe(web_contents); |
| 44 } | 57 } |
| 45 | 58 |
| 46 void AppBannerManager::DidNavigateMainFrame( | 59 void AppBannerManager::DidNavigateMainFrame( |
| 47 const content::LoadCommittedDetails& details, | 60 const content::LoadCommittedDetails& details, |
| 48 const content::FrameNavigateParams& params) { | 61 const content::FrameNavigateParams& params) { |
| 49 // TODO(dfalcantara): Get rid of the current banner. | 62 // Get rid of the current banner. |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 64 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); | |
|
Yaron
2014/02/19 22:41:36
I think this can legitimately be null as the messa
gone
2014/02/20 22:07:24
Entirely possible... added null checks everywhere.
| |
| 65 Java_AppBannerManager_dismissCurrentBanner(env, jobj.obj()); | |
| 66 } | |
| 67 | |
| 68 void AppBannerManager::OnFetchComplete(const GURL url, const SkBitmap* bitmap) { | |
| 69 if (bitmap) { | |
| 70 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 71 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); | |
| 72 ScopedJavaLocalRef<jstring> jimage_url( | |
| 73 ConvertUTF8ToJavaString(env, url.spec())); | |
| 74 ScopedJavaLocalRef<jobject> jimage = gfx::ConvertToJavaBitmap(bitmap); | |
| 75 Java_AppBannerManager_createBanner(env, | |
| 76 jobj.obj(), | |
| 77 jimage_url.obj(), | |
| 78 jimage.obj()); | |
| 79 } else { | |
| 80 VLOG(1) << "Failed to retrieve image: " << url; | |
|
Yaron
2014/02/19 22:41:36
DVLOG
gone
2014/02/20 22:07:24
Done.
| |
| 81 } | |
| 82 | |
| 83 fetcher_.reset(); | |
| 50 } | 84 } |
| 51 | 85 |
| 52 void AppBannerManager::HandleMetaTagContent(const std::string& tag_content, | 86 void AppBannerManager::HandleMetaTagContent(const std::string& tag_content, |
| 53 const GURL& expected_url) { | 87 const GURL& expected_url) { |
| 54 DCHECK(web_contents()); | 88 DCHECK(web_contents()); |
| 55 | 89 |
| 56 if (!AppBannerSettingsHelper::IsAllowed(web_contents(), | 90 if (!AppBannerSettingsHelper::IsAllowed(web_contents(), |
| 57 expected_url, | 91 expected_url, |
| 58 tag_content)) { | 92 tag_content)) { |
| 59 return; | 93 return; |
| 60 } | 94 } |
| 61 | 95 |
| 62 // TODO(dfalcantara): Send the info to the Java side to begin building the | 96 // Send the info to the Java side to get info about the app. |
| 63 // app banner. | 97 JNIEnv* env = base::android::AttachCurrentThread(); |
| 98 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); | |
|
Yaron
2014/02/19 22:41:36
null-check
gone
2014/02/20 22:07:24
Done.
| |
| 99 ScopedJavaLocalRef<jstring> jurl( | |
| 100 ConvertUTF8ToJavaString(env, expected_url.spec())); | |
| 101 ScopedJavaLocalRef<jstring> jpackage( | |
| 102 ConvertUTF8ToJavaString(env, tag_content)); | |
| 103 Java_AppBannerManager_prepareBanner(env, | |
| 104 jobj.obj(), | |
| 105 jurl.obj(), | |
| 106 jpackage.obj()); | |
| 107 } | |
| 108 | |
| 109 void AppBannerManager::FetchIcon(JNIEnv* env, | |
| 110 jobject obj, | |
| 111 jstring jpage_url, | |
| 112 jstring jimage_url) { | |
| 113 std::string page_url = ConvertJavaStringToUTF8(env, jpage_url); | |
| 114 std::string image_url = ConvertJavaStringToUTF8(env, jimage_url); | |
| 115 if (!web_contents() || web_contents()->GetURL().spec() != page_url || | |
| 116 image_url.empty()) { | |
| 117 return; | |
|
Yaron
2014/02/19 22:41:36
Is this early return sufficient? Shouldn't you cal
gone
2014/02/20 22:07:24
Cleaned up the conditions so that this side doesn'
| |
| 118 } | |
| 119 | |
| 120 // Begin asynchronously fetching the app icon. | |
| 121 Profile* profile = | |
| 122 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 123 fetcher_.reset(new chrome::BitmapFetcher(GURL(image_url), this)); | |
|
Yaron
2014/02/19 22:41:36
Do you need to check whether the image_url is vali
gone
2014/02/20 22:07:24
No objection here, but any idea how we could go ab
| |
| 124 fetcher_.get()->Start(profile); | |
| 64 } | 125 } |
| 65 | 126 |
| 66 jlong Init(JNIEnv* env, jobject obj) { | 127 jlong Init(JNIEnv* env, jobject obj) { |
| 67 AppBannerManager* manager = new AppBannerManager(env, obj); | 128 AppBannerManager* manager = new AppBannerManager(env, obj); |
| 68 return reinterpret_cast<intptr_t>(manager); | 129 return reinterpret_cast<intptr_t>(manager); |
| 69 } | 130 } |
| 70 | 131 |
| 71 jboolean IsEnabled(JNIEnv* env, jclass clazz) { | 132 jboolean IsEnabled(JNIEnv* env, jclass clazz) { |
| 72 return false; | 133 return !CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 | 134 switches::kDisableAppBanners); |
| 74 // TODO(dfalcantara): Enable this when more of the pipeline is checked in. | |
| 75 // return !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 76 // switches::kDisableAppBanners); | |
| 77 } | 135 } |
| 78 | 136 |
| 79 // Register native methods | 137 // Register native methods |
| 80 bool RegisterAppBannerManager(JNIEnv* env) { | 138 bool RegisterAppBannerManager(JNIEnv* env) { |
| 81 return RegisterNativesImpl(env); | 139 return RegisterNativesImpl(env); |
| 82 } | 140 } |
| OLD | NEW |