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/cookies_tree_model_util.h" | 5 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
7 #include "base/i18n/time_formatting.h" | 9 #include "base/i18n/time_formatting.h" |
| 10 #include "base/memory/scoped_ptr.h" |
8 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
9 #include "base/string_split.h" | 12 #include "base/string_split.h" |
10 #include "base/string_util.h" | 13 #include "base/string_util.h" |
11 #include "base/values.h" | 14 #include "base/values.h" |
12 #include "chrome/browser/cookies_tree_model.h" | 15 #include "chrome/browser/cookies_tree_model.h" |
13 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
14 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
15 #include "ui/base/text/bytes_formatting.h" | 18 #include "ui/base/text/bytes_formatting.h" |
16 | 19 |
17 namespace { | 20 namespace { |
18 | 21 |
19 static const char kKeyId[] = "id"; | 22 const char kKeyId[] = "id"; |
20 static const char kKeyTitle[] = "title"; | 23 const char kKeyTitle[] = "title"; |
21 static const char kKeyIcon[] = "icon"; | 24 const char kKeyIcon[] = "icon"; |
22 static const char kKeyType[] = "type"; | 25 const char kKeyType[] = "type"; |
23 static const char kKeyHasChildren[] = "hasChildren"; | 26 const char kKeyHasChildren[] = "hasChildren"; |
24 | 27 |
25 static const char kKeyName[] = "name"; | 28 const char kKeyName[] = "name"; |
26 static const char kKeyContent[] = "content"; | 29 const char kKeyContent[] = "content"; |
27 static const char kKeyDomain[] = "domain"; | 30 const char kKeyDomain[] = "domain"; |
28 static const char kKeyPath[] = "path"; | 31 const char kKeyPath[] = "path"; |
29 static const char kKeySendFor[] = "sendfor"; | 32 const char kKeySendFor[] = "sendfor"; |
30 static const char kKeyAccessibleToScript[] = "accessibleToScript"; | 33 const char kKeyAccessibleToScript[] = "accessibleToScript"; |
31 static const char kKeyDesc[] = "desc"; | 34 const char kKeyDesc[] = "desc"; |
32 static const char kKeySize[] = "size"; | 35 const char kKeySize[] = "size"; |
33 static const char kKeyOrigin[] = "origin"; | 36 const char kKeyOrigin[] = "origin"; |
34 static const char kKeyManifest[] = "manifest"; | 37 const char kKeyManifest[] = "manifest"; |
35 static const char kKeyServerId[] = "serverId"; | 38 const char kKeyServerId[] = "serverId"; |
36 | 39 |
37 static const char kKeyAccessed[] = "accessed"; | 40 const char kKeyAccessed[] = "accessed"; |
38 static const char kKeyCreated[] = "created"; | 41 const char kKeyCreated[] = "created"; |
39 static const char kKeyExpires[] = "expires"; | 42 const char kKeyExpires[] = "expires"; |
40 static const char kKeyModified[] = "modified"; | 43 const char kKeyModified[] = "modified"; |
41 | 44 |
42 static const char kKeyPersistent[] = "persistent"; | 45 const char kKeyPersistent[] = "persistent"; |
43 static const char kKeyTemporary[] = "temporary"; | 46 const char kKeyTemporary[] = "temporary"; |
44 | 47 |
45 static const char kKeyTotalUsage[] = "totalUsage"; | 48 const char kKeyTotalUsage[] = "totalUsage"; |
46 static const char kKeyTemporaryUsage[] = "temporaryUsage"; | 49 const char kKeyTemporaryUsage[] = "temporaryUsage"; |
47 static const char kKeyPersistentUsage[] = "persistentUsage"; | 50 const char kKeyPersistentUsage[] = "persistentUsage"; |
48 static const char kKeyPersistentQuota[] = "persistentQuota"; | 51 const char kKeyPersistentQuota[] = "persistentQuota"; |
49 | 52 |
50 static const char kKeyCertType[] = "certType"; | 53 const char kKeyCertType[] = "certType"; |
51 | 54 |
52 static const int64 kNegligibleUsage = 1024; // 1KiB | 55 const int64 kNegligibleUsage = 1024; // 1KiB |
53 | |
54 // Encodes a pointer value into a hex string. | |
55 std::string PointerToHexString(const void* pointer) { | |
56 return base::HexEncode(&pointer, sizeof(pointer)); | |
57 } | |
58 | |
59 // Decodes a pointer from a hex string. | |
60 void* HexStringToPointer(const std::string& str) { | |
61 std::vector<uint8> buffer; | |
62 if (!base::HexStringToBytes(str, &buffer) || | |
63 buffer.size() != sizeof(void*)) { | |
64 return NULL; | |
65 } | |
66 | |
67 return *reinterpret_cast<void**>(&buffer[0]); | |
68 } | |
69 | 56 |
70 std::string ClientCertTypeToString(net::SSLClientCertType type) { | 57 std::string ClientCertTypeToString(net::SSLClientCertType type) { |
71 switch (type) { | 58 switch (type) { |
72 case net::CLIENT_CERT_RSA_SIGN: | 59 case net::CLIENT_CERT_RSA_SIGN: |
73 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_RSA_SIGN); | 60 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_RSA_SIGN); |
74 case net::CLIENT_CERT_ECDSA_SIGN: | 61 case net::CLIENT_CERT_ECDSA_SIGN: |
75 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_ECDSA_SIGN); | 62 return l10n_util::GetStringUTF8(IDS_CLIENT_CERT_ECDSA_SIGN); |
76 default: | 63 default: |
77 return base::IntToString(type); | 64 return base::IntToString(type); |
78 } | 65 } |
79 } | 66 } |
80 | 67 |
81 } // namespace | 68 } // namespace |
82 | 69 |
83 namespace cookies_tree_model_util { | 70 CookiesTreeModelUtil::CookiesTreeModelUtil() { |
84 | |
85 std::string GetTreeNodeId(CookieTreeNode* node) { | |
86 return PointerToHexString(node); | |
87 } | 71 } |
88 | 72 |
89 bool GetCookieTreeNodeDictionary(const CookieTreeNode& node, | 73 CookiesTreeModelUtil::~CookiesTreeModelUtil() { |
90 DictionaryValue* dict) { | 74 } |
| 75 |
| 76 std::string CookiesTreeModelUtil::GetTreeNodeId(const CookieTreeNode* node) { |
| 77 CookieTreeNodeMap::const_iterator iter = node_map_.find(node); |
| 78 if (iter != node_map_.end()) |
| 79 return base::IntToString(iter->second); |
| 80 |
| 81 int32 new_id = id_map_.Add(node); |
| 82 node_map_[node] = new_id; |
| 83 return base::IntToString(new_id); |
| 84 } |
| 85 |
| 86 bool CookiesTreeModelUtil::GetCookieTreeNodeDictionary( |
| 87 const CookieTreeNode& node, |
| 88 base::DictionaryValue* dict) { |
91 // Use node's address as an id for WebUI to look it up. | 89 // Use node's address as an id for WebUI to look it up. |
92 dict->SetString(kKeyId, PointerToHexString(&node)); | 90 dict->SetString(kKeyId, GetTreeNodeId(&node)); |
93 dict->SetString(kKeyTitle, node.GetTitle()); | 91 dict->SetString(kKeyTitle, node.GetTitle()); |
94 dict->SetBoolean(kKeyHasChildren, !node.empty()); | 92 dict->SetBoolean(kKeyHasChildren, !node.empty()); |
95 | 93 |
96 switch (node.GetDetailedInfo().node_type) { | 94 switch (node.GetDetailedInfo().node_type) { |
97 case CookieTreeNode::DetailedInfo::TYPE_ORIGIN: { | 95 case CookieTreeNode::DetailedInfo::TYPE_ORIGIN: { |
98 dict->SetString(kKeyType, "origin"); | 96 dict->SetString(kKeyType, "origin"); |
99 #if defined(OS_MACOSX) | 97 #if defined(OS_MACOSX) |
100 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER"); | 98 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER"); |
101 #endif | 99 #endif |
102 break; | 100 break; |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 } | 252 } |
255 default: | 253 default: |
256 #if defined(OS_MACOSX) | 254 #if defined(OS_MACOSX) |
257 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER"); | 255 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER"); |
258 #endif | 256 #endif |
259 break; | 257 break; |
260 } | 258 } |
261 return true; | 259 return true; |
262 } | 260 } |
263 | 261 |
264 void GetChildNodeList(CookieTreeNode* parent, int start, int count, | 262 void CookiesTreeModelUtil::GetChildNodeList(const CookieTreeNode* parent, |
265 ListValue* nodes) { | 263 int start, |
| 264 int count, |
| 265 base::ListValue* nodes) { |
266 for (int i = 0; i < count; ++i) { | 266 for (int i = 0; i < count; ++i) { |
267 DictionaryValue* dict = new DictionaryValue; | 267 scoped_ptr<base::DictionaryValue> dict(new DictionaryValue); |
268 CookieTreeNode* child = parent->GetChild(start + i); | 268 const CookieTreeNode* child = parent->GetChild(start + i); |
269 if (GetCookieTreeNodeDictionary(*child, dict)) | 269 if (GetCookieTreeNodeDictionary(*child, dict.get())) |
270 nodes->Append(dict); | 270 nodes->Append(dict.release()); |
271 else | |
272 delete dict; | |
273 } | 271 } |
274 } | 272 } |
275 | 273 |
276 CookieTreeNode* GetTreeNodeFromPath(CookieTreeNode* root, | 274 const CookieTreeNode* CookiesTreeModelUtil::GetTreeNodeFromPath( |
277 const std::string& path) { | 275 const CookieTreeNode* root, |
| 276 const std::string& path) { |
278 std::vector<std::string> node_ids; | 277 std::vector<std::string> node_ids; |
279 base::SplitString(path, ',', &node_ids); | 278 base::SplitString(path, ',', &node_ids); |
280 | 279 |
281 CookieTreeNode* child = NULL; | 280 const CookieTreeNode* child = NULL; |
282 CookieTreeNode* parent = root; | 281 const CookieTreeNode* parent = root; |
283 int child_index = -1; | 282 int child_index = -1; |
284 | 283 |
285 // Validate the tree path and get the node pointer. | 284 // Validate the tree path and get the node pointer. |
286 for (size_t i = 0; i < node_ids.size(); ++i) { | 285 for (size_t i = 0; i < node_ids.size(); ++i) { |
287 child = reinterpret_cast<CookieTreeNode*>( | 286 int32 node_id = 0; |
288 HexStringToPointer(node_ids[i])); | 287 if (!base::StringToInt(node_ids[i], &node_id)) |
| 288 break; |
289 | 289 |
| 290 child = id_map_.Lookup(node_id); |
290 child_index = parent->GetIndexOf(child); | 291 child_index = parent->GetIndexOf(child); |
291 if (child_index == -1) | 292 if (child_index == -1) |
292 break; | 293 break; |
293 | 294 |
294 parent = child; | 295 parent = child; |
295 } | 296 } |
296 | 297 |
297 return child_index >= 0 ? child : NULL; | 298 return child_index >= 0 ? child : NULL; |
298 } | 299 } |
299 | |
300 } // namespace cookies_tree_model_util | |
OLD | NEW |