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