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) { | |
oshima
2011/03/10 18:39:09
node && node->Get....
would be simpler.
xiyuan
2011/03/10 19:24:00
Done.
| |
36 if (node->GetDetailedInfo().node_type == | |
37 CookieTreeNode::DetailedInfo::TYPE_ORIGIN) { | |
38 break; | |
39 } | |
40 | |
41 node = node->parent(); | |
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(); | |
oshima
2011/03/10 18:39:09
you may wan to log setting in NOTREACHED
xiyuan
2011/03/10 19:24:00
Done.
| |
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 localized_strings.SetString("closeButton", | |
116 l10n_util::GetStringUTF16(IDS_CLOSE)); | |
117 | |
118 SetFontAndTextDirection(&localized_strings); | |
119 | |
120 static const base::StringPiece html( | |
121 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
122 IDR_COLLECTED_COOKIES_HTML)); | |
123 const std::string response = jstemplate_builder::GetI18nTemplateHtml( | |
124 html, &localized_strings); | |
125 | |
126 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
127 html_bytes->data.resize(response.size()); | |
128 std::copy(response.begin(), response.end(), html_bytes->data.begin()); | |
129 | |
130 SendResponse(request_id, html_bytes); | |
131 } | |
132 | |
133 } // namespace | |
134 | |
135 // static | |
136 void CollectedCookiesUIDelegate::Show(TabContents* tab_contents) { | |
137 CollectedCookiesUIDelegate* delegate = | |
138 new CollectedCookiesUIDelegate(tab_contents); | |
139 ConstrainedHtmlUI::CreateConstrainedHtmlDialog(tab_contents->profile(), | |
140 delegate, | |
141 tab_contents); | |
142 } | |
143 | |
144 CollectedCookiesUIDelegate::CollectedCookiesUIDelegate( | |
145 TabContents* tab_contents) | |
146 : tab_contents_(tab_contents), | |
147 closed_(false) { | |
148 TabSpecificContentSettings* content_settings = | |
149 tab_contents->GetTabSpecificContentSettings(); | |
150 HostContentSettingsMap* host_content_settings_map = | |
151 tab_contents_->profile()->GetHostContentSettingsMap(); | |
152 | |
153 registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN, | |
154 Source<TabSpecificContentSettings>(content_settings)); | |
155 | |
156 allowed_cookies_tree_model_.reset( | |
157 content_settings->GetAllowedCookiesTreeModel()); | |
158 blocked_cookies_tree_model_.reset( | |
159 content_settings->GetBlockedCookiesTreeModel()); | |
160 | |
161 CollectedCookiesSource* source = new CollectedCookiesSource( | |
162 host_content_settings_map->BlockThirdPartyCookies()); | |
163 tab_contents->profile()->GetChromeURLDataManager()->AddDataSource(source); | |
164 } | |
165 | |
166 CollectedCookiesUIDelegate::~CollectedCookiesUIDelegate() { | |
167 } | |
168 | |
169 bool CollectedCookiesUIDelegate::IsDialogModal() const { | |
170 return false; | |
171 } | |
172 | |
173 std::wstring CollectedCookiesUIDelegate::GetDialogTitle() const { | |
174 return L""; | |
175 } | |
176 | |
177 GURL CollectedCookiesUIDelegate::GetDialogContentURL() const { | |
178 return GURL(chrome::kChromeUICollectedCookiesURL); | |
179 } | |
180 | |
181 void CollectedCookiesUIDelegate::GetWebUIMessageHandlers( | |
182 std::vector<WebUIMessageHandler*>* handlers) const { | |
183 const WebUIMessageHandler* handler = this; | |
184 handlers->push_back(const_cast<WebUIMessageHandler*>(handler)); | |
oshima
2011/03/10 18:39:09
just curious. is handler necessary?
xiyuan
2011/03/10 19:24:00
yep, you need two casts to convert this here to We
| |
185 } | |
186 | |
187 void CollectedCookiesUIDelegate::GetDialogSize(gfx::Size* size) const { | |
188 size->set_width(kDialogWidth); | |
189 size->set_height(kDialogHeight); | |
190 } | |
191 | |
192 std::string CollectedCookiesUIDelegate::GetDialogArgs() const { | |
193 return ""; | |
194 } | |
195 | |
196 void CollectedCookiesUIDelegate::OnDialogClosed( | |
197 const std::string& json_retval) { | |
198 closed_ = true; | |
199 } | |
200 | |
201 bool CollectedCookiesUIDelegate::ShouldShowDialogTitle() const { | |
202 return true; | |
203 } | |
204 | |
205 void CollectedCookiesUIDelegate::RegisterMessages() { | |
206 web_ui_->RegisterMessageCallback("BindCookiesTreeModel", | |
207 NewCallback(this, &CollectedCookiesUIDelegate::BindCookiesTreeModel)); | |
208 web_ui_->RegisterMessageCallback("Block", | |
209 NewCallback(this, &CollectedCookiesUIDelegate::Block)); | |
210 web_ui_->RegisterMessageCallback("Allow", | |
211 NewCallback(this, &CollectedCookiesUIDelegate::Allow)); | |
212 web_ui_->RegisterMessageCallback("AllowThisSession", | |
213 NewCallback(this, &CollectedCookiesUIDelegate::AllowThisSession)); | |
214 | |
215 allowed_cookies_adapter_.Init(web_ui_); | |
216 blocked_cookies_adapter_.Init(web_ui_); | |
217 } | |
218 | |
219 void CollectedCookiesUIDelegate::CloseDialog() { | |
220 if (!closed_ && web_ui_) | |
221 web_ui_->CallJavascriptFunction("closeDialog"); | |
222 } | |
223 | |
224 void CollectedCookiesUIDelegate::SetInfobarLabel(const std::string& text) { | |
225 StringValue string(text); | |
226 web_ui_->CallJavascriptFunction("setInfobarLabel", string); | |
227 } | |
228 | |
229 void CollectedCookiesUIDelegate::AddContentException( | |
230 CookieTreeOriginNode* origin_node, ContentSetting setting) { | |
231 if (origin_node->CanCreateContentException()) { | |
232 origin_node->CreateContentException( | |
233 tab_contents_->profile()->GetHostContentSettingsMap(), setting); | |
234 | |
235 SetInfobarLabel(GetInfobarLabel(setting, origin_node->GetTitle())); | |
236 } | |
237 } | |
238 | |
239 void CollectedCookiesUIDelegate::Observe(NotificationType type, | |
240 const NotificationSource& source, | |
241 const NotificationDetails& details) { | |
242 DCHECK_EQ(type.value, NotificationType::COLLECTED_COOKIES_SHOWN); | |
243 DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(), | |
244 tab_contents_->GetTabSpecificContentSettings()); | |
245 CloseDialog(); | |
246 } | |
247 | |
248 void CollectedCookiesUIDelegate::BindCookiesTreeModel(const ListValue* args) { | |
249 allowed_cookies_adapter_.Bind("allowed-cookies", | |
250 allowed_cookies_tree_model_.get()); | |
251 blocked_cookies_adapter_.Bind("blocked-cookies", | |
252 blocked_cookies_tree_model_.get()); | |
253 } | |
254 | |
255 void CollectedCookiesUIDelegate::Block(const ListValue* args) { | |
256 std::string node_path; | |
257 if (!args->GetString(0, &node_path)) | |
258 return; | |
259 | |
260 CookieTreeOriginNode* origin_node = GetOriginNode( | |
261 allowed_cookies_tree_model_.get(), node_path); | |
262 if (!origin_node) | |
263 return; | |
264 | |
265 AddContentException(origin_node, CONTENT_SETTING_BLOCK); | |
266 } | |
267 | |
268 void CollectedCookiesUIDelegate::Allow(const ListValue* args) { | |
269 std::string node_path; | |
270 if (!args->GetString(0, &node_path)) | |
271 return; | |
272 | |
273 CookieTreeOriginNode* origin_node = GetOriginNode( | |
274 blocked_cookies_tree_model_.get(), node_path); | |
275 if (!origin_node) | |
276 return; | |
277 | |
278 AddContentException(origin_node, CONTENT_SETTING_ALLOW); | |
279 } | |
280 | |
281 void CollectedCookiesUIDelegate::AllowThisSession(const ListValue* args) { | |
282 std::string node_path; | |
283 if (!args->GetString(0, &node_path)) | |
284 return; | |
285 | |
286 CookieTreeOriginNode* origin_node = GetOriginNode( | |
287 blocked_cookies_tree_model_.get(), node_path); | |
288 if (!origin_node) | |
289 return; | |
290 | |
291 AddContentException(origin_node, CONTENT_SETTING_SESSION_ONLY); | |
292 } | |
OLD | NEW |