OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/fullscreen/fullscreen_infobar_delegate.h" | 5 #include "chrome/browser/ui/android/infobars/document_mode_opt_out_infobar_deleg
ate.h" |
6 | 6 |
7 #include "base/android/jni_string.h" | 7 #include "base/metrics/sparse_histogram.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/prefs/pref_service.h" | |
10 #include "chrome/browser/android/android_theme_resources.h" | 8 #include "chrome/browser/android/android_theme_resources.h" |
11 #include "chrome/browser/android/tab_android.h" | 9 #include "chrome/browser/android/tab_android.h" |
12 #include "chrome/browser/infobars/infobar_service.h" | 10 #include "chrome/browser/infobars/infobar_service.h" |
13 #include "chrome/browser/profiles/profile_manager.h" | |
14 #include "chrome/common/pref_names.h" | |
15 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
16 #include "components/infobars/core/infobar.h" | 12 #include "components/infobars/core/infobar.h" |
17 #include "components/url_formatter/elide_url.h" | 13 #include "jni/DocumentModeOptOutInfoBarDelegate_jni.h" |
18 #include "grit/components_strings.h" | |
19 #include "jni/FullscreenInfoBarDelegate_jni.h" | |
20 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
21 #include "url/gurl.h" | 15 |
| 16 namespace { |
| 17 |
| 18 // Keep in sync with the values defined in histograms.xml. |
| 19 enum InfoBarEvent { |
| 20 SHOWN = 0, |
| 21 ACCEPTED, |
| 22 DENIED, |
| 23 DISMISSED |
| 24 }; |
| 25 |
| 26 } // namespace |
22 | 27 |
23 // static | 28 // static |
24 jlong LaunchFullscreenInfoBar(JNIEnv* env, | 29 jlong ShowDocumentModeOptOutInfoBar(JNIEnv* env, |
25 const JavaParamRef<jobject>& obj, | 30 const JavaParamRef<jobject>& jcaller, |
26 const JavaParamRef<jobject>& tab) { | 31 const JavaParamRef<jobject>& tab) { |
27 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, tab); | 32 UMA_HISTOGRAM_SPARSE_SLOWLY("DocumentModeOptOutInfoBar", InfoBarEvent::SHOWN); |
28 GURL origin = tab_android->GetURL().GetOrigin(); | 33 |
29 FullscreenInfoBarDelegate* delegate = new FullscreenInfoBarDelegate( | 34 DocumentModeOptOutInfoBarDelegate* delegate = |
30 env, obj, origin); | 35 new DocumentModeOptOutInfoBarDelegate(env, jcaller); |
31 InfoBarService* infobar_service = | 36 InfoBarService* infobar_service = InfoBarService::FromWebContents( |
32 InfoBarService::FromWebContents(tab_android->web_contents()); | 37 TabAndroid::GetNativeTab(env, tab)->web_contents()); |
33 infobar_service->AddInfoBar( | 38 infobar_service->AddInfoBar( |
34 infobar_service->CreateConfirmInfoBar(make_scoped_ptr(delegate))); | 39 infobar_service->CreateConfirmInfoBar(make_scoped_ptr(delegate))); |
35 return reinterpret_cast<intptr_t>(delegate); | 40 return reinterpret_cast<intptr_t>(delegate); |
36 } | 41 } |
37 | 42 |
38 bool FullscreenInfoBarDelegate::RegisterFullscreenInfoBarDelegate(JNIEnv* env) { | 43 // static |
| 44 bool DocumentModeOptOutInfoBarDelegate:: |
| 45 RegisterDocumentModeOptOutInfoBarDelegate(JNIEnv* env) { |
39 return RegisterNativesImpl(env); | 46 return RegisterNativesImpl(env); |
40 } | 47 } |
41 | 48 |
42 FullscreenInfoBarDelegate::FullscreenInfoBarDelegate( | 49 DocumentModeOptOutInfoBarDelegate::DocumentModeOptOutInfoBarDelegate( |
43 JNIEnv* env, jobject obj, GURL origin) | 50 JNIEnv* env, jobject obj) |
44 : origin_(origin) { | 51 : ConfirmInfoBarDelegate() { |
45 j_delegate_.Reset(env, obj); | 52 j_delegate_.Reset(env, obj); |
46 } | 53 } |
47 | 54 |
48 FullscreenInfoBarDelegate::~FullscreenInfoBarDelegate() { | 55 DocumentModeOptOutInfoBarDelegate::~DocumentModeOptOutInfoBarDelegate() { |
49 if (!j_delegate_.is_null()) { | |
50 Java_FullscreenInfoBarDelegate_onInfoBarDismissed( | |
51 base::android::AttachCurrentThread(), j_delegate_.obj()); | |
52 } | |
53 } | 56 } |
54 | 57 |
55 void FullscreenInfoBarDelegate::CloseFullscreenInfoBar( | 58 int DocumentModeOptOutInfoBarDelegate::GetIconId() const { |
56 JNIEnv* env, jobject obj) { | 59 return IDR_ANDROID_INFOBAR_DOCUMENT_MODE_OPT_OUT; |
57 j_delegate_.Reset(); | |
58 if (infobar() && infobar()->owner()) | |
59 infobar()->owner()->RemoveInfoBar(infobar()); | |
60 } | 60 } |
61 | 61 |
62 int FullscreenInfoBarDelegate::GetIconId() const { | 62 bool DocumentModeOptOutInfoBarDelegate::ShouldExpire( |
63 return IDR_ANDROID_INFOBAR_FULLSCREEN; | 63 const NavigationDetails& details) const { |
| 64 return false; |
64 } | 65 } |
65 | 66 |
66 base::string16 FullscreenInfoBarDelegate::GetMessageText() const { | 67 void DocumentModeOptOutInfoBarDelegate::InfoBarDismissed() { |
67 Profile* profile = | 68 UMA_HISTOGRAM_SPARSE_SLOWLY("DocumentModeOptOutInfoBar", |
68 ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); | 69 InfoBarEvent::DISMISSED); |
69 std::string language = | |
70 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
71 return l10n_util::GetStringFUTF16( | |
72 IDS_FULLSCREEN_INFOBAR_TEXT, | |
73 url_formatter::FormatUrlForSecurityDisplay(origin_, language)); | |
74 } | 70 } |
75 | 71 |
76 base::string16 FullscreenInfoBarDelegate::GetButtonLabel( | 72 base::string16 DocumentModeOptOutInfoBarDelegate::GetMessageText() const { |
77 InfoBarButton button) const { | 73 return l10n_util::GetStringUTF16(IDS_DOCUMENT_MODE_OPT_OUT_INFOBAR_TEXT); |
78 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | |
79 IDS_FULLSCREEN_INFOBAR_ALLOW_BUTTON : | |
80 IDS_FULLSCREEN_INFOBAR_EXIT_FULLSCREEN_BUTTON); | |
81 } | 74 } |
82 | 75 |
83 bool FullscreenInfoBarDelegate::Accept() { | 76 int DocumentModeOptOutInfoBarDelegate::GetButtons() const { |
84 JNIEnv* env = base::android::AttachCurrentThread(); | 77 return BUTTON_OK | BUTTON_CANCEL; |
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; | |
90 } | 78 } |
91 | 79 |
92 bool FullscreenInfoBarDelegate::Cancel() { | 80 base::string16 DocumentModeOptOutInfoBarDelegate::GetButtonLabel( |
93 Java_FullscreenInfoBarDelegate_onFullscreenCancelled( | 81 InfoBarButton button) const { |
| 82 DCHECK(button == BUTTON_OK || button == BUTTON_CANCEL); |
| 83 return l10n_util::GetStringUTF16( |
| 84 button == BUTTON_OK ? IDS_SHOW : IDS_NO_THANKS); |
| 85 } |
| 86 |
| 87 bool DocumentModeOptOutInfoBarDelegate::Accept() { |
| 88 UMA_HISTOGRAM_SPARSE_SLOWLY("DocumentModeOptOutInfoBar", |
| 89 InfoBarEvent::ACCEPTED); |
| 90 Java_DocumentModeOptOutInfoBarDelegate_accept( |
| 91 base::android::AttachCurrentThread(), j_delegate_.obj()); |
| 92 |
| 93 // Chrome is restarting in tabbed mode so we don't need to close. |
| 94 // The visual difference is that, if we return true here, InfoBar starts to |
| 95 // slide down and Chrome is restarted before the sliding is finished. |
| 96 return false; |
| 97 } |
| 98 |
| 99 bool DocumentModeOptOutInfoBarDelegate::Cancel() { |
| 100 UMA_HISTOGRAM_SPARSE_SLOWLY("DocumentModeOptOutInfoBar", |
| 101 InfoBarEvent::DENIED); |
| 102 Java_DocumentModeOptOutInfoBarDelegate_cancel( |
94 base::android::AttachCurrentThread(), j_delegate_.obj()); | 103 base::android::AttachCurrentThread(), j_delegate_.obj()); |
95 return true; | 104 return true; |
96 } | 105 } |
OLD | NEW |