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/ui/webui/collected_cookies_ui_delegate.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/cookies_tree_model.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 13 #include "chrome/browser/ui/webui/constrained_html_ui.h" |
| 14 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" |
| 15 #include "chrome/common/jstemplate_builder.h" |
| 16 #include "chrome/common/notification_service.h" |
| 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/browser/tab_contents/tab_contents.h" |
| 19 #include "grit/browser_resources.h" |
| 20 #include "grit/generated_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" |
| 23 |
| 24 namespace { |
| 25 |
| 26 // TODO(xiyuan): Localize this. |
| 27 const int kDialogWidth = 480; |
| 28 const int kDialogHeight = 420; |
| 29 |
| 30 CookieTreeOriginNode* GetOriginNode(CookiesTreeModel* model, |
| 31 const std::string& node_path) { |
| 32 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( |
| 33 model->GetRoot(), node_path); |
| 34 |
| 35 while (node) { |
| 36 if (node->GetDetailedInfo().node_type == |
| 37 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) { |
| 38 break; |
| 39 } |
| 40 |
| 41 node = node->GetParent(); |
| 42 } |
| 43 |
| 44 return static_cast<CookieTreeOriginNode*>(node); |
| 45 } |
| 46 |
| 47 std::string GetInfobarLabel(ContentSetting setting, |
| 48 const string16& domain_name) { |
| 49 switch (setting) { |
| 50 case CONTENT_SETTING_BLOCK: |
| 51 return l10n_util::GetStringFUTF8( |
| 52 IDS_COLLECTED_COOKIES_BLOCK_RULE_CREATED, domain_name); |
| 53 |
| 54 case CONTENT_SETTING_ALLOW: |
| 55 return l10n_util::GetStringFUTF8( |
| 56 IDS_COLLECTED_COOKIES_ALLOW_RULE_CREATED, domain_name); |
| 57 |
| 58 case CONTENT_SETTING_SESSION_ONLY: |
| 59 return l10n_util::GetStringFUTF8( |
| 60 IDS_COLLECTED_COOKIES_SESSION_RULE_CREATED, domain_name); |
| 61 |
| 62 default: |
| 63 NOTREACHED(); |
| 64 return std::string(); |
| 65 } |
| 66 } |
| 67 |
| 68 class CollectedCookiesSource : public ChromeURLDataManager::DataSource { |
| 69 public: |
| 70 explicit CollectedCookiesSource(bool block_third_party_cookies) |
| 71 : DataSource(chrome::kChromeUICollectedCookiesHost, |
| 72 MessageLoop::current()), |
| 73 block_third_party_cookies_(block_third_party_cookies) { |
| 74 } |
| 75 |
| 76 virtual void StartDataRequest(const std::string& path, |
| 77 bool is_off_the_record, |
| 78 int request_id); |
| 79 |
| 80 virtual std::string GetMimeType(const std::string& path) const { |
| 81 return "text/html"; |
| 82 } |
| 83 |
| 84 private: |
| 85 virtual ~CollectedCookiesSource() {} |
| 86 |
| 87 bool block_third_party_cookies_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(CollectedCookiesSource); |
| 90 }; |
| 91 |
| 92 void CollectedCookiesSource::StartDataRequest(const std::string& path, |
| 93 bool is_off_the_record, |
| 94 int request_id) { |
| 95 DictionaryValue localized_strings; |
| 96 localized_strings.SetString("title", |
| 97 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_DIALOG_TITLE)); |
| 98 |
| 99 localized_strings.SetString("title", |
| 100 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_DIALOG_TITLE)); |
| 101 localized_strings.SetString("allowedCookies", |
| 102 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_ALLOWED_COOKIES_LABEL)); |
| 103 localized_strings.SetString("blockButton", |
| 104 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_BLOCK_BUTTON)); |
| 105 |
| 106 localized_strings.SetString("blockedCookies", |
| 107 l10n_util::GetStringUTF16(block_third_party_cookies_ ? |
| 108 IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED : |
| 109 IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL)); |
| 110 |
| 111 localized_strings.SetString("allowButton", |
| 112 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_ALLOW_BUTTON)); |
| 113 localized_strings.SetString("allowThisSessionButton", |
| 114 l10n_util::GetStringUTF16(IDS_COLLECTED_COOKIES_SESSION_ONLY_BUTTON)); |
| 115 |
| 116 SetFontAndTextDirection(&localized_strings); |
| 117 |
| 118 static const base::StringPiece html( |
| 119 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 120 IDR_COLLECTED_COOKIES_HTML)); |
| 121 const std::string response = jstemplate_builder::GetI18nTemplateHtml( |
| 122 html, &localized_strings); |
| 123 |
| 124 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 125 html_bytes->data.resize(response.size()); |
| 126 std::copy(response.begin(), response.end(), html_bytes->data.begin()); |
| 127 |
| 128 SendResponse(request_id, html_bytes); |
| 129 } |
| 130 |
| 131 } // namespace |
| 132 |
| 133 // static |
| 134 void CollectedCookiesUIDelegate::Show(TabContents* tab_contents) { |
| 135 CollectedCookiesUIDelegate* delegate = |
| 136 new CollectedCookiesUIDelegate(tab_contents); |
| 137 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(tab_contents->profile(), |
| 138 delegate, |
| 139 tab_contents); |
| 140 } |
| 141 |
| 142 CollectedCookiesUIDelegate::CollectedCookiesUIDelegate( |
| 143 TabContents* tab_contents) |
| 144 : tab_contents_(tab_contents), |
| 145 closed_(false) { |
| 146 TabSpecificContentSettings* content_settings = |
| 147 tab_contents->GetTabSpecificContentSettings(); |
| 148 HostContentSettingsMap* host_content_settings_map = |
| 149 tab_contents_->profile()->GetHostContentSettingsMap(); |
| 150 |
| 151 registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN, |
| 152 Source<TabSpecificContentSettings>(content_settings)); |
| 153 |
| 154 allowed_cookies_tree_model_.reset( |
| 155 content_settings->GetAllowedCookiesTreeModel()); |
| 156 blocked_cookies_tree_model_.reset( |
| 157 content_settings->GetBlockedCookiesTreeModel()); |
| 158 |
| 159 CollectedCookiesSource* source = new CollectedCookiesSource( |
| 160 host_content_settings_map->BlockThirdPartyCookies()); |
| 161 tab_contents->profile()->GetChromeURLDataManager()->AddDataSource(source); |
| 162 } |
| 163 |
| 164 CollectedCookiesUIDelegate::~CollectedCookiesUIDelegate() { |
| 165 } |
| 166 |
| 167 bool CollectedCookiesUIDelegate::IsDialogModal() const { |
| 168 return false; |
| 169 } |
| 170 |
| 171 std::wstring CollectedCookiesUIDelegate::GetDialogTitle() const { |
| 172 return L""; |
| 173 } |
| 174 |
| 175 GURL CollectedCookiesUIDelegate::GetDialogContentURL() const { |
| 176 return GURL(chrome::kChromeUICollectedCookiesURL); |
| 177 } |
| 178 |
| 179 void CollectedCookiesUIDelegate::GetWebUIMessageHandlers( |
| 180 std::vector<WebUIMessageHandler*>* handlers) const { |
| 181 const WebUIMessageHandler* handler = this; |
| 182 handlers->push_back(const_cast<WebUIMessageHandler*>(handler)); |
| 183 } |
| 184 |
| 185 void CollectedCookiesUIDelegate::GetDialogSize(gfx::Size* size) const { |
| 186 size->set_width(kDialogWidth); |
| 187 size->set_height(kDialogHeight); |
| 188 } |
| 189 |
| 190 std::string CollectedCookiesUIDelegate::GetDialogArgs() const { |
| 191 return ""; |
| 192 } |
| 193 |
| 194 void CollectedCookiesUIDelegate::OnDialogClosed( |
| 195 const std::string& json_retval) { |
| 196 closed_ = true; |
| 197 } |
| 198 |
| 199 bool CollectedCookiesUIDelegate::ShouldShowDialogTitle() const { |
| 200 return true; |
| 201 } |
| 202 |
| 203 void CollectedCookiesUIDelegate::RegisterMessages() { |
| 204 web_ui_->RegisterMessageCallback("bindCookiesTreeModel", |
| 205 NewCallback(this, &CollectedCookiesUIDelegate::BindCookiesTreeModel)); |
| 206 web_ui_->RegisterMessageCallback("block", |
| 207 NewCallback(this, &CollectedCookiesUIDelegate::Block)); |
| 208 web_ui_->RegisterMessageCallback("allow", |
| 209 NewCallback(this, &CollectedCookiesUIDelegate::Allow)); |
| 210 web_ui_->RegisterMessageCallback("allowThisSession", |
| 211 NewCallback(this, &CollectedCookiesUIDelegate::AllowThisSession)); |
| 212 |
| 213 allowed_cookies_adapter_.Init(web_ui_); |
| 214 blocked_cookies_adapter_.Init(web_ui_); |
| 215 } |
| 216 |
| 217 void CollectedCookiesUIDelegate::CloseDialog() { |
| 218 if (!closed_ && web_ui_) |
| 219 web_ui_->CallJavascriptFunction(L"closeDialog"); |
| 220 } |
| 221 |
| 222 void CollectedCookiesUIDelegate::SetInfobarLabel(const std::string& text) { |
| 223 StringValue string(text); |
| 224 web_ui_->CallJavascriptFunction(L"setInfobarLabel", string); |
| 225 } |
| 226 |
| 227 void CollectedCookiesUIDelegate::AddContentException( |
| 228 CookieTreeOriginNode* origin_node, ContentSetting setting) { |
| 229 if (origin_node->CanCreateContentException()) { |
| 230 origin_node->CreateContentException( |
| 231 tab_contents_->profile()->GetHostContentSettingsMap(), setting); |
| 232 |
| 233 SetInfobarLabel(GetInfobarLabel(setting, origin_node->GetTitle())); |
| 234 } |
| 235 } |
| 236 |
| 237 void CollectedCookiesUIDelegate::Observe(NotificationType type, |
| 238 const NotificationSource& source, |
| 239 const NotificationDetails& details) { |
| 240 DCHECK(type == NotificationType::COLLECTED_COOKIES_SHOWN); |
| 241 DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(), |
| 242 tab_contents_->GetTabSpecificContentSettings()); |
| 243 CloseDialog(); |
| 244 } |
| 245 |
| 246 void CollectedCookiesUIDelegate::BindCookiesTreeModel(const ListValue* args) { |
| 247 allowed_cookies_adapter_.Bind("allowedCookies", |
| 248 allowed_cookies_tree_model_.get()); |
| 249 blocked_cookies_adapter_.Bind("blockedCookies", |
| 250 blocked_cookies_tree_model_.get()); |
| 251 } |
| 252 |
| 253 void CollectedCookiesUIDelegate::Block(const ListValue* args) { |
| 254 std::string node_path; |
| 255 if (!args->GetString(0, &node_path)) |
| 256 return; |
| 257 |
| 258 CookieTreeOriginNode* origin_node = GetOriginNode( |
| 259 allowed_cookies_tree_model_.get(), node_path); |
| 260 if (!origin_node) |
| 261 return; |
| 262 |
| 263 AddContentException(origin_node, CONTENT_SETTING_BLOCK); |
| 264 } |
| 265 |
| 266 void CollectedCookiesUIDelegate::Allow(const ListValue* args) { |
| 267 std::string node_path; |
| 268 if (!args->GetString(0, &node_path)) |
| 269 return; |
| 270 |
| 271 CookieTreeOriginNode* origin_node = GetOriginNode( |
| 272 blocked_cookies_tree_model_.get(), node_path); |
| 273 if (!origin_node) |
| 274 return; |
| 275 |
| 276 AddContentException(origin_node, CONTENT_SETTING_ALLOW); |
| 277 } |
| 278 |
| 279 void CollectedCookiesUIDelegate::AllowThisSession(const ListValue* args) { |
| 280 std::string node_path; |
| 281 if (!args->GetString(0, &node_path)) |
| 282 return; |
| 283 |
| 284 CookieTreeOriginNode* origin_node = GetOriginNode( |
| 285 blocked_cookies_tree_model_.get(), node_path); |
| 286 if (!origin_node) |
| 287 return; |
| 288 |
| 289 AddContentException(origin_node, CONTENT_SETTING_SESSION_ONLY); |
| 290 } |
OLD | NEW |