Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: chrome/browser/ui/webui/options/cookies_view_handler.cc

Issue 6611030: Move cookies tree model helper code into cookies_tree_model_util. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix typo Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/options/cookies_view_handler.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/options/cookies_view_handler.h" 5 #include "chrome/browser/ui/webui/options/cookies_view_handler.h"
6 6
7 #include "base/i18n/time_formatting.h"
8 #include "base/string_number_conversions.h"
9 #include "base/string_split.h"
10 #include "base/string_util.h"
11 #include "base/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
12 #include "base/values.h" 8 #include "base/values.h"
13 #include "chrome/browser/browsing_data_appcache_helper.h" 9 #include "chrome/browser/browsing_data_appcache_helper.h"
14 #include "chrome/browser/browsing_data_database_helper.h" 10 #include "chrome/browser/browsing_data_database_helper.h"
15 #include "chrome/browser/browsing_data_indexed_db_helper.h" 11 #include "chrome/browser/browsing_data_indexed_db_helper.h"
16 #include "chrome/browser/browsing_data_local_storage_helper.h" 12 #include "chrome/browser/browsing_data_local_storage_helper.h"
17 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/webui/cookies_tree_model_util.h"
18 #include "grit/generated_resources.h" 15 #include "grit/generated_resources.h"
19 #include "net/base/cookie_monster.h"
20 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/base/l10n/l10n_util.h"
21 17
22 namespace { 18 namespace {
23 19
24 static const char kKeyId[] = "id";
25 static const char kKeyTitle[] = "title";
26 static const char kKeyIcon[] = "icon";
27 static const char kKeyType[] = "type";
28 static const char kKeyHasChildren[] = "hasChildren";
29
30 static const char kKeyName[] = "name";
31 static const char kKeyContent[] = "content";
32 static const char kKeyDomain[] = "domain";
33 static const char kKeyPath[] = "path";
34 static const char kKeySendFor[] = "sendfor";
35 static const char kKeyAccessibleToScript[] = "accessibleToScript";
36 static const char kKeyDesc[] = "desc";
37 static const char kKeySize[] = "size";
38 static const char kKeyOrigin[] = "origin";
39 static const char kKeyManifest[] = "manifest";
40
41 static const char kKeyAccessed[] = "accessed";
42 static const char kKeyCreated[] = "created";
43 static const char kKeyExpires[] = "expires";
44 static const char kKeyModified[] = "modified";
45
46 // Encodes a pointer value into a hex string.
47 std::string PointerToHexString(const void* pointer) {
48 return base::HexEncode(&pointer, sizeof(pointer));
49 }
50
51 // Decodes a pointer from a hex string.
52 void* HexStringToPointer(const std::string& str) {
53 std::vector<uint8> buffer;
54 if (!base::HexStringToBytes(str, &buffer) ||
55 buffer.size() != sizeof(void*)) {
56 return NULL;
57 }
58
59 return *reinterpret_cast<void**>(&buffer[0]);
60 }
61
62 // Populate given |dict| with cookie tree node properties.
63 void GetCookieTreeNodeDictionary(const CookieTreeNode& node,
64 DictionaryValue* dict) {
65 // Use node's address as an id for WebUI to look it up.
66 dict->SetString(kKeyId, PointerToHexString(&node));
67 dict->SetString(kKeyTitle, node.GetTitle());
68 dict->SetBoolean(kKeyHasChildren, !!node.GetChildCount());
69
70 switch (node.GetDetailedInfo().node_type) {
71 case CookieTreeNode::DetailedInfo::TYPE_ORIGIN: {
72 dict->SetString(kKeyType, "origin");
73 #if defined(OS_MACOSX)
74 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER");
75 #endif
76 break;
77 }
78 case CookieTreeNode::DetailedInfo::TYPE_COOKIE: {
79 dict->SetString(kKeyType, "cookie");
80 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_ICON");
81
82 const net::CookieMonster::CanonicalCookie& cookie =
83 *node.GetDetailedInfo().cookie;
84
85 dict->SetString(kKeyName, cookie.Name());
86 dict->SetString(kKeyContent, cookie.Value());
87 dict->SetString(kKeyDomain, cookie.Domain());
88 dict->SetString(kKeyPath, cookie.Path());
89 dict->SetString(kKeySendFor, cookie.IsSecure() ?
90 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_SENDFOR_SECURE) :
91 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_SENDFOR_ANY));
92 std::string accessible = cookie.IsHttpOnly() ?
93 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_NO) :
94 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_YES);
95 dict->SetString(kKeyAccessibleToScript, accessible);
96 dict->SetString(kKeyCreated, UTF16ToUTF8(
97 base::TimeFormatFriendlyDateAndTime(cookie.CreationDate())));
98 dict->SetString(kKeyExpires, cookie.DoesExpire() ? UTF16ToUTF8(
99 base::TimeFormatFriendlyDateAndTime(cookie.ExpiryDate())) :
100 l10n_util::GetStringUTF8(IDS_COOKIES_COOKIE_EXPIRES_SESSION));
101
102 break;
103 }
104 case CookieTreeNode::DetailedInfo::TYPE_DATABASE: {
105 dict->SetString(kKeyType, "database");
106 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
107
108 const BrowsingDataDatabaseHelper::DatabaseInfo& database_info =
109 *node.GetDetailedInfo().database_info;
110
111 dict->SetString(kKeyName, database_info.database_name.empty() ?
112 l10n_util::GetStringUTF8(IDS_COOKIES_WEB_DATABASE_UNNAMED_NAME) :
113 database_info.database_name);
114 dict->SetString(kKeyDesc, database_info.description);
115 dict->SetString(kKeySize,
116 FormatBytes(database_info.size,
117 GetByteDisplayUnits(database_info.size),
118 true));
119 dict->SetString(kKeyModified, UTF16ToUTF8(
120 base::TimeFormatFriendlyDateAndTime(database_info.last_modified)));
121
122 break;
123 }
124 case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE: {
125 dict->SetString(kKeyType, "local_storage");
126 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
127
128 const BrowsingDataLocalStorageHelper::LocalStorageInfo&
129 local_storage_info = *node.GetDetailedInfo().local_storage_info;
130
131 dict->SetString(kKeyOrigin, local_storage_info.origin);
132 dict->SetString(kKeySize,
133 FormatBytes(local_storage_info.size,
134 GetByteDisplayUnits(local_storage_info.size),
135 true));
136 dict->SetString(kKeyModified, UTF16ToUTF8(
137 base::TimeFormatFriendlyDateAndTime(
138 local_storage_info.last_modified)));
139
140 break;
141 }
142 case CookieTreeNode::DetailedInfo::TYPE_APPCACHE: {
143 dict->SetString(kKeyType, "app_cache");
144 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
145
146 const appcache::AppCacheInfo& appcache_info =
147 *node.GetDetailedInfo().appcache_info;
148
149 dict->SetString(kKeyManifest, appcache_info.manifest_url.spec());
150 dict->SetString(kKeySize,
151 FormatBytes(appcache_info.size,
152 GetByteDisplayUnits(appcache_info.size),
153 true));
154 dict->SetString(kKeyCreated, UTF16ToUTF8(
155 base::TimeFormatFriendlyDateAndTime(appcache_info.creation_time)));
156 dict->SetString(kKeyAccessed, UTF16ToUTF8(
157 base::TimeFormatFriendlyDateAndTime(appcache_info.last_access_time)));
158
159 break;
160 }
161 case CookieTreeNode::DetailedInfo::TYPE_INDEXED_DB: {
162 dict->SetString(kKeyType, "indexed_db");
163 dict->SetString(kKeyIcon, "chrome://theme/IDR_COOKIE_STORAGE_ICON");
164
165 const BrowsingDataIndexedDBHelper::IndexedDBInfo& indexed_db_info =
166 *node.GetDetailedInfo().indexed_db_info;
167
168 dict->SetString(kKeyOrigin, indexed_db_info.origin);
169 dict->SetString(kKeySize,
170 FormatBytes(indexed_db_info.size,
171 GetByteDisplayUnits(indexed_db_info.size),
172 true));
173 dict->SetString(kKeyModified, UTF16ToUTF8(
174 base::TimeFormatFriendlyDateAndTime(indexed_db_info.last_modified)));
175
176 break;
177 }
178 default:
179 #if defined(OS_MACOSX)
180 dict->SetString(kKeyIcon, "chrome://theme/IDR_BOOKMARK_BAR_FOLDER");
181 #endif
182 break;
183 }
184 }
185
186 // Append the children nodes of |parent| in specified range to |nodes| list.
187 void GetChildNodeList(CookieTreeNode* parent, int start, int count,
188 ListValue* nodes) {
189 for (int i = 0; i < count; ++i) {
190 DictionaryValue* dict = new DictionaryValue;
191 CookieTreeNode* child = parent->GetChild(start + i);
192 GetCookieTreeNodeDictionary(*child, dict);
193 nodes->Append(dict);
194 }
195 }
196
197 // TODO(xiyuan): Remove this function when strings are updated. 20 // TODO(xiyuan): Remove this function when strings are updated.
198 // Remove "&" in button label for WebUI. 21 // Remove "&" in button label for WebUI.
199 string16 CleanButtonLabel(const string16& text) { 22 string16 CleanButtonLabel(const string16& text) {
200 string16 out(text); 23 string16 out(text);
201 ReplaceFirstSubstringAfterOffset(&out, 0, ASCIIToUTF16("&"), string16()); 24 ReplaceFirstSubstringAfterOffset(&out, 0, ASCIIToUTF16("&"), string16());
202 return out; 25 return out;
203 } 26 }
204 27
205 } // namespace 28 } // namespace
206 29
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 } 94 }
272 95
273 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, 96 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model,
274 ui::TreeModelNode* parent, 97 ui::TreeModelNode* parent,
275 int start, 98 int start,
276 int count) { 99 int count) {
277 // Skip if there is a batch update in progress. 100 // Skip if there is a batch update in progress.
278 if (batch_update_) 101 if (batch_update_)
279 return; 102 return;
280 103
104 CookieTreeNode* parent_node = cookies_tree_model_->AsNode(parent);
105
281 ListValue* children = new ListValue; 106 ListValue* children = new ListValue;
282 GetChildNodeList(cookies_tree_model_->AsNode(parent), start, count, children); 107 cookies_tree_model_util::GetChildNodeList(parent_node, start, count,
108 children);
283 109
284 ListValue args; 110 ListValue args;
285 args.Append(parent == cookies_tree_model_->GetRoot() ? 111 args.Append(parent == cookies_tree_model_->GetRoot() ?
286 Value::CreateNullValue() : 112 Value::CreateNullValue() :
287 Value::CreateStringValue(PointerToHexString(parent))); 113 Value::CreateStringValue(
114 cookies_tree_model_util::GetTreeNodeId(parent_node)));
288 args.Append(Value::CreateIntegerValue(start)); 115 args.Append(Value::CreateIntegerValue(start));
289 args.Append(children); 116 args.Append(children);
290 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemAdded", args); 117 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemAdded", args);
291 } 118 }
292 119
293 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, 120 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model,
294 ui::TreeModelNode* parent, 121 ui::TreeModelNode* parent,
295 int start, 122 int start,
296 int count) { 123 int count) {
297 // Skip if there is a batch update in progress. 124 // Skip if there is a batch update in progress.
298 if (batch_update_) 125 if (batch_update_)
299 return; 126 return;
300 127
301 ListValue args; 128 ListValue args;
302 args.Append(parent == cookies_tree_model_->GetRoot() ? 129 args.Append(parent == cookies_tree_model_->GetRoot() ?
303 Value::CreateNullValue() : 130 Value::CreateNullValue() :
304 Value::CreateStringValue(PointerToHexString(parent))); 131 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(
132 cookies_tree_model_->AsNode(parent))));
305 args.Append(Value::CreateIntegerValue(start)); 133 args.Append(Value::CreateIntegerValue(start));
306 args.Append(Value::CreateIntegerValue(count)); 134 args.Append(Value::CreateIntegerValue(count));
307 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemRemoved", args); 135 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemRemoved", args);
308 } 136 }
309 137
310 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { 138 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) {
311 DCHECK(!batch_update_); // There should be no nested batch begin. 139 DCHECK(!batch_update_); // There should be no nested batch begin.
312 batch_update_ = true; 140 batch_update_ = true;
313 } 141 }
314 142
(...skipping 28 matching lines...) Expand all
343 void CookiesViewHandler::RemoveAll(const ListValue* args) { 171 void CookiesViewHandler::RemoveAll(const ListValue* args) {
344 cookies_tree_model_->DeleteAllStoredObjects(); 172 cookies_tree_model_->DeleteAllStoredObjects();
345 } 173 }
346 174
347 void CookiesViewHandler::Remove(const ListValue* args) { 175 void CookiesViewHandler::Remove(const ListValue* args) {
348 std::string node_path; 176 std::string node_path;
349 if (!args->GetString(0, &node_path)){ 177 if (!args->GetString(0, &node_path)){
350 return; 178 return;
351 } 179 }
352 180
353 CookieTreeNode* node = GetTreeNodeFromPath(node_path); 181 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath(
182 cookies_tree_model_->GetRoot(), node_path);
354 if (node) 183 if (node)
355 cookies_tree_model_->DeleteCookieNode(node); 184 cookies_tree_model_->DeleteCookieNode(node);
356 } 185 }
357 186
358 void CookiesViewHandler::LoadChildren(const ListValue* args) { 187 void CookiesViewHandler::LoadChildren(const ListValue* args) {
359 std::string node_path; 188 std::string node_path;
360 if (!args->GetString(0, &node_path)){ 189 if (!args->GetString(0, &node_path)){
361 return; 190 return;
362 } 191 }
363 192
364 CookieTreeNode* node = GetTreeNodeFromPath(node_path); 193 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath(
194 cookies_tree_model_->GetRoot(), node_path);
365 if (node) 195 if (node)
366 SendChildren(node); 196 SendChildren(node);
367 } 197 }
368 198
369 CookieTreeNode* CookiesViewHandler::GetTreeNodeFromPath(
370 const std::string& path) {
371 std::vector<std::string> node_ids;
372 base::SplitString(path, ',', &node_ids);
373
374 CookieTreeNode* child = NULL;
375 CookieTreeNode* parent = cookies_tree_model_->GetRoot();
376 int child_index = -1;
377
378 // Validate the tree path and get the node pointer.
379 for (size_t i = 0; i < node_ids.size(); ++i) {
380 child = reinterpret_cast<CookieTreeNode*>(
381 HexStringToPointer(node_ids[i]));
382
383 child_index = parent->IndexOfChild(child);
384 if (child_index == -1)
385 break;
386
387 parent = child;
388 }
389
390 return child_index >= 0 ? child : NULL;
391 }
392
393 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) { 199 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) {
394 ListValue* children = new ListValue; 200 ListValue* children = new ListValue;
395 GetChildNodeList(parent, 0, parent->GetChildCount(), children); 201 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->GetChildCount(),
202 children);
396 203
397 ListValue args; 204 ListValue args;
398 args.Append(parent == cookies_tree_model_->GetRoot() ? 205 args.Append(parent == cookies_tree_model_->GetRoot() ?
399 Value::CreateNullValue() : 206 Value::CreateNullValue() :
400 Value::CreateStringValue(PointerToHexString(parent))); 207 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent)));
401 args.Append(children); 208 args.Append(children);
402 209
403 web_ui_->CallJavascriptFunction(L"CookiesView.loadChildren", args); 210 web_ui_->CallJavascriptFunction(L"CookiesView.loadChildren", args);
404 } 211 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/cookies_view_handler.h ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698