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

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: 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 } 110 }
288 111
289 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, 112 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model,
290 ui::TreeModelNode* parent, 113 ui::TreeModelNode* parent,
291 int start, 114 int start,
292 int count) { 115 int count) {
293 // Skip if there is a batch update in progress. 116 // Skip if there is a batch update in progress.
294 if (batch_update_) 117 if (batch_update_)
295 return; 118 return;
296 119
120 CookieTreeNode* parend_node = cookies_tree_model_->AsNode(parent);
Mike Mammarella 2011/03/04 01:05:12 parent_node?
xiyuan 2011/03/04 17:00:44 Oops. Fixed. :p
121
297 ListValue* children = new ListValue; 122 ListValue* children = new ListValue;
298 GetChildNodeList(cookies_tree_model_->AsNode(parent), start, count, children); 123 cookies_tree_model_util::GetChildNodeList(parend_node, start, count,
124 children);
299 125
300 ListValue args; 126 ListValue args;
301 args.Append(parent == cookies_tree_model_->GetRoot() ? 127 args.Append(parent == cookies_tree_model_->GetRoot() ?
302 Value::CreateNullValue() : 128 Value::CreateNullValue() :
303 Value::CreateStringValue(PointerToHexString(parent))); 129 Value::CreateStringValue(
130 cookies_tree_model_util::GetTreeNodeId(parend_node)));
304 args.Append(Value::CreateIntegerValue(start)); 131 args.Append(Value::CreateIntegerValue(start));
305 args.Append(children); 132 args.Append(children);
306 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemAdded", args); 133 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemAdded", args);
307 } 134 }
308 135
309 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, 136 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model,
310 ui::TreeModelNode* parent, 137 ui::TreeModelNode* parent,
311 int start, 138 int start,
312 int count) { 139 int count) {
313 // Skip if there is a batch update in progress. 140 // Skip if there is a batch update in progress.
314 if (batch_update_) 141 if (batch_update_)
315 return; 142 return;
316 143
317 ListValue args; 144 ListValue args;
318 args.Append(parent == cookies_tree_model_->GetRoot() ? 145 args.Append(parent == cookies_tree_model_->GetRoot() ?
319 Value::CreateNullValue() : 146 Value::CreateNullValue() :
320 Value::CreateStringValue(PointerToHexString(parent))); 147 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(
148 cookies_tree_model_->AsNode(parent))));
321 args.Append(Value::CreateIntegerValue(start)); 149 args.Append(Value::CreateIntegerValue(start));
322 args.Append(Value::CreateIntegerValue(count)); 150 args.Append(Value::CreateIntegerValue(count));
323 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemRemoved", args); 151 web_ui_->CallJavascriptFunction(L"CookiesView.onTreeItemRemoved", args);
324 } 152 }
325 153
326 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { 154 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) {
327 DCHECK(!batch_update_); // There should be no nested batch begin. 155 DCHECK(!batch_update_); // There should be no nested batch begin.
328 batch_update_ = true; 156 batch_update_ = true;
329 } 157 }
330 158
(...skipping 28 matching lines...) Expand all
359 void CookiesViewHandler::RemoveAll(const ListValue* args) { 187 void CookiesViewHandler::RemoveAll(const ListValue* args) {
360 cookies_tree_model_->DeleteAllStoredObjects(); 188 cookies_tree_model_->DeleteAllStoredObjects();
361 } 189 }
362 190
363 void CookiesViewHandler::Remove(const ListValue* args) { 191 void CookiesViewHandler::Remove(const ListValue* args) {
364 std::string node_path; 192 std::string node_path;
365 if (!args->GetString(0, &node_path)){ 193 if (!args->GetString(0, &node_path)){
366 return; 194 return;
367 } 195 }
368 196
369 CookieTreeNode* node = GetTreeNodeFromPath(node_path); 197 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath(
198 cookies_tree_model_->GetRoot(), node_path);
370 if (node) 199 if (node)
371 cookies_tree_model_->DeleteCookieNode(node); 200 cookies_tree_model_->DeleteCookieNode(node);
372 } 201 }
373 202
374 void CookiesViewHandler::LoadChildren(const ListValue* args) { 203 void CookiesViewHandler::LoadChildren(const ListValue* args) {
375 std::string node_path; 204 std::string node_path;
376 if (!args->GetString(0, &node_path)){ 205 if (!args->GetString(0, &node_path)){
377 return; 206 return;
378 } 207 }
379 208
380 CookieTreeNode* node = GetTreeNodeFromPath(node_path); 209 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath(
210 cookies_tree_model_->GetRoot(), node_path);
381 if (node) 211 if (node)
382 SendChildren(node); 212 SendChildren(node);
383 } 213 }
384 214
385 CookieTreeNode* CookiesViewHandler::GetTreeNodeFromPath(
386 const std::string& path) {
387 std::vector<std::string> node_ids;
388 base::SplitString(path, ',', &node_ids);
389
390 CookieTreeNode* child = NULL;
391 CookieTreeNode* parent = cookies_tree_model_->GetRoot();
392 int child_index = -1;
393
394 // Validate the tree path and get the node pointer.
395 for (size_t i = 0; i < node_ids.size(); ++i) {
396 child = reinterpret_cast<CookieTreeNode*>(
397 HexStringToPointer(node_ids[i]));
398
399 child_index = parent->IndexOfChild(child);
400 if (child_index == -1)
401 break;
402
403 parent = child;
404 }
405
406 return child_index >= 0 ? child : NULL;
407 }
408
409 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) { 215 void CookiesViewHandler::SendChildren(CookieTreeNode* parent) {
410 ListValue* children = new ListValue; 216 ListValue* children = new ListValue;
411 GetChildNodeList(parent, 0, parent->GetChildCount(), children); 217 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->GetChildCount(),
218 children);
412 219
413 ListValue args; 220 ListValue args;
414 args.Append(parent == cookies_tree_model_->GetRoot() ? 221 args.Append(parent == cookies_tree_model_->GetRoot() ?
415 Value::CreateNullValue() : 222 Value::CreateNullValue() :
416 Value::CreateStringValue(PointerToHexString(parent))); 223 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent)));
417 args.Append(children); 224 args.Append(children);
418 225
419 web_ui_->CallJavascriptFunction(L"CookiesView.loadChildren", args); 226 web_ui_->CallJavascriptFunction(L"CookiesView.loadChildren", args);
420 } 227 }
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