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/cookies_tree_model_util.h"
9 #include "base/i18n/time_formatting.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/values.h"
16 #include "chrome/browser/browsing_data/cookies_tree_model.h"
17 #include "content/public/browser/indexed_db_context.h"
18 #include "grit/generated_resources.h"
19 #include "net/cookies/canonical_cookie.h"
20 #include "net/ssl/ssl_client_cert_type.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/text/bytes_formatting.h"
23 #include "webkit/common/fileapi/file_system_types.h"
27 const char kKeyId[] = "id";
28 const char kKeyTitle[] = "title";
29 const char kKeyIcon[] = "icon";
30 const char kKeyType[] = "type";
31 const char kKeyHasChildren[] = "hasChildren";
33 const char kKeyAppsProtectingThis[] = "appsProtectingThis";
34 const char kKeyName[] = "name";
35 const char kKeyContent[] = "content";
36 const char kKeyDomain[] = "domain";
37 const char kKeyPath[] = "path";
38 const char kKeySendFor[] = "sendfor";
39 const char kKeyAccessibleToScript[] = "accessibleToScript";
40 const char kKeyDesc[] = "desc";
41 const char kKeySize[] = "size";
42 const char kKeyOrigin[] = "origin";
43 const char kKeyManifest[] = "manifest";
44 const char kKeyServerId[] = "serverId";
46 const char kKeyAccessed[] = "accessed";
47 const char kKeyCreated[] = "created";
48 const char kKeyExpires[] = "expires";
49 const char kKeyModified[] = "modified";
51 const char kKeyPersistent[] = "persistent";
52 const char kKeyTemporary[] = "temporary";
54 const char kKeyTotalUsage[] = "totalUsage";
55 const char kKeyTemporaryUsage[] = "temporaryUsage";
56 const char kKeyPersistentUsage[] = "persistentUsage";
58 const char kKeyCertType[] = "certType";
60 const int64 kNegligibleUsage = 1024; // 1KiB
62 std::string ClientCertTypeToString(net::SSLClientCertType type) {
64 case net::CLIENT_CERT_RSA_SIGN:
65 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_RSA_SIGN);
66 case net::CLIENT_CERT_DSS_SIGN:
67 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_DSS_SIGN);
68 case net::CLIENT_CERT_ECDSA_SIGN:
69 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_ECDSA_SIGN);
71 return base::IntToString(type);
77 CookiesTreeModelUtil::CookiesTreeModelUtil() {
80 CookiesTreeModelUtil::~CookiesTreeModelUtil() {
83 std::string CookiesTreeModelUtil::GetTreeNodeId(const CookieTreeNode* node) {
84 CookieTreeNodeMap::const_iterator iter = node_map_.find(node);
85 if (iter != node_map_.end())
86 return base::IntToString(iter->second);
88 int32 new_id = id_map_.Add(node);
89 node_map_[node] = new_id;
90 return base::IntToString(new_id);
93 bool CookiesTreeModelUtil::GetCookieTreeNodeDictionary(
94 const CookieTreeNode& node,
95 base::DictionaryValue* dict) {
96 // Use node's address as an id for WebUI to look it up.
97 dict->SetString(kKeyId, GetTreeNodeId(&node));
98 dict->SetString(kKeyTitle, node.GetTitle());
99 dict->SetBoolean(kKeyHasChildren, !node.empty());
101 switch (node.GetDetailedInfo().node_type) {
102 case CookieTreeNode::DetailedInfo::TYPE_HOST: {
103 dict->SetString(kKeyType, "origin");
104 #if defined(OS_MACOSX)
105 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER");
109 case CookieTreeNode::DetailedInfo::TYPE_COOKIE: {
110 dict->SetString(kKeyType, "cookie");
111 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_ICON");
113 const net::CanonicalCookie& cookie = *node.GetDetailedInfo().cookie;
115 dict->SetString(kKeyName, cookie.Name());
116 dict->SetString(kKeyContent, cookie.Value());
117 dict->SetString(kKeyDomain, cookie.Domain());
118 dict->SetString(kKeyPath, cookie.Path());
119 dict->SetString(kKeySendFor, cookie.IsSecure() ?
120 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_SENDFOR_SECURE) :
121 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_SENDFOR_ANY));
122 std::string accessible = cookie.IsHttpOnly() ?
123 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_NO) :
124 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_YES);
125 dict->SetString(kKeyAccessibleToScript, accessible);
126 dict->SetString(kKeyCreated, UTF16ToUTF8(
127 base::TimeFormatFriendlyDateAndTime(cookie.CreationDate())));
128 dict->SetString(kKeyExpires, cookie.IsPersistent() ? UTF16ToUTF8(
129 base::TimeFormatFriendlyDateAndTime(cookie.ExpiryDate())) :
130 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_EXPIRES_SESSION));
134 case CookieTreeNode::DetailedInfo::TYPE_DATABASE: {
135 dict->SetString(kKeyType, "database");
136 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
138 const BrowsingDataDatabaseHelper::DatabaseInfo& database_info =
139 *node.GetDetailedInfo().database_info;
141 dict->SetString(kKeyName, database_info.database_name.empty() ?
142 l10n_util::GetStringUTF8(IDS_COOKIES_WEB_DATABASE_UNNAMED_NAME) :
143 database_info.database_name);
144 dict->SetString(kKeyDesc, database_info.description);
145 dict->SetString(kKeySize, ui::FormatBytes(database_info.size));
146 dict->SetString(kKeyModified, UTF16ToUTF8(
147 base::TimeFormatFriendlyDateAndTime(database_info.last_modified)));
151 case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE: {
152 dict->SetString(kKeyType, "local_storage");
153 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
155 const BrowsingDataLocalStorageHelper::LocalStorageInfo&
156 local_storage_info = *node.GetDetailedInfo().local_storage_info;
158 dict->SetString(kKeyOrigin, local_storage_info.origin_url.spec());
159 dict->SetString(kKeySize, ui::FormatBytes(local_storage_info.size));
160 dict->SetString(kKeyModified, UTF16ToUTF8(
161 base::TimeFormatFriendlyDateAndTime(
162 local_storage_info.last_modified)));
166 case CookieTreeNode::DetailedInfo::TYPE_APPCACHE: {
167 dict->SetString(kKeyType, "app_cache");
168 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
170 const appcache::AppCacheInfo& appcache_info =
171 *node.GetDetailedInfo().appcache_info;
173 dict->SetString(kKeyManifest, appcache_info.manifest_url.spec());
174 dict->SetString(kKeySize, ui::FormatBytes(appcache_info.size));
175 dict->SetString(kKeyCreated, UTF16ToUTF8(
176 base::TimeFormatFriendlyDateAndTime(appcache_info.creation_time)));
177 dict->SetString(kKeyAccessed, UTF16ToUTF8(
178 base::TimeFormatFriendlyDateAndTime(appcache_info.last_access_time)));
182 case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB: {
183 dict->SetString(kKeyType, "indexed_db");
184 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
186 const content::IndexedDBInfo& indexed_db_info =
187 *node.GetDetailedInfo().indexed_db_info;
189 dict->SetString(kKeyOrigin, indexed_db_info.origin_.spec());
190 dict->SetString(kKeySize, ui::FormatBytes(indexed_db_info.size_));
191 dict->SetString(kKeyModified, UTF16ToUTF8(
192 base::TimeFormatFriendlyDateAndTime(indexed_db_info.last_modified_)));
196 case CookieTreeNode::DetailedInfo::TYPE_FILE_SYSTEM: {
197 dict->SetString(kKeyType, "file_system");
198 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
200 const BrowsingDataFileSystemHelper::FileSystemInfo& file_system_info =
201 *node.GetDetailedInfo().file_system_info;
202 const fileapi::FileSystemType kPerm = fileapi::kFileSystemTypePersistent;
203 const fileapi::FileSystemType kTemp = fileapi::kFileSystemTypeTemporary;
205 dict->SetString(kKeyOrigin, file_system_info.origin.spec());
206 dict->SetString(kKeyPersistent,
207 ContainsKey(file_system_info.usage_map, kPerm) ?
208 UTF16ToUTF8(ui::FormatBytes(
209 file_system_info.usage_map.find(kPerm)->second)) :
210 l10n_util::GetStringUTF8(
211 IDS_COOKIES_FILE_SYSTEM_USAGE_NONE));
212 dict->SetString(kKeyTemporary,
213 ContainsKey(file_system_info.usage_map, kTemp) ?
214 UTF16ToUTF8(ui::FormatBytes(
215 file_system_info.usage_map.find(kTemp)->second)) :
216 l10n_util::GetStringUTF8(
217 IDS_COOKIES_FILE_SYSTEM_USAGE_NONE));
220 case CookieTreeNode::DetailedInfo::TYPE_QUOTA: {
221 dict->SetString(kKeyType, "quota");
222 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
224 const BrowsingDataQuotaHelper::QuotaInfo& quota_info =
225 *node.GetDetailedInfo().quota_info;
226 if (quota_info.temporary_usage + quota_info.persistent_usage <=
230 dict->SetString(kKeyOrigin, quota_info.host);
231 dict->SetString(kKeyTotalUsage,
232 UTF16ToUTF8(ui::FormatBytes(
233 quota_info.temporary_usage +
234 quota_info.persistent_usage)));
235 dict->SetString(kKeyTemporaryUsage,
236 UTF16ToUTF8(ui::FormatBytes(
237 quota_info.temporary_usage)));
238 dict->SetString(kKeyPersistentUsage,
239 UTF16ToUTF8(ui::FormatBytes(
240 quota_info.persistent_usage)));
243 case CookieTreeNode::DetailedInfo::TYPE_SERVER_BOUND_CERT: {
244 dict->SetString(kKeyType, "server_bound_cert");
245 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_ICON");
247 const net::ServerBoundCertStore::ServerBoundCert& server_bound_cert =
248 *node.GetDetailedInfo().server_bound_cert;
250 dict->SetString(kKeyServerId, server_bound_cert.server_identifier());
251 dict->SetString(kKeyCertType,
252 ClientCertTypeToString(net::CLIENT_CERT_ECDSA_SIGN));
253 dict->SetString(kKeyCreated, UTF16ToUTF8(
254 base::TimeFormatFriendlyDateAndTime(
255 server_bound_cert.creation_time())));
258 case CookieTreeNode::DetailedInfo::TYPE_FLASH_LSO: {
259 dict->SetString(kKeyType, "flash_lso");
260 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_ICON");
262 dict->SetString(kKeyDomain, node.GetDetailedInfo().flash_lso_domain);
265 #if defined(OS_MACOSX)
266 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER");
271 const ExtensionSet* protecting_apps =
272 node.GetModel()->ExtensionsProtectingNode(node);
273 if (protecting_apps && !protecting_apps->is_empty()) {
274 base::ListValue* app_infos = new base::ListValue;
275 for (ExtensionSet::const_iterator it = protecting_apps->begin();
276 it != protecting_apps->end(); ++it) {
277 base::DictionaryValue* app_info = new base::DictionaryValue();
278 app_info->SetString(kKeyId, (*it)->id());
279 app_info->SetString(kKeyName, (*it)->name());
280 app_infos->Append(app_info);
282 dict->Set(kKeyAppsProtectingThis, app_infos);
288 void CookiesTreeModelUtil::GetChildNodeList(const CookieTreeNode* parent,
291 base::ListValue* nodes) {
292 for (int i = 0; i < count; ++i) {
293 scoped_ptr<base::DictionaryValue> dict(new DictionaryValue);
294 const CookieTreeNode* child = parent->GetChild(start + i);
295 if (GetCookieTreeNodeDictionary(*child, dict.get()))
296 nodes->Append(dict.release());
300 const CookieTreeNode* CookiesTreeModelUtil::GetTreeNodeFromPath(
301 const CookieTreeNode* root,
302 const std::string& path) {
303 std::vector<std::string> node_ids;
304 base::SplitString(path, ',', &node_ids);
306 const CookieTreeNode* child = NULL;
307 const CookieTreeNode* parent = root;
308 int child_index = -1;
310 // Validate the tree path and get the node pointer.
311 for (size_t i = 0; i < node_ids.size(); ++i) {
313 if (!base::StringToInt(node_ids[i], &node_id))
316 child = id_map_.Lookup(node_id);
317 child_index = parent->GetIndexOf(child);
318 if (child_index == -1)
324 return child_index >= 0 ? child : NULL;