Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / website_settings_popup_android.cc
blobe9cab3b4e706e27e06eba7a78c9d3c666b4ad18b
1 // Copyright (c) 2013 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/website_settings_popup_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/website_settings/website_settings.h"
13 #include "chrome/browser/ui/website_settings/website_settings_ui.h"
14 #include "components/content_settings/core/common/content_settings.h"
15 #include "components/content_settings/core/common/content_settings_types.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/cert_store.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/web_contents.h"
21 #include "grit/generated_resources.h"
22 #include "jni/WebsiteSettingsPopup_jni.h"
23 #include "ui/base/l10n/l10n_util.h"
25 using base::android::ConvertUTF16ToJavaString;
26 using base::android::ConvertUTF8ToJavaString;
28 // static
29 static jlong Init(JNIEnv* env,
30 const JavaParamRef<jclass>& clazz,
31 const JavaParamRef<jobject>& obj,
32 const JavaParamRef<jobject>& java_web_contents) {
33 content::WebContents* web_contents =
34 content::WebContents::FromJavaWebContents(java_web_contents);
36 return reinterpret_cast<intptr_t>(
37 new WebsiteSettingsPopupAndroid(env, obj, web_contents));
40 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
41 JNIEnv* env,
42 jobject java_website_settings_pop,
43 content::WebContents* web_contents) {
44 // Important to use GetVisibleEntry to match what's showing in the omnibox.
45 content::NavigationEntry* nav_entry =
46 web_contents->GetController().GetVisibleEntry();
47 if (nav_entry == NULL)
48 return;
50 url_ = nav_entry->GetURL();
52 popup_jobject_.Reset(env, java_website_settings_pop);
54 SecurityStateModel* security_model =
55 SecurityStateModel::FromWebContents(web_contents);
56 DCHECK(security_model);
58 presenter_.reset(new WebsiteSettings(
59 this, Profile::FromBrowserContext(web_contents->GetBrowserContext()),
60 TabSpecificContentSettings::FromWebContents(web_contents), web_contents,
61 nav_entry->GetURL(), security_model->GetSecurityInfo(),
62 content::CertStore::GetInstance()));
65 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
67 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
68 delete this;
71 void WebsiteSettingsPopupAndroid::OnPermissionSettingChanged(JNIEnv* env,
72 jobject obj,
73 jint type,
74 jint setting) {
75 ContentSettingsType content_setting_type =
76 static_cast<ContentSettingsType>(type);
77 ContentSetting content_setting = static_cast<ContentSetting>(setting);
78 presenter_->OnSitePermissionChanged(content_setting_type, content_setting);
81 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
82 const IdentityInfo& identity_info) {
83 JNIEnv* env = base::android::AttachCurrentThread();
84 Java_WebsiteSettingsPopup_showDialog(env, popup_jobject_.obj());
87 void WebsiteSettingsPopupAndroid::SetCookieInfo(
88 const CookieInfoList& cookie_info_list) {
89 NOTIMPLEMENTED();
92 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
93 const PermissionInfoList& permission_info_list) {
94 JNIEnv* env = base::android::AttachCurrentThread();
96 // On Android, we only want to display a subset of the available options in a
97 // particular order, but only if their value is different from the default.
98 std::vector<ContentSettingsType> permissions_to_display;
99 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_GEOLOCATION);
100 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
101 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
102 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
103 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_IMAGES);
104 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_JAVASCRIPT);
105 permissions_to_display.push_back(CONTENT_SETTINGS_TYPE_POPUPS);
107 std::map<ContentSettingsType, ContentSetting>
108 user_specified_settings_to_display;
110 for (const auto& permission : permission_info_list) {
111 if (std::find(permissions_to_display.begin(),
112 permissions_to_display.end(),
113 permission.type) != permissions_to_display.end() &&
114 permission.setting != CONTENT_SETTING_DEFAULT) {
115 user_specified_settings_to_display[permission.type] = permission.setting;
119 for (const auto& permission : permissions_to_display) {
120 if (ContainsKey(user_specified_settings_to_display, permission)) {
121 base::string16 setting_title =
122 WebsiteSettingsUI::PermissionTypeToUIString(permission);
124 Java_WebsiteSettingsPopup_addPermissionSection(
125 env,
126 popup_jobject_.obj(),
127 ConvertUTF16ToJavaString(env, setting_title).obj(),
128 static_cast<jint>(permission),
129 static_cast<jint>(user_specified_settings_to_display[permission]));
133 Java_WebsiteSettingsPopup_updatePermissionDisplay(env, popup_jobject_.obj());
136 void WebsiteSettingsPopupAndroid::SetSelectedTab(
137 WebsiteSettingsUI::TabId tab_id) {
138 // There's no tab UI on Android - only connection info is shown.
139 NOTIMPLEMENTED();
142 // static
143 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
144 JNIEnv* env) {
145 return RegisterNativesImpl(env);