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_channel_id_helper.h"
15 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
16 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
17 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
20 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
21 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h"
22 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/webui/cookies_tree_model_util.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "content/public/browser/browser_context.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/browser/site_instance.h"
29 #include "content/public/browser/storage_partition.h"
30 #include "content/public/browser/web_ui.h"
33 class FileSystemContext
;
38 CookiesViewHandler::CookiesViewHandler()
39 : batch_update_(false),
40 model_util_(new CookiesTreeModelUtil
) {
43 CookiesViewHandler::~CookiesViewHandler() {
46 void CookiesViewHandler::GetLocalizedValues(
47 base::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_service_worker_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
71 { "label_service_worker_size",
72 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL
},
73 { "label_service_worker_scopes", IDS_COOKIES_SERVICE_WORKER_SCOPES_LABEL
},
74 { "label_app_cache_manifest",
75 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL
},
76 { "label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL
},
77 { "cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER
},
78 { "cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER
},
79 { "cookie_singular", IDS_COOKIES_SINGLE_COOKIE
},
80 { "cookie_plural", IDS_COOKIES_PLURAL_COOKIES
},
81 { "cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE
},
82 { "cookie_indexed_db", IDS_COOKIES_INDEXED_DB
},
83 { "cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE
},
84 { "cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE
},
85 { "cookie_service_worker", IDS_COOKIES_SERVICE_WORKER
},
86 { "cookie_flash_lso", IDS_COOKIES_FLASH_LSO
},
87 { "search_cookies", IDS_COOKIES_SEARCH_COOKIES
},
88 { "remove_cookie", IDS_COOKIES_REMOVE_LABEL
},
89 { "remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL
},
90 { "remove_all_shown_cookie", IDS_COOKIES_REMOVE_ALL_SHOWN_LABEL
},
91 { "cookie_file_system", IDS_COOKIES_FILE_SYSTEM
},
92 { "label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL
},
93 { "label_file_system_temporary_usage",
94 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL
},
95 { "label_file_system_persistent_usage",
96 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL
},
97 { "cookie_channel_id", IDS_COOKIES_CHANNEL_ID
},
98 { "label_channel_id_server_id",
99 IDS_COOKIES_CHANNEL_ID_ORIGIN_LABEL
},
100 { "label_channel_id_type",
101 IDS_COOKIES_CHANNEL_ID_TYPE_LABEL
},
102 { "label_channel_id_created",
103 IDS_COOKIES_CHANNEL_ID_CREATED_LABEL
},
104 { "label_channel_id_expires",
105 IDS_COOKIES_CHANNEL_ID_EXPIRES_LABEL
},
106 { "label_protected_by_apps",
107 IDS_GEOLOCATION_SET_BY_HOVER
}, // TODO(bauerb): Use a better string
110 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
111 RegisterTitle(localized_strings
, "cookiesViewPage",
112 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE
);
115 void CookiesViewHandler::RegisterMessages() {
116 web_ui()->RegisterMessageCallback("updateCookieSearchResults",
117 base::Bind(&CookiesViewHandler::UpdateSearchResults
,
118 base::Unretained(this)));
119 web_ui()->RegisterMessageCallback("removeAllCookies",
120 base::Bind(&CookiesViewHandler::RemoveAll
,
121 base::Unretained(this)));
122 web_ui()->RegisterMessageCallback("removeCookie",
123 base::Bind(&CookiesViewHandler::Remove
,
124 base::Unretained(this)));
125 web_ui()->RegisterMessageCallback("loadCookie",
126 base::Bind(&CookiesViewHandler::LoadChildren
,
127 base::Unretained(this)));
128 web_ui()->RegisterMessageCallback("reloadCookies",
129 base::Bind(&CookiesViewHandler::ReloadCookies
,
130 base::Unretained(this)));
133 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel
* model
,
134 ui::TreeModelNode
* parent
,
137 // Skip if there is a batch update in progress.
141 CookiesTreeModel
* tree_model
= static_cast<CookiesTreeModel
*>(model
);
142 CookieTreeNode
* parent_node
= tree_model
->AsNode(parent
);
144 scoped_ptr
<base::ListValue
> children(new base::ListValue
);
145 model_util_
->GetChildNodeList(parent_node
, start
, count
, children
.get());
147 base::ListValue args
;
148 if (parent
== tree_model
->GetRoot())
149 args
.Append(base::Value::CreateNullValue());
151 args
.AppendString(model_util_
->GetTreeNodeId(parent_node
));
152 args
.AppendInteger(start
);
153 args
.Append(children
.Pass());
154 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args
);
157 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel
* model
,
158 ui::TreeModelNode
* parent
,
161 // Skip if there is a batch update in progress.
165 CookiesTreeModel
* tree_model
= static_cast<CookiesTreeModel
*>(model
);
167 base::ListValue args
;
168 if (parent
== tree_model
->GetRoot())
169 args
.Append(base::Value::CreateNullValue());
171 args
.AppendString(model_util_
->GetTreeNodeId(tree_model
->AsNode(parent
)));
172 args
.AppendInteger(start
);
173 args
.AppendInteger(count
);
174 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args
);
177 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel
* model
) {
178 DCHECK(!batch_update_
); // There should be no nested batch begin.
179 batch_update_
= true;
182 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel
* model
) {
183 DCHECK(batch_update_
);
184 batch_update_
= false;
186 SendChildren(model
->GetRoot());
189 void CookiesViewHandler::EnsureCookiesTreeModelCreated() {
190 if (!cookies_tree_model_
.get()) {
191 Profile
* profile
= Profile::FromWebUI(web_ui());
192 content::StoragePartition
* storage_partition
=
193 content::BrowserContext::GetDefaultStoragePartition(profile
);
194 content::IndexedDBContext
* indexed_db_context
=
195 storage_partition
->GetIndexedDBContext();
196 content::ServiceWorkerContext
* service_worker_context
=
197 storage_partition
->GetServiceWorkerContext();
198 storage::FileSystemContext
* file_system_context
=
199 storage_partition
->GetFileSystemContext();
200 LocalDataContainer
* container
= new LocalDataContainer(
201 new BrowsingDataCookieHelper(profile
->GetRequestContext()),
202 new BrowsingDataDatabaseHelper(profile
),
203 new BrowsingDataLocalStorageHelper(profile
),
205 new BrowsingDataAppCacheHelper(profile
),
206 new BrowsingDataIndexedDBHelper(indexed_db_context
),
207 BrowsingDataFileSystemHelper::Create(file_system_context
),
208 BrowsingDataQuotaHelper::Create(profile
),
209 BrowsingDataChannelIDHelper::Create(profile
->GetRequestContext()),
210 new BrowsingDataServiceWorkerHelper(service_worker_context
),
211 BrowsingDataFlashLSOHelper::Create(profile
));
212 cookies_tree_model_
.reset(
213 new CookiesTreeModel(container
,
214 profile
->GetExtensionSpecialStoragePolicy(),
216 cookies_tree_model_
->AddCookiesTreeObserver(this);
220 void CookiesViewHandler::UpdateSearchResults(const base::ListValue
* args
) {
221 base::string16 query
;
222 if (!args
->GetString(0, &query
))
225 EnsureCookiesTreeModelCreated();
227 cookies_tree_model_
->UpdateSearchResults(query
);
230 void CookiesViewHandler::RemoveAll(const base::ListValue
* args
) {
231 EnsureCookiesTreeModelCreated();
232 cookies_tree_model_
->DeleteAllStoredObjects();
235 void CookiesViewHandler::Remove(const base::ListValue
* args
) {
236 std::string node_path
;
237 if (!args
->GetString(0, &node_path
))
240 EnsureCookiesTreeModelCreated();
242 const CookieTreeNode
* node
= model_util_
->GetTreeNodeFromPath(
243 cookies_tree_model_
->GetRoot(), node_path
);
245 cookies_tree_model_
->DeleteCookieNode(const_cast<CookieTreeNode
*>(node
));
248 void CookiesViewHandler::LoadChildren(const base::ListValue
* args
) {
249 std::string node_path
;
250 if (!args
->GetString(0, &node_path
))
253 EnsureCookiesTreeModelCreated();
255 const CookieTreeNode
* node
= model_util_
->GetTreeNodeFromPath(
256 cookies_tree_model_
->GetRoot(), node_path
);
261 void CookiesViewHandler::SendChildren(const CookieTreeNode
* parent
) {
262 scoped_ptr
<base::ListValue
> children(new base::ListValue
);
263 model_util_
->GetChildNodeList(parent
, 0, parent
->child_count(),
266 base::ListValue args
;
267 if (parent
== cookies_tree_model_
->GetRoot())
268 args
.Append(base::Value::CreateNullValue());
270 args
.AppendString(model_util_
->GetTreeNodeId(parent
));
271 args
.Append(children
.Pass());
273 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args
);
276 void CookiesViewHandler::ReloadCookies(const base::ListValue
* args
) {
277 cookies_tree_model_
.reset();
279 EnsureCookiesTreeModelCreated();
282 } // namespace options