Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / fullscreen / fullscreen_infobar_delegate.cc
bloba36cb6c34a89f04bc57f9de849a5b067cc4b2a84
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/fullscreen/fullscreen_infobar_delegate.h"
7 #include "base/android/jni_string.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/android/tab_android.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/infobars/core/infobar.h"
16 #include "components/url_formatter/url_formatter.h"
17 #include "grit/components_strings.h"
18 #include "grit/theme_resources.h"
19 #include "jni/FullscreenInfoBarDelegate_jni.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "url/gurl.h"
23 // static
24 jlong LaunchFullscreenInfoBar(JNIEnv* env,
25 const JavaParamRef<jobject>& obj,
26 const JavaParamRef<jobject>& tab) {
27 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab);
28 GURL origin = tab_android->GetURL().GetOrigin();
29 FullscreenInfoBarDelegate* delegate = new FullscreenInfoBarDelegate(
30 env, obj, origin);
31 InfoBarService* infobar_service =
32 InfoBarService::FromWebContents(tab_android->web_contents());
33 infobar_service->AddInfoBar(
34 infobar_service->CreateConfirmInfoBar(make_scoped_ptr(delegate)));
35 return reinterpret_cast<intptr_t>(delegate);
38 bool FullscreenInfoBarDelegate::RegisterFullscreenInfoBarDelegate(JNIEnv* env) {
39 return RegisterNativesImpl(env);
42 FullscreenInfoBarDelegate::FullscreenInfoBarDelegate(
43 JNIEnv* env, jobject obj, GURL origin)
44 : origin_(origin) {
45 j_delegate_.Reset(env, obj);
48 FullscreenInfoBarDelegate::~FullscreenInfoBarDelegate() {
49 if (!j_delegate_.is_null()) {
50 Java_FullscreenInfoBarDelegate_onInfoBarDismissed(
51 base::android::AttachCurrentThread(), j_delegate_.obj());
55 void FullscreenInfoBarDelegate::CloseFullscreenInfoBar(
56 JNIEnv* env, jobject obj) {
57 j_delegate_.Reset();
58 if (infobar() && infobar()->owner())
59 infobar()->owner()->RemoveInfoBar(infobar());
62 int FullscreenInfoBarDelegate::GetIconId() const {
63 return IDR_INFOBAR_FULLSCREEN;
66 base::string16 FullscreenInfoBarDelegate::GetMessageText() const {
67 Profile* profile =
68 ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
69 std::string language =
70 profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
71 return l10n_util::GetStringFUTF16(
72 IDS_FULLSCREEN_INFOBAR_TEXT,
73 url_formatter::FormatUrl(GURL(origin_), language));
76 base::string16 FullscreenInfoBarDelegate::GetButtonLabel(
77 InfoBarButton button) const {
78 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
79 IDS_FULLSCREEN_INFOBAR_ALLOW_BUTTON :
80 IDS_FULLSCREEN_INFOBAR_EXIT_FULLSCREEN_BUTTON);
83 bool FullscreenInfoBarDelegate::Accept() {
84 JNIEnv* env = base::android::AttachCurrentThread();
85 ScopedJavaLocalRef<jstring> j_origin =
86 base::android::ConvertUTF8ToJavaString(env, origin_.spec());
87 Java_FullscreenInfoBarDelegate_onFullscreenAllowed(
88 env, j_delegate_.obj(), j_origin.obj());
89 return true;
92 bool FullscreenInfoBarDelegate::Cancel() {
93 Java_FullscreenInfoBarDelegate_onFullscreenCancelled(
94 base::android::AttachCurrentThread(), j_delegate_.obj());
95 return true;