| 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); |
| 65 if (jobj.is_null()) |
| 66 return; |
| 67 Java_AppBannerManager_dismissCurrentBanner(env, jobj.obj()); |
| 68 } |
| 69 |
| 70 void AppBannerManager::OnFetchComplete(const GURL url, const SkBitmap* bitmap) { |
| 71 if (bitmap) { |
| 72 JNIEnv* env = base::android::AttachCurrentThread(); |
| 73 |
| 74 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); |
| 75 if (jobj.is_null()) |
| 76 return; |
| 77 |
| 78 ScopedJavaLocalRef<jstring> jimage_url( |
| 79 ConvertUTF8ToJavaString(env, url.spec())); |
| 80 ScopedJavaLocalRef<jobject> jimage = gfx::ConvertToJavaBitmap(bitmap); |
| 81 Java_AppBannerManager_createBanner(env, |
| 82 jobj.obj(), |
| 83 jimage_url.obj(), |
| 84 jimage.obj()); |
| 85 } else { |
| 86 DVLOG(1) << "Failed to retrieve image: " << url; |
| 87 } |
| 88 |
| 89 fetcher_.reset(); |
| 50 } | 90 } |
| 51 | 91 |
| 52 void AppBannerManager::HandleMetaTagContent(const std::string& tag_content, | 92 void AppBannerManager::HandleMetaTagContent(const std::string& tag_content, |
| 53 const GURL& expected_url) { | 93 const GURL& expected_url) { |
| 54 DCHECK(web_contents()); | 94 DCHECK(web_contents()); |
| 55 | 95 |
| 56 if (!AppBannerSettingsHelper::IsAllowed(web_contents(), | 96 if (!AppBannerSettingsHelper::IsAllowed(web_contents(), |
| 57 expected_url, | 97 expected_url, |
| 58 tag_content)) { | 98 tag_content)) { |
| 59 return; | 99 return; |
| 60 } | 100 } |
| 61 | 101 |
| 62 // TODO(dfalcantara): Send the info to the Java side to begin building the | 102 // Send the info to the Java side to get info about the app. |
| 63 // app banner. | 103 JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); |
| 105 if (jobj.is_null()) |
| 106 return; |
| 107 |
| 108 ScopedJavaLocalRef<jstring> jurl( |
| 109 ConvertUTF8ToJavaString(env, expected_url.spec())); |
| 110 ScopedJavaLocalRef<jstring> jpackage( |
| 111 ConvertUTF8ToJavaString(env, tag_content)); |
| 112 Java_AppBannerManager_prepareBanner(env, |
| 113 jobj.obj(), |
| 114 jurl.obj(), |
| 115 jpackage.obj()); |
| 116 } |
| 117 |
| 118 bool AppBannerManager::FetchIcon(JNIEnv* env, |
| 119 jobject obj, |
| 120 jstring jimage_url) { |
| 121 std::string image_url = ConvertJavaStringToUTF8(env, jimage_url); |
| 122 if (!web_contents()) |
| 123 return false; |
| 124 |
| 125 // Begin asynchronously fetching the app icon. |
| 126 Profile* profile = |
| 127 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 128 fetcher_.reset(new chrome::BitmapFetcher(GURL(image_url), this)); |
| 129 fetcher_.get()->Start(profile); |
| 130 return true; |
| 64 } | 131 } |
| 65 | 132 |
| 66 jlong Init(JNIEnv* env, jobject obj) { | 133 jlong Init(JNIEnv* env, jobject obj) { |
| 67 AppBannerManager* manager = new AppBannerManager(env, obj); | 134 AppBannerManager* manager = new AppBannerManager(env, obj); |
| 68 return reinterpret_cast<intptr_t>(manager); | 135 return reinterpret_cast<intptr_t>(manager); |
| 69 } | 136 } |
| 70 | 137 |
| 71 jboolean IsEnabled(JNIEnv* env, jclass clazz) { | 138 jboolean IsEnabled(JNIEnv* env, jclass clazz) { |
| 72 return false; | 139 return !CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 | 140 switches::kDisableAppBanners); |
| 74 // TODO(dfalcantara): Enable this when more of the pipeline is checked in. | |
| 75 // return !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 76 // switches::kDisableAppBanners); | |
| 77 } | 141 } |
| 78 | 142 |
| 79 // Register native methods | 143 // Register native methods |
| 80 bool RegisterAppBannerManager(JNIEnv* env) { | 144 bool RegisterAppBannerManager(JNIEnv* env) { |
| 81 return RegisterNativesImpl(env); | 145 return RegisterNativesImpl(env); |
| 82 } | 146 } |
| OLD | NEW |