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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/options/cookies_view_handler.h"
10 #include "base/bind_helpers.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
14 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
16 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
17 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
20 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h"
21 #include "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/webui/cookies_tree_model_util.h"
24 #include "content/public/browser/browser_context.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/browser/site_instance.h"
27 #include "content/public/browser/storage_partition.h"
28 #include "content/public/browser/web_ui.h"
29 #include "grit/generated_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
33 class FileSystemContext;
38 CookiesViewHandler::CookiesViewHandler()
39 : batch_update_(false),
40 model_util_(new CookiesTreeModelUtil) {
43 CookiesViewHandler::~CookiesViewHandler() {
46 void CookiesViewHandler::GetLocalizedValues(
47 DictionaryValue* localized_strings) {
48 DCHECK(localized_strings);
50 static OptionsStringResource resources[] = {
51 { "label_cookie_name", IDS_COOKIES_COOKIE_NAME_LABEL },
52 { "label_cookie_content", IDS_COOKIES_COOKIE_CONTENT_LABEL },
53 { "label_cookie_domain", IDS_COOKIES_COOKIE_DOMAIN_LABEL },
54 { "label_cookie_path", IDS_COOKIES_COOKIE_PATH_LABEL },
55 { "label_cookie_send_for", IDS_COOKIES_COOKIE_SENDFOR_LABEL },
56 { "label_cookie_accessible_to_script",
57 IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_LABEL },
58 { "label_cookie_created", IDS_COOKIES_COOKIE_CREATED_LABEL },
59 { "label_cookie_expires", IDS_COOKIES_COOKIE_EXPIRES_LABEL },
60 { "label_webdb_desc", IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL },
61 { "label_local_storage_size",
62 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL },
63 { "label_local_storage_last_modified",
64 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL },
65 { "label_local_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL },
66 { "label_indexed_db_size", IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL },
67 { "label_indexed_db_last_modified",
68 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL },
69 { "label_indexed_db_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL },
70 { "label_app_cache_manifest",
71 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL },
72 { "label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL },
73 { "cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER },
74 { "cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER },
75 { "cookie_singular", IDS_COOKIES_SINGLE_COOKIE },
76 { "cookie_plural", IDS_COOKIES_PLURAL_COOKIES },
77 { "cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE },
78 { "cookie_indexed_db", IDS_COOKIES_INDEXED_DB },
79 { "cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE },
80 { "cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE },
81 { "cookie_flash_lso", IDS_COOKIES_FLASH_LSO },
82 { "search_cookies", IDS_COOKIES_SEARCH_COOKIES },
83 { "remove_cookie", IDS_COOKIES_REMOVE_LABEL },
84 { "remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL },
85 { "cookie_file_system", IDS_COOKIES_FILE_SYSTEM },
86 { "label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL },
87 { "label_file_system_temporary_usage",
88 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL },
89 { "label_file_system_persistent_usage",
90 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL },
91 { "cookie_server_bound_cert", IDS_COOKIES_SERVER_BOUND_CERT },
92 { "label_server_bound_cert_server_id",
93 IDS_COOKIES_SERVER_BOUND_CERT_ORIGIN_LABEL },
94 { "label_server_bound_cert_type",
95 IDS_COOKIES_SERVER_BOUND_CERT_TYPE_LABEL },
96 { "label_server_bound_cert_created",
97 IDS_COOKIES_SERVER_BOUND_CERT_CREATED_LABEL },
98 { "label_server_bound_cert_expires",
99 IDS_COOKIES_SERVER_BOUND_CERT_EXPIRES_LABEL },
100 { "label_protected_by_apps",
101 IDS_GEOLOCATION_SET_BY_HOVER }, // TODO(bauerb): Use a better string
104 RegisterStrings(localized_strings, resources, arraysize(resources));
105 RegisterTitle(localized_strings, "cookiesViewPage",
106 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE);
109 void CookiesViewHandler::RegisterMessages() {
110 web_ui()->RegisterMessageCallback("updateCookieSearchResults",
111 base::Bind(&CookiesViewHandler::UpdateSearchResults,
112 base::Unretained(this)));
113 web_ui()->RegisterMessageCallback("removeAllCookies",
114 base::Bind(&CookiesViewHandler::RemoveAll,
115 base::Unretained(this)));
116 web_ui()->RegisterMessageCallback("removeCookie",
117 base::Bind(&CookiesViewHandler::Remove,
118 base::Unretained(this)));
119 web_ui()->RegisterMessageCallback("loadCookie",
120 base::Bind(&CookiesViewHandler::LoadChildren,
121 base::Unretained(this)));
122 web_ui()->RegisterMessageCallback("reloadCookies",
123 base::Bind(&CookiesViewHandler::ReloadCookies,
124 base::Unretained(this)));
127 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model,
128 ui::TreeModelNode* parent,
131 // Skip if there is a batch update in progress.
135 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model);
136 CookieTreeNode* parent_node = tree_model->AsNode(parent);
138 ListValue* children = new ListValue;
139 model_util_->GetChildNodeList(parent_node, start, count, children);
142 args.Append(parent == tree_model->GetRoot() ?
143 Value::CreateNullValue() :
144 new base::StringValue(model_util_->GetTreeNodeId(parent_node)));
145 args.Append(new base::FundamentalValue(start));
146 args.Append(children);
147 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args);
150 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model,
151 ui::TreeModelNode* parent,
154 // Skip if there is a batch update in progress.
158 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model);
161 args.Append(parent == tree_model->GetRoot() ?
162 Value::CreateNullValue() :
163 new base::StringValue(model_util_->GetTreeNodeId(
164 tree_model->AsNode(parent))));
165 args.Append(new base::FundamentalValue(start));
166 args.Append(new base::FundamentalValue(count));
167 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args);
170 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) {
171 DCHECK(!batch_update_); // There should be no nested batch begin.
172 batch_update_ = true;
175 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) {
176 DCHECK(batch_update_);
177 batch_update_ = false;
179 SendChildren(model->GetRoot());
182 void CookiesViewHandler::EnsureCookiesTreeModelCreated() {
183 if (!cookies_tree_model_.get()) {
184 Profile* profile = Profile::FromWebUI(web_ui());
185 content::StoragePartition* storage_partition =
186 content::BrowserContext::GetDefaultStoragePartition(profile);
187 content::IndexedDBContext* indexed_db_context =
188 storage_partition->GetIndexedDBContext();
189 fileapi::FileSystemContext* file_system_context =
190 storage_partition->GetFileSystemContext();
191 LocalDataContainer* container = new LocalDataContainer(
192 new BrowsingDataCookieHelper(profile->GetRequestContext()),
193 new BrowsingDataDatabaseHelper(profile),
194 new BrowsingDataLocalStorageHelper(profile),
196 new BrowsingDataAppCacheHelper(profile),
197 BrowsingDataIndexedDBHelper::Create(indexed_db_context),
198 BrowsingDataFileSystemHelper::Create(file_system_context),
199 BrowsingDataQuotaHelper::Create(profile),
200 BrowsingDataServerBoundCertHelper::Create(profile),
201 BrowsingDataFlashLSOHelper::Create(profile));
202 cookies_tree_model_.reset(
203 new CookiesTreeModel(container,
204 profile->GetExtensionSpecialStoragePolicy(),
206 cookies_tree_model_->AddCookiesTreeObserver(this);
210 void CookiesViewHandler::UpdateSearchResults(const ListValue* args) {
212 if (!args->GetString(0, &query))
215 EnsureCookiesTreeModelCreated();
217 cookies_tree_model_->UpdateSearchResults(query);
220 void CookiesViewHandler::RemoveAll(const ListValue* args) {
221 EnsureCookiesTreeModelCreated();
222 cookies_tree_model_->DeleteAllStoredObjects();
225 void CookiesViewHandler::Remove(const ListValue* args) {
226 std::string node_path;
227 if (!args->GetString(0, &node_path))
230 EnsureCookiesTreeModelCreated();
232 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath(
233 cookies_tree_model_->GetRoot(), node_path);
235 cookies_tree_model_->DeleteCookieNode(const_cast<CookieTreeNode*>(node));
238 void CookiesViewHandler::LoadChildren(const ListValue* args) {
239 std::string node_path;
240 if (!args->GetString(0, &node_path))
243 EnsureCookiesTreeModelCreated();
245 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath(
246 cookies_tree_model_->GetRoot(), node_path);
251 void CookiesViewHandler::SendChildren(const CookieTreeNode* parent) {
252 ListValue* children = new ListValue;
253 model_util_->GetChildNodeList(parent, 0, parent->child_count(),
257 args.Append(parent == cookies_tree_model_->GetRoot() ?
258 Value::CreateNullValue() :
259 new base::StringValue(model_util_->GetTreeNodeId(parent)));
260 args.Append(children);
262 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args);
265 void CookiesViewHandler::ReloadCookies(const base::ListValue* args) {
266 cookies_tree_model_.reset();
268 EnsureCookiesTreeModelCreated();
271 } // namespace options