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_cache_storage_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h"
16 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
17 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
20 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
21 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
22 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h"
23 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/webui/cookies_tree_model_util.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "content/public/browser/browser_context.h"
28 #include "content/public/browser/render_process_host.h"
29 #include "content/public/browser/site_instance.h"
30 #include "content/public/browser/storage_partition.h"
31 #include "content/public/browser/web_ui.h"
34 class FileSystemContext
;
39 CookiesViewHandler::CookiesViewHandler()
40 : batch_update_(false),
41 model_util_(new CookiesTreeModelUtil
) {
44 CookiesViewHandler::~CookiesViewHandler() {
47 void CookiesViewHandler::GetLocalizedValues(
48 base::DictionaryValue
* localized_strings
) {
49 DCHECK(localized_strings
);
51 static OptionsStringResource resources
[] = {
52 {"label_cookie_name", IDS_COOKIES_COOKIE_NAME_LABEL
},
53 {"label_cookie_content", IDS_COOKIES_COOKIE_CONTENT_LABEL
},
54 {"label_cookie_domain", IDS_COOKIES_COOKIE_DOMAIN_LABEL
},
55 {"label_cookie_path", IDS_COOKIES_COOKIE_PATH_LABEL
},
56 {"label_cookie_send_for", IDS_COOKIES_COOKIE_SENDFOR_LABEL
},
57 {"label_cookie_accessible_to_script",
58 IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_LABEL
},
59 {"label_cookie_created", IDS_COOKIES_COOKIE_CREATED_LABEL
},
60 {"label_cookie_expires", IDS_COOKIES_COOKIE_EXPIRES_LABEL
},
61 {"label_webdb_desc", IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL
},
62 {"label_local_storage_size",
63 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL
},
64 {"label_local_storage_last_modified",
65 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL
},
66 {"label_local_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
67 {"label_indexed_db_size", IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL
},
68 {"label_indexed_db_last_modified",
69 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL
},
70 {"label_indexed_db_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
71 {"label_service_worker_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
72 {"label_service_worker_size",
73 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL
},
74 {"label_service_worker_scopes", IDS_COOKIES_SERVICE_WORKER_SCOPES_LABEL
},
75 {"label_cache_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
76 {"label_cache_storage_size",
77 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL
},
78 {"label_cache_storage_last_modified",
79 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL
},
80 {"label_app_cache_manifest",
81 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL
},
82 {"label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL
},
83 {"cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER
},
84 {"cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER
},
85 {"cookie_singular", IDS_COOKIES_SINGLE_COOKIE
},
86 {"cookie_plural", IDS_COOKIES_PLURAL_COOKIES
},
87 {"cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE
},
88 {"cookie_indexed_db", IDS_COOKIES_INDEXED_DB
},
89 {"cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE
},
90 {"cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE
},
91 {"cookie_service_worker", IDS_COOKIES_SERVICE_WORKER
},
92 {"cookie_cache_storage", IDS_COOKIES_CACHE_STORAGE
},
93 {"cookie_flash_lso", IDS_COOKIES_FLASH_LSO
},
94 {"search_cookies", IDS_COOKIES_SEARCH_COOKIES
},
95 {"remove_cookie", IDS_COOKIES_REMOVE_LABEL
},
96 {"remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL
},
97 {"remove_all_shown_cookie", IDS_COOKIES_REMOVE_ALL_SHOWN_LABEL
},
98 {"cookie_file_system", IDS_COOKIES_FILE_SYSTEM
},
99 {"label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
100 {"label_file_system_temporary_usage",
101 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL
},
102 {"label_file_system_persistent_usage",
103 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL
},
104 {"cookie_channel_id", IDS_COOKIES_CHANNEL_ID
},
105 {"label_channel_id_server_id", IDS_COOKIES_CHANNEL_ID_ORIGIN_LABEL
},
106 {"label_channel_id_type", IDS_COOKIES_CHANNEL_ID_TYPE_LABEL
},
107 {"label_channel_id_created", IDS_COOKIES_CHANNEL_ID_CREATED_LABEL
},
108 {"label_channel_id_expires", IDS_COOKIES_CHANNEL_ID_EXPIRES_LABEL
},
109 {"label_protected_by_apps",
110 IDS_GEOLOCATION_SET_BY_HOVER
}, // TODO(bauerb): Use a better string
113 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
114 RegisterTitle(localized_strings
, "cookiesViewPage",
115 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE
);
118 void CookiesViewHandler::RegisterMessages() {
119 web_ui()->RegisterMessageCallback("updateCookieSearchResults",
120 base::Bind(&CookiesViewHandler::UpdateSearchResults
,
121 base::Unretained(this)));
122 web_ui()->RegisterMessageCallback("removeAllCookies",
123 base::Bind(&CookiesViewHandler::RemoveAll
,
124 base::Unretained(this)));
125 web_ui()->RegisterMessageCallback("removeCookie",
126 base::Bind(&CookiesViewHandler::Remove
,
127 base::Unretained(this)));
128 web_ui()->RegisterMessageCallback("loadCookie",
129 base::Bind(&CookiesViewHandler::LoadChildren
,
130 base::Unretained(this)));
131 web_ui()->RegisterMessageCallback("reloadCookies",
132 base::Bind(&CookiesViewHandler::ReloadCookies
,
133 base::Unretained(this)));
136 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel
* model
,
137 ui::TreeModelNode
* parent
,
140 // Skip if there is a batch update in progress.
144 CookiesTreeModel
* tree_model
= static_cast<CookiesTreeModel
*>(model
);
145 CookieTreeNode
* parent_node
= tree_model
->AsNode(parent
);
147 scoped_ptr
<base::ListValue
> children(new base::ListValue
);
148 model_util_
->GetChildNodeList(parent_node
, start
, count
, children
.get());
150 base::ListValue args
;
151 if (parent
== tree_model
->GetRoot())
152 args
.Append(base::Value::CreateNullValue());
154 args
.AppendString(model_util_
->GetTreeNodeId(parent_node
));
155 args
.AppendInteger(start
);
156 args
.Append(children
.Pass());
157 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args
);
160 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel
* model
,
161 ui::TreeModelNode
* parent
,
164 // Skip if there is a batch update in progress.
168 CookiesTreeModel
* tree_model
= static_cast<CookiesTreeModel
*>(model
);
170 base::ListValue args
;
171 if (parent
== tree_model
->GetRoot())
172 args
.Append(base::Value::CreateNullValue());
174 args
.AppendString(model_util_
->GetTreeNodeId(tree_model
->AsNode(parent
)));
175 args
.AppendInteger(start
);
176 args
.AppendInteger(count
);
177 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args
);
180 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel
* model
) {
181 DCHECK(!batch_update_
); // There should be no nested batch begin.
182 batch_update_
= true;
185 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel
* model
) {
186 DCHECK(batch_update_
);
187 batch_update_
= false;
189 SendChildren(model
->GetRoot());
192 void CookiesViewHandler::EnsureCookiesTreeModelCreated() {
193 if (!cookies_tree_model_
.get()) {
194 Profile
* profile
= Profile::FromWebUI(web_ui());
195 content::StoragePartition
* storage_partition
=
196 content::BrowserContext::GetDefaultStoragePartition(profile
);
197 content::IndexedDBContext
* indexed_db_context
=
198 storage_partition
->GetIndexedDBContext();
199 content::ServiceWorkerContext
* service_worker_context
=
200 storage_partition
->GetServiceWorkerContext();
201 content::CacheStorageContext
* cache_storage_context
=
202 storage_partition
->GetCacheStorageContext();
203 storage::FileSystemContext
* file_system_context
=
204 storage_partition
->GetFileSystemContext();
205 LocalDataContainer
* container
= new LocalDataContainer(
206 new BrowsingDataCookieHelper(profile
->GetRequestContext()),
207 new BrowsingDataDatabaseHelper(profile
),
208 new BrowsingDataLocalStorageHelper(profile
), NULL
,
209 new BrowsingDataAppCacheHelper(profile
),
210 new BrowsingDataIndexedDBHelper(indexed_db_context
),
211 BrowsingDataFileSystemHelper::Create(file_system_context
),
212 BrowsingDataQuotaHelper::Create(profile
),
213 BrowsingDataChannelIDHelper::Create(profile
->GetRequestContext()),
214 new BrowsingDataServiceWorkerHelper(service_worker_context
),
215 new BrowsingDataCacheStorageHelper(cache_storage_context
),
216 BrowsingDataFlashLSOHelper::Create(profile
));
217 cookies_tree_model_
.reset(
218 new CookiesTreeModel(container
,
219 profile
->GetExtensionSpecialStoragePolicy(),
221 cookies_tree_model_
->AddCookiesTreeObserver(this);
225 void CookiesViewHandler::UpdateSearchResults(const base::ListValue
* args
) {
226 base::string16 query
;
227 if (!args
->GetString(0, &query
))
230 EnsureCookiesTreeModelCreated();
232 cookies_tree_model_
->UpdateSearchResults(query
);
235 void CookiesViewHandler::RemoveAll(const base::ListValue
* args
) {
236 EnsureCookiesTreeModelCreated();
237 cookies_tree_model_
->DeleteAllStoredObjects();
240 void CookiesViewHandler::Remove(const base::ListValue
* args
) {
241 std::string node_path
;
242 if (!args
->GetString(0, &node_path
))
245 EnsureCookiesTreeModelCreated();
247 const CookieTreeNode
* node
= model_util_
->GetTreeNodeFromPath(
248 cookies_tree_model_
->GetRoot(), node_path
);
250 cookies_tree_model_
->DeleteCookieNode(const_cast<CookieTreeNode
*>(node
));
253 void CookiesViewHandler::LoadChildren(const base::ListValue
* args
) {
254 std::string node_path
;
255 if (!args
->GetString(0, &node_path
))
258 EnsureCookiesTreeModelCreated();
260 const CookieTreeNode
* node
= model_util_
->GetTreeNodeFromPath(
261 cookies_tree_model_
->GetRoot(), node_path
);
266 void CookiesViewHandler::SendChildren(const CookieTreeNode
* parent
) {
267 scoped_ptr
<base::ListValue
> children(new base::ListValue
);
268 model_util_
->GetChildNodeList(parent
, 0, parent
->child_count(),
271 base::ListValue args
;
272 if (parent
== cookies_tree_model_
->GetRoot())
273 args
.Append(base::Value::CreateNullValue());
275 args
.AppendString(model_util_
->GetTreeNodeId(parent
));
276 args
.Append(children
.Pass());
278 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args
);
281 void CookiesViewHandler::ReloadCookies(const base::ListValue
* args
) {
282 cookies_tree_model_
.reset();
284 EnsureCookiesTreeModelCreated();
287 } // namespace options