| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "chrome/browser/dom_ui/content_settings_handler.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/host_content_settings_map.h" |
| 12 #include "chrome/browser/profile.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #include "grit/locale_settings.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 // The list of content settings types that we display in the tabbed options |
| 19 // under the Content Settings page. This should be filled in as more pages |
| 20 // are added. |
| 21 ContentSettingsType kContentSettingsTypes[] = { |
| 22 CONTENT_SETTINGS_TYPE_COOKIES, |
| 23 CONTENT_SETTINGS_TYPE_IMAGES, |
| 24 }; |
| 25 |
| 26 std::wstring ContentSettingsTypeToGroupName(ContentSettingsType type) { |
| 27 switch (type) { |
| 28 case CONTENT_SETTINGS_TYPE_COOKIES: |
| 29 return L"cookies"; |
| 30 case CONTENT_SETTINGS_TYPE_IMAGES: |
| 31 return L"images"; |
| 32 |
| 33 default: |
| 34 NOTREACHED(); |
| 35 return L""; |
| 36 } |
| 37 } |
| 38 |
| 39 ContentSettingsType ContentSettingsTypeFromGroupName(const std::string& name) { |
| 40 if (name == "cookies") |
| 41 return CONTENT_SETTINGS_TYPE_COOKIES; |
| 42 if (name == "images") |
| 43 return CONTENT_SETTINGS_TYPE_IMAGES; |
| 44 |
| 45 NOTREACHED(); |
| 46 return CONTENT_SETTINGS_TYPE_DEFAULT; |
| 47 } |
| 48 |
| 49 std::string ContentSettingToString(ContentSetting setting) { |
| 50 switch (setting) { |
| 51 case CONTENT_SETTING_ALLOW: |
| 52 return "allow"; |
| 53 case CONTENT_SETTING_ASK: |
| 54 return "ask"; |
| 55 case CONTENT_SETTING_BLOCK: |
| 56 return "block"; |
| 57 |
| 58 default: |
| 59 NOTREACHED(); |
| 60 return ""; |
| 61 } |
| 62 } |
| 63 |
| 64 ContentSetting ContentSettingFromString(const std::string& name) { |
| 65 if (name == "allow") |
| 66 return CONTENT_SETTING_ALLOW; |
| 67 if (name == "ask") |
| 68 return CONTENT_SETTING_ASK; |
| 69 if (name == "block") |
| 70 return CONTENT_SETTING_BLOCK; |
| 71 |
| 72 NOTREACHED(); |
| 73 return CONTENT_SETTING_DEFAULT; |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
| 78 ContentSettingsHandler::ContentSettingsHandler() { |
| 79 } |
| 80 |
| 81 ContentSettingsHandler::~ContentSettingsHandler() { |
| 82 } |
| 83 |
| 84 void ContentSettingsHandler::GetLocalizedValues( |
| 85 DictionaryValue* localized_strings) { |
| 86 DCHECK(localized_strings); |
| 87 |
| 88 localized_strings->SetString(L"content_exceptions", |
| 89 l10n_util::GetString(IDS_COOKIES_EXCEPTIONS_BUTTON)); |
| 90 localized_strings->SetString(L"contentSettingsPage", |
| 91 l10n_util::GetString(IDS_CONTENT_SETTINGS_TITLE)); |
| 92 |
| 93 // Cookies filter. |
| 94 localized_strings->SetString(L"cookies_tab_label", |
| 95 l10n_util::GetString(IDS_COOKIES_TAB_LABEL)); |
| 96 localized_strings->SetString(L"cookies_modify", |
| 97 l10n_util::GetString(IDS_MODIFY_COOKIE_STORING_LABEL)); |
| 98 localized_strings->SetString(L"cookies_allow", |
| 99 l10n_util::GetString(IDS_COOKIES_ALLOW_RADIO)); |
| 100 localized_strings->SetString(L"cookies_ask", |
| 101 l10n_util::GetString(IDS_COOKIES_ASK_EVERY_TIME_RADIO)); |
| 102 localized_strings->SetString(L"cookies_block", |
| 103 l10n_util::GetString(IDS_COOKIES_BLOCK_RADIO)); |
| 104 localized_strings->SetString(L"cookies_block_3rd_party", |
| 105 l10n_util::GetString(IDS_COOKIES_BLOCK_3RDPARTY_CHKBOX)); |
| 106 localized_strings->SetString(L"cookies_clear_on_exit", |
| 107 l10n_util::GetString(IDS_COOKIES_CLEAR_WHEN_CLOSE_CHKBOX)); |
| 108 localized_strings->SetString(L"cookies_show_cookies", |
| 109 l10n_util::GetString(IDS_COOKIES_SHOW_COOKIES_BUTTON)); |
| 110 localized_strings->SetString(L"flash_storage_settings", |
| 111 l10n_util::GetString(IDS_FLASH_STORAGE_SETTINGS)); |
| 112 localized_strings->SetString(L"flash_storage_url", |
| 113 l10n_util::GetString(IDS_FLASH_STORAGE_URL)); |
| 114 |
| 115 // Image filter. |
| 116 localized_strings->SetString(L"images_tab_label", |
| 117 l10n_util::GetString(IDS_IMAGES_TAB_LABEL)); |
| 118 localized_strings->SetString(L"images_setting", |
| 119 l10n_util::GetString(IDS_IMAGES_SETTING_LABEL)); |
| 120 localized_strings->SetString(L"images_allow", |
| 121 l10n_util::GetString(IDS_IMAGES_LOAD_RADIO)); |
| 122 localized_strings->SetString(L"images_block", |
| 123 l10n_util::GetString(IDS_IMAGES_NOLOAD_RADIO)); |
| 124 } |
| 125 |
| 126 void ContentSettingsHandler::RegisterMessages() { |
| 127 dom_ui_->RegisterMessageCallback("getContentFilterSettings", |
| 128 NewCallback(this, |
| 129 &ContentSettingsHandler::GetContentFilterSettings)); |
| 130 dom_ui_->RegisterMessageCallback("setContentFilter", |
| 131 NewCallback(this, |
| 132 &ContentSettingsHandler::SetContentFilter)); |
| 133 dom_ui_->RegisterMessageCallback("setAllowThirdPartyCookies", |
| 134 NewCallback(this, |
| 135 &ContentSettingsHandler::SetAllowThirdPartyCookies)); |
| 136 } |
| 137 |
| 138 void ContentSettingsHandler::GetContentFilterSettings(const Value* value) { |
| 139 // We send a list of the <input> IDs that should be checked. |
| 140 DictionaryValue dict_value; |
| 141 |
| 142 const HostContentSettingsMap* settings_map = |
| 143 dom_ui_->GetProfile()->GetHostContentSettingsMap(); |
| 144 for (size_t i = 0; i < arraysize(kContentSettingsTypes); ++i) { |
| 145 ContentSettingsType type = kContentSettingsTypes[i]; |
| 146 ContentSetting default_setting = settings_map-> |
| 147 GetDefaultContentSetting(type); |
| 148 |
| 149 dict_value.SetString(ContentSettingsTypeToGroupName(type), |
| 150 ContentSettingToString(default_setting)); |
| 151 } |
| 152 |
| 153 dom_ui_->CallJavascriptFunction( |
| 154 L"ContentSettings.setInitialContentFilterSettingsValue", dict_value); |
| 155 |
| 156 scoped_ptr<Value> bool_value(Value::CreateBooleanValue( |
| 157 settings_map->BlockThirdPartyCookies())); |
| 158 dom_ui_->CallJavascriptFunction( |
| 159 L"ContentSettings.setBlockThirdPartyCookies", *bool_value.get()); |
| 160 } |
| 161 |
| 162 void ContentSettingsHandler::SetContentFilter(const Value* value) { |
| 163 const ListValue* list_value = static_cast<const ListValue*>(value); |
| 164 DCHECK_EQ(2U, list_value->GetSize()); |
| 165 std::string group, setting; |
| 166 if (!(list_value->GetString(0, &group) && |
| 167 list_value->GetString(1, &setting))) { |
| 168 NOTREACHED(); |
| 169 return; |
| 170 } |
| 171 |
| 172 dom_ui_->GetProfile()->GetHostContentSettingsMap()->SetDefaultContentSetting( |
| 173 ContentSettingsTypeFromGroupName(group), |
| 174 ContentSettingFromString(setting)); |
| 175 } |
| 176 |
| 177 void ContentSettingsHandler::SetAllowThirdPartyCookies(const Value* value) { |
| 178 std::wstring allow = ExtractStringValue(value); |
| 179 |
| 180 dom_ui_->GetProfile()->GetHostContentSettingsMap()->SetBlockThirdPartyCookies( |
| 181 allow == L"true"); |
| 182 } |
| OLD | NEW |