Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / android / banners / app_banner_manager_android.cc
blob357d36208bca2e9f7053a71d31437dbd0d6fa346
1 // Copyright 2015 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.
5 #include "chrome/browser/android/banners/app_banner_manager_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "chrome/browser/android/banners/app_banner_data_fetcher_android.h"
12 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/render_messages.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/frame_navigate_params.h"
17 #include "jni/AppBannerManager_jni.h"
19 using base::android::ConvertJavaStringToUTF8;
20 using base::android::ConvertJavaStringToUTF16;
21 using base::android::ConvertUTF8ToJavaString;
22 using base::android::ConvertUTF16ToJavaString;
24 namespace {
25 const char kPlayPlatform[] = "play";
26 } // anonymous namespace
28 namespace banners {
30 AppBannerManagerAndroid::AppBannerManagerAndroid(JNIEnv* env,
31 jobject obj,
32 int icon_size)
33 : AppBannerManager(icon_size),
34 weak_java_banner_view_manager_(env, obj) {
37 AppBannerManagerAndroid::~AppBannerManagerAndroid() {
40 void AppBannerManagerAndroid::Destroy(JNIEnv* env, jobject obj) {
41 delete this;
44 void AppBannerManagerAndroid::ReplaceWebContents(JNIEnv* env,
45 jobject obj,
46 jobject jweb_contents) {
47 content::WebContents* web_contents =
48 content::WebContents::FromJavaWebContents(jweb_contents);
49 AppBannerManager::ReplaceWebContents(web_contents);
52 bool AppBannerManagerAndroid::HandleNonWebApp(const std::string& platform,
53 const GURL& url,
54 const std::string& id) {
55 if (!CheckPlatformAndId(platform, id))
56 return false;
58 banners::TrackDisplayEvent(DISPLAY_EVENT_NATIVE_APP_BANNER_REQUESTED);
60 // Send the info to the Java side to get info about the app.
61 JNIEnv* env = base::android::AttachCurrentThread();
62 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env);
63 if (jobj.is_null())
64 return false;
66 ScopedJavaLocalRef<jstring> jurl(
67 ConvertUTF8ToJavaString(env, data_fetcher()->validated_url().spec()));
68 ScopedJavaLocalRef<jstring> jpackage(
69 ConvertUTF8ToJavaString(env, id));
70 Java_AppBannerManager_fetchAppDetails(
71 env, jobj.obj(), jurl.obj(), jpackage.obj(), ideal_icon_size());
72 return true;
75 bool AppBannerManagerAndroid::CheckPlatformAndId(const std::string& platform,
76 const std::string& id) {
77 if (platform != kPlayPlatform) {
78 banners::OutputDeveloperDebugMessage(
79 web_contents(), platform + banners::kIgnoredNotSupportedOnAndroid);
80 return false;
82 if (id.empty()) {
83 banners::OutputDeveloperDebugMessage(web_contents(), banners::kIgnoredNoId);
84 return false;
86 return true;
89 bool AppBannerManagerAndroid::CheckFetcherMatchesContents() {
90 if (!web_contents()) {
91 return false;
93 if (!data_fetcher() ||
94 data_fetcher()->validated_url() != web_contents()->GetURL()) {
95 banners::OutputDeveloperNotShownMessage(
96 web_contents(), banners::kUserNavigatedBeforeBannerShown);
97 return false;
99 return true;
102 AppBannerDataFetcher* AppBannerManagerAndroid::CreateAppBannerDataFetcher(
103 base::WeakPtr<Delegate> weak_delegate,
104 const int ideal_icon_size) {
105 return new AppBannerDataFetcherAndroid(web_contents(), weak_delegate,
106 ideal_icon_size);
109 bool AppBannerManagerAndroid::OnAppDetailsRetrieved(JNIEnv* env,
110 jobject obj,
111 jobject japp_data,
112 jstring japp_title,
113 jstring japp_package,
114 jstring jicon_url) {
115 if (!CheckFetcherMatchesContents())
116 return false;
118 base::android::ScopedJavaLocalRef<jobject> native_app_data;
119 native_app_data.Reset(env, japp_data);
120 GURL image_url = GURL(ConvertJavaStringToUTF8(env, jicon_url));
122 AppBannerDataFetcherAndroid* android_fetcher =
123 static_cast<AppBannerDataFetcherAndroid*>(data_fetcher().get());
124 return android_fetcher->ContinueFetching(
125 ConvertJavaStringToUTF16(env, japp_title),
126 ConvertJavaStringToUTF8(env, japp_package),
127 native_app_data, image_url);
130 bool AppBannerManagerAndroid::IsFetcherActive(JNIEnv* env, jobject obj) {
131 return AppBannerManager::IsFetcherActive();
134 // static
135 bool AppBannerManagerAndroid::Register(JNIEnv* env) {
136 return RegisterNativesImpl(env);
139 jlong Init(JNIEnv* env, jobject obj, jint icon_size) {
140 AppBannerManagerAndroid* manager =
141 new AppBannerManagerAndroid(env, obj, icon_size);
142 return reinterpret_cast<intptr_t>(manager);
145 void SetTimeDeltaForTesting(JNIEnv* env, jclass clazz, jint days) {
146 AppBannerDataFetcher::SetTimeDeltaForTesting(days);
149 void DisableSecureSchemeCheckForTesting(JNIEnv* env, jclass clazz) {
150 AppBannerManager::DisableSecureSchemeCheckForTesting();
153 void ForceEngagementWeightsForTesting(JNIEnv* env,
154 jclass clazz,
155 jdouble direct_engagement,
156 jdouble indirect_engagement) {
157 AppBannerManager::ForceEngagementWeightsForTesting(direct_engagement,
158 indirect_engagement);
161 jboolean IsEnabled(JNIEnv* env, jclass clazz) {
162 return base::FieldTrialList::FindFullName("AppBanners") == "Enabled";
165 } // namespace banners