[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / ui / android / toolbar / toolbar_model_android.cc
blob3990d36e90524cce104ef0956270879f0f41bc58
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/android/toolbar/toolbar_model_android.h"
7 #include "base/android/jni_string.h"
8 #include "base/metrics/field_trial.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/search.h"
11 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
12 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
13 #include "content/public/browser/cert_store.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/ssl_status.h"
17 #include "jni/ToolbarModel_jni.h"
18 #include "net/cert/x509_certificate.h"
20 using base::android::ScopedJavaLocalRef;
22 ToolbarModelAndroid::ToolbarModelAndroid(JNIEnv* env, jobject jdelegate)
23 : toolbar_model_(new ToolbarModelImpl(this)),
24 weak_java_delegate_(env, jdelegate) {
27 ToolbarModelAndroid::~ToolbarModelAndroid() {
30 void ToolbarModelAndroid::Destroy(JNIEnv* env, jobject obj) {
31 delete this;
34 ScopedJavaLocalRef<jstring> ToolbarModelAndroid::GetText(JNIEnv* env,
35 jobject obj) {
36 return base::android::ConvertUTF16ToJavaString(env,
37 toolbar_model_->GetText());
40 ScopedJavaLocalRef<jstring> ToolbarModelAndroid::GetQueryExtractionParam(
41 JNIEnv* env,
42 jobject obj) {
43 content::WebContents* web_contents = GetActiveWebContents();
44 if (!web_contents)
45 return ScopedJavaLocalRef<jstring>();
46 Profile* profile =
47 Profile::FromBrowserContext(web_contents->GetBrowserContext());
48 UIThreadSearchTermsData search_terms_data(profile);
49 return base::android::ConvertUTF8ToJavaString(
50 env, chrome::InstantExtendedEnabledParam(true));
53 ScopedJavaLocalRef<jstring> ToolbarModelAndroid::GetCorpusChipText(
54 JNIEnv* env,
55 jobject obj) {
56 return base::android::ConvertUTF16ToJavaString(
57 env,
58 toolbar_model_->GetCorpusNameForMobile());
61 content::WebContents* ToolbarModelAndroid::GetActiveWebContents() const {
62 JNIEnv* env = base::android::AttachCurrentThread();
63 ScopedJavaLocalRef<jobject> jdelegate = weak_java_delegate_.get(env);
64 if (!jdelegate.obj())
65 return NULL;
66 ScopedJavaLocalRef<jobject> jweb_contents =
67 Java_ToolbarModelDelegate_getActiveWebContents(env, jdelegate.obj());
68 return content::WebContents::FromJavaWebContents(jweb_contents.obj());
71 // static
72 bool ToolbarModelAndroid::RegisterToolbarModelAndroid(JNIEnv* env) {
73 return RegisterNativesImpl(env);
76 // static
77 jlong Init(JNIEnv* env, jobject obj, jobject delegate) {
78 ToolbarModelAndroid* toolbar_model = new ToolbarModelAndroid(env, delegate);
79 return reinterpret_cast<intptr_t>(toolbar_model);
82 // static
83 jint GetSecurityLevelForWebContents(JNIEnv* env,
84 jclass jcaller,
85 jobject jweb_contents) {
86 content::WebContents* web_contents =
87 content::WebContents::FromJavaWebContents(jweb_contents);
88 DCHECK(web_contents);
89 return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents);
92 // Temporary method to allow us to surface a SHA-1 deprecation string on Android
93 // in M42. This duplicates a subset of the logic from
94 // ToolbarModelImpl::GetSecurityLevelForWebContents() and
95 // WebsiteSettings::Init(), which should really be refactored.
96 // This is at the wrong layer, and needs to be refactored (along with desktop):
97 // https://crbug.com/471390
99 // static
100 jboolean IsDeprecatedSHA1Present(JNIEnv* env,
101 jclass jcaller,
102 jobject jweb_contents) {
103 content::WebContents* web_contents =
104 content::WebContents::FromJavaWebContents(jweb_contents);
105 DCHECK(web_contents);
107 content::NavigationEntry* entry =
108 web_contents->GetController().GetVisibleEntry();
109 if (!entry)
110 return false;
112 const content::SSLStatus& ssl = entry->GetSSL();
113 if (ssl.security_style == content::SECURITY_STYLE_AUTHENTICATED) {
114 scoped_refptr<net::X509Certificate> cert;
115 // NOTE: This constant needs to be kept in sync with
116 // ToolbarModelImpl::GetSecurityLevelForWebContents().
117 static const int64_t kJanuary2016 = INT64_C(13096080000000000);
118 if (content::CertStore::GetInstance()->RetrieveCert(ssl.cert_id, &cert) &&
119 (ssl.cert_status & net::CERT_STATUS_SHA1_SIGNATURE_PRESENT) &&
120 cert->valid_expiry() > base::Time::FromInternalValue(kJanuary2016) &&
121 // NOTE: This use of SHA1IdentityUIWarning needs to be kept in sync
122 // with WebsiteSettings::Init().
123 base::FieldTrialList::FindFullName("SHA1IdentityUIWarning") ==
124 "Enabled") {
125 return true;
128 return false;