OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options2/cookies_view_handler2.h" | 5 #include "chrome/browser/ui/webui/options2/cookies_view_handler2.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/common/extensions/extension.h" | |
12 #include "chrome/common/extensions/extension_set.h" | |
11 #include "chrome/browser/browsing_data_appcache_helper.h" | 13 #include "chrome/browser/browsing_data_appcache_helper.h" |
12 #include "chrome/browser/browsing_data_cookie_helper.h" | 14 #include "chrome/browser/browsing_data_cookie_helper.h" |
13 #include "chrome/browser/browsing_data_database_helper.h" | 15 #include "chrome/browser/browsing_data_database_helper.h" |
14 #include "chrome/browser/browsing_data_file_system_helper.h" | 16 #include "chrome/browser/browsing_data_file_system_helper.h" |
15 #include "chrome/browser/browsing_data_indexed_db_helper.h" | 17 #include "chrome/browser/browsing_data_indexed_db_helper.h" |
16 #include "chrome/browser/browsing_data_local_storage_helper.h" | 18 #include "chrome/browser/browsing_data_local_storage_helper.h" |
17 #include "chrome/browser/browsing_data_quota_helper.h" | 19 #include "chrome/browser/browsing_data_quota_helper.h" |
18 #include "chrome/browser/browsing_data_server_bound_cert_helper.h" | 20 #include "chrome/browser/browsing_data_server_bound_cert_helper.h" |
21 #include "chrome/browser/extensions/extension_service.h" | |
19 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
20 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" | 23 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" |
21 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
22 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
23 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
24 | 27 |
25 namespace options2 { | 28 namespace options2 { |
26 | 29 |
27 CookiesViewHandler::CookiesViewHandler() : batch_update_(false) { | 30 CookiesViewHandler::CookiesViewHandler() |
31 : batch_update_(false), | |
32 app_context_(false) { | |
28 } | 33 } |
29 | 34 |
30 CookiesViewHandler::~CookiesViewHandler() { | 35 CookiesViewHandler::~CookiesViewHandler() { |
31 } | 36 } |
32 | 37 |
33 void CookiesViewHandler::GetLocalizedValues( | 38 void CookiesViewHandler::GetLocalizedValues( |
34 DictionaryValue* localized_strings) { | 39 DictionaryValue* localized_strings) { |
35 DCHECK(localized_strings); | 40 DCHECK(localized_strings); |
36 | 41 |
37 static OptionsStringResource resources[] = { | 42 static OptionsStringResource resources[] = { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 IDS_COOKIES_SERVER_BOUND_CERT_TYPE_LABEL }, | 86 IDS_COOKIES_SERVER_BOUND_CERT_TYPE_LABEL }, |
82 { "label_server_bound_cert_created", | 87 { "label_server_bound_cert_created", |
83 IDS_COOKIES_SERVER_BOUND_CERT_CREATED_LABEL }, | 88 IDS_COOKIES_SERVER_BOUND_CERT_CREATED_LABEL }, |
84 { "label_server_bound_cert_expires", | 89 { "label_server_bound_cert_expires", |
85 IDS_COOKIES_SERVER_BOUND_CERT_EXPIRES_LABEL }, | 90 IDS_COOKIES_SERVER_BOUND_CERT_EXPIRES_LABEL }, |
86 }; | 91 }; |
87 | 92 |
88 RegisterStrings(localized_strings, resources, arraysize(resources)); | 93 RegisterStrings(localized_strings, resources, arraysize(resources)); |
89 RegisterTitle(localized_strings, "cookiesViewPage", | 94 RegisterTitle(localized_strings, "cookiesViewPage", |
90 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); | 95 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); |
96 RegisterTitle(localized_strings, "appCookiesViewPage", | |
97 IDS_APP_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); | |
91 } | 98 } |
92 | 99 |
93 void CookiesViewHandler::RegisterMessages() { | 100 void CookiesViewHandler::RegisterMessages() { |
94 web_ui()->RegisterMessageCallback("updateCookieSearchResults", | 101 web_ui()->RegisterMessageCallback("updateCookieSearchResults", |
95 base::Bind(&CookiesViewHandler::UpdateSearchResults, | 102 base::Bind(&CookiesViewHandler::UpdateSearchResults, |
96 base::Unretained(this))); | 103 base::Unretained(this))); |
97 web_ui()->RegisterMessageCallback("removeAllCookies", | 104 web_ui()->RegisterMessageCallback("removeAllCookies", |
98 base::Bind(&CookiesViewHandler::RemoveAll, | 105 base::Bind(&CookiesViewHandler::RemoveAll, |
99 base::Unretained(this))); | 106 base::Unretained(this))); |
100 web_ui()->RegisterMessageCallback("removeCookie", | 107 web_ui()->RegisterMessageCallback("removeCookie", |
101 base::Bind(&CookiesViewHandler::Remove, | 108 base::Bind(&CookiesViewHandler::Remove, |
102 base::Unretained(this))); | 109 base::Unretained(this))); |
103 web_ui()->RegisterMessageCallback("loadCookie", | 110 web_ui()->RegisterMessageCallback("loadCookie", |
104 base::Bind(&CookiesViewHandler::LoadChildren, | 111 base::Bind(&CookiesViewHandler::LoadChildren, |
105 base::Unretained(this))); | 112 base::Unretained(this))); |
113 web_ui()->RegisterMessageCallback("setViewContext", | |
114 base::Bind(&CookiesViewHandler::SetViewContext, | |
115 base::Unretained(this))); | |
106 } | 116 } |
107 | 117 |
108 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, | 118 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, |
109 ui::TreeModelNode* parent, | 119 ui::TreeModelNode* parent, |
110 int start, | 120 int start, |
111 int count) { | 121 int count) { |
112 // Skip if there is a batch update in progress. | 122 // Skip if there is a batch update in progress. |
113 if (batch_update_) | 123 if (batch_update_) |
114 return; | 124 return; |
115 | 125 |
116 CookieTreeNode* parent_node = cookies_tree_model_->AsNode(parent); | 126 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model); |
127 CookieTreeNode* parent_node = tree_model->AsNode(parent); | |
117 | 128 |
118 ListValue* children = new ListValue; | 129 ListValue* children = new ListValue; |
119 cookies_tree_model_util::GetChildNodeList(parent_node, start, count, | 130 cookies_tree_model_util::GetChildNodeList(parent_node, start, count, |
120 children); | 131 children); |
121 | 132 |
122 ListValue args; | 133 ListValue args; |
123 args.Append(parent == cookies_tree_model_->GetRoot() ? | 134 args.Append(parent == tree_model->GetRoot() ? |
124 Value::CreateNullValue() : | 135 Value::CreateNullValue() : |
125 Value::CreateStringValue( | 136 Value::CreateStringValue( |
126 cookies_tree_model_util::GetTreeNodeId(parent_node))); | 137 cookies_tree_model_util::GetTreeNodeId(parent_node))); |
127 args.Append(Value::CreateIntegerValue(start)); | 138 args.Append(Value::CreateIntegerValue(start)); |
128 args.Append(children); | 139 args.Append(children); |
129 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args); | 140 if (tree_model == app_cookies_tree_model_) { |
141 web_ui()->CallJavascriptFunction("AppCookiesView.onTreeItemAdded", args); | |
142 } else { | |
143 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args); | |
144 } | |
130 } | 145 } |
131 | 146 |
132 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, | 147 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, |
133 ui::TreeModelNode* parent, | 148 ui::TreeModelNode* parent, |
134 int start, | 149 int start, |
135 int count) { | 150 int count) { |
136 // Skip if there is a batch update in progress. | 151 // Skip if there is a batch update in progress. |
137 if (batch_update_) | 152 if (batch_update_) |
138 return; | 153 return; |
139 | 154 |
155 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model); | |
156 | |
140 ListValue args; | 157 ListValue args; |
141 args.Append(parent == cookies_tree_model_->GetRoot() ? | 158 args.Append(parent == tree_model->GetRoot() ? |
142 Value::CreateNullValue() : | 159 Value::CreateNullValue() : |
143 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId( | 160 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId( |
144 cookies_tree_model_->AsNode(parent)))); | 161 tree_model->AsNode(parent)))); |
145 args.Append(Value::CreateIntegerValue(start)); | 162 args.Append(Value::CreateIntegerValue(start)); |
146 args.Append(Value::CreateIntegerValue(count)); | 163 args.Append(Value::CreateIntegerValue(count)); |
147 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args); | 164 if (tree_model == app_cookies_tree_model_) { |
165 web_ui()->CallJavascriptFunction("AppCookiesView.onTreeItemRemoved", args); | |
166 } else { | |
167 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args); | |
168 } | |
148 } | 169 } |
149 | 170 |
150 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { | 171 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { |
151 DCHECK(!batch_update_); // There should be no nested batch begin. | 172 DCHECK(!batch_update_); // There should be no nested batch begin. |
152 batch_update_ = true; | 173 batch_update_ = true; |
153 } | 174 } |
154 | 175 |
155 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) { | 176 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) { |
156 DCHECK(batch_update_); | 177 DCHECK(batch_update_); |
157 batch_update_ = false; | 178 batch_update_ = false; |
158 | 179 |
159 SendChildren(cookies_tree_model_->GetRoot()); | 180 SendChildren(model->GetRoot()); |
160 } | 181 } |
161 | 182 |
162 void CookiesViewHandler::EnsureCookiesTreeModelCreated() { | 183 void CookiesViewHandler::EnsureCookiesTreeModelCreated() { |
163 if (!cookies_tree_model_.get()) { | 184 if (!app_context_ && !cookies_tree_model_.get()) { |
164 Profile* profile = Profile::FromWebUI(web_ui()); | 185 Profile* profile = Profile::FromWebUI(web_ui()); |
165 cookies_tree_model_.reset(new CookiesTreeModel( | 186 ContainerMap apps_map; |
187 apps_map[std::string()] = new LocalDataContainer( | |
188 "Site Data", std::string(), | |
166 new BrowsingDataCookieHelper(profile->GetRequestContext()), | 189 new BrowsingDataCookieHelper(profile->GetRequestContext()), |
167 new BrowsingDataDatabaseHelper(profile), | 190 new BrowsingDataDatabaseHelper(profile), |
168 new BrowsingDataLocalStorageHelper(profile), | 191 new BrowsingDataLocalStorageHelper(profile), |
169 NULL, | 192 NULL, |
170 new BrowsingDataAppCacheHelper(profile), | 193 new BrowsingDataAppCacheHelper(profile), |
171 BrowsingDataIndexedDBHelper::Create(profile), | 194 BrowsingDataIndexedDBHelper::Create(profile), |
172 BrowsingDataFileSystemHelper::Create(profile), | 195 BrowsingDataFileSystemHelper::Create(profile), |
173 BrowsingDataQuotaHelper::Create(profile), | 196 BrowsingDataQuotaHelper::Create(profile), |
174 BrowsingDataServerBoundCertHelper::Create(profile), | 197 BrowsingDataServerBoundCertHelper::Create(profile)); |
175 false)); | 198 cookies_tree_model_.reset(new CookiesTreeModel(apps_map, false)); |
176 cookies_tree_model_->AddCookiesTreeObserver(this); | 199 cookies_tree_model_->AddCookiesTreeObserver(this); |
177 } | 200 } |
201 | |
202 if (app_context_ && !app_cookies_tree_model_.get()) { | |
203 Profile* profile = Profile::FromWebUI(web_ui()); | |
204 ContainerMap apps_map; | |
205 const ExtensionService* service = profile->GetExtensionService(); | |
206 if (service) { | |
207 const ExtensionSet* extensions = service->extensions(); | |
208 for (ExtensionSet::const_iterator it = extensions->begin(); | |
209 it != extensions->end(); ++it) { | |
210 if ((*it)->is_storage_isolated()) { | |
211 net::URLRequestContextGetter* context_getter = | |
212 profile->GetRequestContextForIsolatedApp((*it)->id()); | |
213 // TODO(nasko): When new types of storage are isolated, add the | |
214 // appropriate browsing data helper objects to the consutrctor. | |
215 // For now, just cookies are isolated, so other parameters are NULL. | |
216 apps_map[(*it)->id()] = new LocalDataContainer( | |
217 (*it)->name(), (*it)->id(), | |
218 new BrowsingDataCookieHelper(context_getter), | |
219 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
220 } | |
221 } | |
222 app_cookies_tree_model_.reset(new CookiesTreeModel(apps_map, false)); | |
223 app_cookies_tree_model_->AddCookiesTreeObserver(this); | |
224 } | |
225 } | |
178 } | 226 } |
179 | 227 |
180 void CookiesViewHandler::UpdateSearchResults(const ListValue* args) { | 228 void CookiesViewHandler::UpdateSearchResults(const ListValue* args) { |
181 std::string query; | 229 string16 query; |
182 if (!args->GetString(0, &query)) { | 230 if (!args->GetString(0, &query)) { |
183 return; | 231 return; |
184 } | 232 } |
185 | 233 |
186 EnsureCookiesTreeModelCreated(); | 234 EnsureCookiesTreeModelCreated(); |
187 | 235 |
188 cookies_tree_model_->UpdateSearchResults(UTF8ToWide(query)); | 236 GetTreeModel()->UpdateSearchResults(query); |
189 } | 237 } |
190 | 238 |
191 void CookiesViewHandler::RemoveAll(const ListValue* args) { | 239 void CookiesViewHandler::RemoveAll(const ListValue* args) { |
192 EnsureCookiesTreeModelCreated(); | 240 EnsureCookiesTreeModelCreated(); |
193 cookies_tree_model_->DeleteAllStoredObjects(); | 241 GetTreeModel()->DeleteAllStoredObjects(); |
194 } | 242 } |
195 | 243 |
196 void CookiesViewHandler::Remove(const ListValue* args) { | 244 void CookiesViewHandler::Remove(const ListValue* args) { |
197 std::string node_path; | 245 std::string node_path; |
198 if (!args->GetString(0, &node_path)) { | 246 if (!args->GetString(0, &node_path)) { |
199 return; | 247 return; |
200 } | 248 } |
201 | 249 |
202 EnsureCookiesTreeModelCreated(); | 250 EnsureCookiesTreeModelCreated(); |
203 | 251 |
204 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( | 252 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( |
205 cookies_tree_model_->GetRoot(), node_path); | 253 GetTreeModel()->GetRoot(), node_path); |
206 if (node) | 254 if (node) |
207 cookies_tree_model_->DeleteCookieNode(node); | 255 GetTreeModel()->DeleteCookieNode(node); |
208 } | 256 } |
209 | 257 |
210 void CookiesViewHandler::LoadChildren(const ListValue* args) { | 258 void CookiesViewHandler::LoadChildren(const ListValue* args) { |
211 std::string node_path; | 259 std::string node_path; |
212 if (!args->GetString(0, &node_path)) { | 260 if (!args->GetString(0, &node_path)) { |
213 return; | 261 return; |
214 } | 262 } |
215 | 263 |
216 EnsureCookiesTreeModelCreated(); | 264 EnsureCookiesTreeModelCreated(); |
217 | 265 |
218 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( | 266 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( |
219 cookies_tree_model_->GetRoot(), node_path); | 267 GetTreeModel()->GetRoot(), node_path); |
220 if (node) | 268 if (node) |
221 SendChildren(node); | 269 SendChildren(node); |
222 } | 270 } |
223 | 271 |
224 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) { | 272 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) { |
225 ListValue* children = new ListValue; | 273 ListValue* children = new ListValue; |
226 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->child_count(), | 274 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->child_count(), |
227 children); | 275 children); |
228 | 276 |
229 ListValue args; | 277 ListValue args; |
230 args.Append(parent == cookies_tree_model_->GetRoot() ? | 278 args.Append(parent == GetTreeModel()->GetRoot() ? |
231 Value::CreateNullValue() : | 279 Value::CreateNullValue() : |
232 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent))); | 280 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent))); |
233 args.Append(children); | 281 args.Append(children); |
234 | 282 |
235 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args); | 283 if (GetTreeModel() == app_cookies_tree_model_) { |
284 web_ui()->CallJavascriptFunction("AppCookiesView.loadChildren", args); | |
Evan Stade
2012/07/03 18:11:28
why not use a helper function like what you had be
nasko
2012/07/09 17:07:06
Done.
| |
285 } else { | |
286 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args); | |
287 } | |
288 } | |
289 | |
290 void CookiesViewHandler::SetViewContext(const base::ListValue* args) { | |
291 bool app_context = false; | |
292 if (args->GetBoolean(0, &app_context)) | |
293 app_context_ = app_context; | |
294 } | |
295 | |
296 CookiesTreeModel* CookiesViewHandler::GetTreeModel() { | |
297 CookiesTreeModel* model = app_context_ ? | |
298 app_cookies_tree_model_.get() : cookies_tree_model_.get(); | |
299 DCHECK(model); | |
300 return model; | |
236 } | 301 } |
237 | 302 |
238 } // namespace options2 | 303 } // namespace options2 |
OLD | NEW |