OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/cookies_tree_model.h" | 5 #include "chrome/browser/cookies_tree_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
12 #include "app/resource_bundle.h" | 12 #include "app/resource_bundle.h" |
13 #include "app/table_model_observer.h" | 13 #include "app/table_model_observer.h" |
14 #include "app/tree_node_model.h" | 14 #include "app/tree_node_model.h" |
15 #include "base/linked_ptr.h" | 15 #include "base/linked_ptr.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "chrome/browser/in_process_webkit/webkit_context.h" |
17 #include "chrome/browser/net/chrome_url_request_context.h" | 18 #include "chrome/browser/net/chrome_url_request_context.h" |
18 #include "chrome/browser/profile.h" | 19 #include "chrome/browser/profile.h" |
19 #include "grit/app_resources.h" | 20 #include "grit/app_resources.h" |
20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
21 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
22 #include "net/base/cookie_monster.h" | 23 #include "net/base/cookie_monster.h" |
23 #include "net/base/registry_controlled_domain.h" | 24 #include "net/base/registry_controlled_domain.h" |
24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
25 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
26 | 27 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 retval += host.substr(next_dot + 1, position - (next_dot + 1)); | 108 retval += host.substr(next_dot + 1, position - (next_dot + 1)); |
108 position = next_dot; | 109 position = next_dot; |
109 } | 110 } |
110 return retval; | 111 return retval; |
111 } | 112 } |
112 }; | 113 }; |
113 | 114 |
114 } // namespace | 115 } // namespace |
115 | 116 |
116 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
| 118 // CookieTreeLocalStorageNode, public: |
| 119 |
| 120 CookieTreeLocalStorageNode::CookieTreeLocalStorageNode( |
| 121 BrowsingDataLocalStorageHelper::LocalStorageInfo* local_storage_info) |
| 122 : CookieTreeNode(UTF8ToWide( |
| 123 !local_storage_info->origin.empty() ? |
| 124 local_storage_info->origin : |
| 125 local_storage_info->database_identifier)), |
| 126 local_storage_info_(local_storage_info) { |
| 127 } |
| 128 |
| 129 void CookieTreeLocalStorageNode::DeleteStoredObjects() { |
| 130 GetModel()->DeleteLocalStorage(local_storage_info_->file_path); |
| 131 } |
| 132 |
| 133 /////////////////////////////////////////////////////////////////////////////// |
117 // CookieTreeRootNode, public: | 134 // CookieTreeRootNode, public: |
118 CookieTreeOriginNode* CookieTreeRootNode::GetOrCreateOriginNode( | 135 CookieTreeOriginNode* CookieTreeRootNode::GetOrCreateOriginNode( |
119 const std::wstring& origin) { | 136 const std::wstring& origin) { |
120 // Strip the trailing dot if it exists. | 137 // Strip the trailing dot if it exists. |
121 std::wstring rewritten_origin = origin; | 138 std::wstring rewritten_origin = origin; |
122 if (origin.length() >= 1 && origin[0] == '.') | 139 if (origin.length() >= 1 && origin[0] == '.') |
123 rewritten_origin = origin.substr(1); | 140 rewritten_origin = origin.substr(1); |
124 | 141 |
125 CookieTreeOriginNode rewritten_origin_node(rewritten_origin); | 142 CookieTreeOriginNode rewritten_origin_node(rewritten_origin); |
126 | 143 |
(...skipping 21 matching lines...) Expand all Loading... |
148 CookieTreeCookiesNode* CookieTreeOriginNode::GetOrCreateCookiesNode() { | 165 CookieTreeCookiesNode* CookieTreeOriginNode::GetOrCreateCookiesNode() { |
149 if (cookies_child_) | 166 if (cookies_child_) |
150 return cookies_child_; | 167 return cookies_child_; |
151 // need to make a Cookies node, add it to the tree, and return it | 168 // need to make a Cookies node, add it to the tree, and return it |
152 CookieTreeCookiesNode* retval = new CookieTreeCookiesNode; | 169 CookieTreeCookiesNode* retval = new CookieTreeCookiesNode; |
153 GetModel()->Add(this, 0, retval); | 170 GetModel()->Add(this, 0, retval); |
154 cookies_child_ = retval; | 171 cookies_child_ = retval; |
155 return retval; | 172 return retval; |
156 } | 173 } |
157 | 174 |
| 175 CookieTreeLocalStoragesNode* |
| 176 CookieTreeOriginNode::GetOrCreateLocalStoragesNode() { |
| 177 if (local_storages_child_) |
| 178 return local_storages_child_; |
| 179 // need to make a LocalStorages node, add it to the tree, and return it |
| 180 CookieTreeLocalStoragesNode* retval = new CookieTreeLocalStoragesNode; |
| 181 GetModel()->Add(this, cookies_child_ ? 1 : 0, retval); |
| 182 local_storages_child_ = retval; |
| 183 return retval; |
| 184 } |
| 185 |
158 /////////////////////////////////////////////////////////////////////////////// | 186 /////////////////////////////////////////////////////////////////////////////// |
159 // CookieTreeCookiesNode, public: | 187 // CookieTreeCookiesNode, public: |
160 | 188 |
161 CookieTreeCookiesNode::CookieTreeCookiesNode() | 189 CookieTreeCookiesNode::CookieTreeCookiesNode() |
162 : CookieTreeNode(l10n_util::GetString(IDS_COOKIES_COOKIES)) {} | 190 : CookieTreeNode(l10n_util::GetString(IDS_COOKIES_COOKIES)) {} |
163 | 191 |
164 | 192 |
165 void CookieTreeCookiesNode::AddCookieNode( | 193 void CookieTreeCookiesNode::AddCookieNode( |
166 CookieTreeCookieNode* new_child) { | 194 CookieTreeCookieNode* new_child) { |
167 std::vector<CookieTreeNode*>::iterator cookie_iterator = | 195 std::vector<CookieTreeNode*>::iterator cookie_iterator = |
168 lower_bound(children().begin(), | 196 lower_bound(children().begin(), |
169 children().end(), | 197 children().end(), |
170 new_child, | 198 new_child, |
171 CookieTreeCookieNode::CookieNodeComparator()); | 199 CookieTreeCookieNode::CookieNodeComparator()); |
172 GetModel()->Add(this, (cookie_iterator - children().begin()), new_child); | 200 GetModel()->Add(this, (cookie_iterator - children().begin()), new_child); |
173 } | 201 } |
174 | 202 |
175 /////////////////////////////////////////////////////////////////////////////// | 203 /////////////////////////////////////////////////////////////////////////////// |
| 204 // CookieTreeLocalStoragesNode, public: |
| 205 |
| 206 CookieTreeLocalStoragesNode::CookieTreeLocalStoragesNode() |
| 207 : CookieTreeNode(l10n_util::GetString(IDS_COOKIES_LOCAL_STORAGE)) { |
| 208 } |
| 209 |
| 210 void CookieTreeLocalStoragesNode::AddLocalStorageNode( |
| 211 CookieTreeLocalStorageNode* new_child) { |
| 212 std::vector<CookieTreeNode*>::iterator local_storage_iterator = |
| 213 lower_bound(children().begin(), |
| 214 children().end(), |
| 215 new_child, |
| 216 CookieTreeLocalStorageNode::CookieNodeComparator()); |
| 217 GetModel()->Add(this, |
| 218 (local_storage_iterator - children().begin()), |
| 219 new_child); |
| 220 } |
| 221 |
| 222 /////////////////////////////////////////////////////////////////////////////// |
176 // CookieTreeCookieNode, private | 223 // CookieTreeCookieNode, private |
177 | 224 |
178 bool CookieTreeCookieNode::CookieNodeComparator::operator() ( | 225 bool CookieTreeCookieNode::CookieNodeComparator::operator() ( |
179 const CookieTreeNode* lhs, const CookieTreeNode* rhs) { | 226 const CookieTreeNode* lhs, const CookieTreeNode* rhs) { |
180 return (static_cast<const CookieTreeCookieNode*>(lhs)-> | 227 const CookieTreeCookieNode* left = |
181 cookie_->second.Name() < | 228 static_cast<const CookieTreeCookieNode*>(lhs); |
182 static_cast<const CookieTreeCookieNode*>(rhs)-> | 229 const CookieTreeCookieNode* right = |
183 cookie_->second.Name()); | 230 static_cast<const CookieTreeCookieNode*>(rhs); |
| 231 return (left->cookie_->second.Name() < right->cookie_->second.Name()); |
| 232 } |
| 233 |
| 234 /////////////////////////////////////////////////////////////////////////////// |
| 235 // CookieTreeLocalStorageNode, private |
| 236 |
| 237 bool CookieTreeLocalStorageNode::CookieNodeComparator::operator() ( |
| 238 const CookieTreeNode* lhs, const CookieTreeNode* rhs) { |
| 239 const CookieTreeLocalStorageNode* left = |
| 240 static_cast<const CookieTreeLocalStorageNode*>(lhs); |
| 241 const CookieTreeLocalStorageNode* right = |
| 242 static_cast<const CookieTreeLocalStorageNode*>(rhs); |
| 243 return (left->local_storage_info_->origin < |
| 244 right->local_storage_info_->origin); |
184 } | 245 } |
185 | 246 |
186 /////////////////////////////////////////////////////////////////////////////// | 247 /////////////////////////////////////////////////////////////////////////////// |
187 // CookiesTreeModel, public: | 248 // CookiesTreeModel, public: |
188 | 249 |
189 CookiesTreeModel::CookiesTreeModel(Profile* profile) | 250 CookiesTreeModel::CookiesTreeModel( |
| 251 Profile* profile, |
| 252 BrowsingDataLocalStorageHelper* local_storage_helper) |
190 : ALLOW_THIS_IN_INITIALIZER_LIST(TreeNodeModel<CookieTreeNode>( | 253 : ALLOW_THIS_IN_INITIALIZER_LIST(TreeNodeModel<CookieTreeNode>( |
191 new CookieTreeRootNode(this))), | 254 new CookieTreeRootNode(this))), |
192 profile_(profile) { | 255 profile_(profile), |
| 256 local_storage_helper_(local_storage_helper) { |
193 LoadCookies(); | 257 LoadCookies(); |
| 258 DCHECK(local_storage_helper_); |
| 259 local_storage_helper_->StartFetching(NewCallback( |
| 260 this, &CookiesTreeModel::OnStorageModelInfoLoaded)); |
| 261 } |
| 262 |
| 263 CookiesTreeModel::~CookiesTreeModel() { |
| 264 local_storage_helper_->CancelNotification(); |
194 } | 265 } |
195 | 266 |
196 /////////////////////////////////////////////////////////////////////////////// | 267 /////////////////////////////////////////////////////////////////////////////// |
197 // CookiesTreeModel, TreeModel methods (public): | 268 // CookiesTreeModel, TreeModel methods (public): |
198 | 269 |
199 // TreeModel methods: | 270 // TreeModel methods: |
200 // Returns the set of icons for the nodes in the tree. You only need override | 271 // Returns the set of icons for the nodes in the tree. You only need override |
201 // this if you don't want to use the default folder icons. | 272 // this if you don't want to use the default folder icons. |
202 void CookiesTreeModel::GetIcons(std::vector<SkBitmap>* icons) { | 273 void CookiesTreeModel::GetIcons(std::vector<SkBitmap>* icons) { |
203 icons->push_back(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | 274 icons->push_back(*ResourceBundle::GetSharedInstance().GetBitmapNamed( |
204 IDR_DEFAULT_FAVICON)); | 275 IDR_DEFAULT_FAVICON)); |
205 icons->push_back(*ResourceBundle::GetSharedInstance().GetBitmapNamed( | 276 icons->push_back(*ResourceBundle::GetSharedInstance().GetBitmapNamed( |
206 IDR_COOKIE_ICON)); | 277 IDR_COOKIE_ICON)); |
207 } | 278 } |
208 | 279 |
209 // Returns the index of the icon to use for |node|. Return -1 to use the | 280 // Returns the index of the icon to use for |node|. Return -1 to use the |
210 // default icon. The index is relative to the list of icons returned from | 281 // default icon. The index is relative to the list of icons returned from |
211 // GetIcons. | 282 // GetIcons. |
212 int CookiesTreeModel::GetIconIndex(TreeModelNode* node) { | 283 int CookiesTreeModel::GetIconIndex(TreeModelNode* node) { |
213 CookieTreeNode* ct_node = static_cast<CookieTreeNode*>(node); | 284 CookieTreeNode* ct_node = static_cast<CookieTreeNode*>(node); |
214 switch (ct_node->GetDetailedInfo().node_type) { | 285 switch (ct_node->GetDetailedInfo().node_type) { |
215 case CookieTreeNode::DetailedInfo::TYPE_ORIGIN: | 286 case CookieTreeNode::DetailedInfo::TYPE_ORIGIN: |
216 return ORIGIN; | 287 return ORIGIN; |
217 break; | 288 break; |
218 case CookieTreeNode::DetailedInfo::TYPE_COOKIE: | 289 case CookieTreeNode::DetailedInfo::TYPE_COOKIE: |
219 return COOKIE; | 290 return COOKIE; |
220 break; | 291 break; |
| 292 case CookieTreeNode::DetailedInfo::TYPE_LOCAL_STORAGE: |
| 293 // TODO(bulach): add an icon for local storage. |
221 default: | 294 default: |
222 return -1; | 295 return -1; |
223 } | 296 } |
224 } | 297 } |
225 | 298 |
226 void CookiesTreeModel::LoadCookies() { | 299 void CookiesTreeModel::LoadCookies() { |
227 LoadCookiesWithFilter(L""); | 300 LoadCookiesWithFilter(L""); |
228 } | 301 } |
229 | 302 |
230 void CookiesTreeModel::LoadCookiesWithFilter(const std::wstring& filter) { | 303 void CookiesTreeModel::LoadCookiesWithFilter(const std::wstring& filter) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 } | 349 } |
277 | 350 |
278 void CookiesTreeModel::DeleteCookieNode(CookieTreeNode* cookie_node) { | 351 void CookiesTreeModel::DeleteCookieNode(CookieTreeNode* cookie_node) { |
279 cookie_node->DeleteStoredObjects(); | 352 cookie_node->DeleteStoredObjects(); |
280 // find the parent and index | 353 // find the parent and index |
281 CookieTreeNode* parent_node = cookie_node->GetParent(); | 354 CookieTreeNode* parent_node = cookie_node->GetParent(); |
282 int cookie_node_index = parent_node->IndexOfChild(cookie_node); | 355 int cookie_node_index = parent_node->IndexOfChild(cookie_node); |
283 delete Remove(parent_node, cookie_node_index); | 356 delete Remove(parent_node, cookie_node_index); |
284 } | 357 } |
285 | 358 |
| 359 void CookiesTreeModel::DeleteLocalStorage(const FilePath& file_path) { |
| 360 local_storage_helper_->DeleteLocalStorageFile(file_path); |
| 361 } |
| 362 |
| 363 void CookiesTreeModel::DeleteAllLocalStorage() { |
| 364 local_storage_helper_->DeleteAllLocalStorageFiles(); |
| 365 } |
| 366 |
286 void CookiesTreeModel::UpdateSearchResults(const std::wstring& filter) { | 367 void CookiesTreeModel::UpdateSearchResults(const std::wstring& filter) { |
287 CookieTreeNode* root = GetRoot(); | 368 CookieTreeNode* root = GetRoot(); |
288 int num_children = root->GetChildCount(); | 369 int num_children = root->GetChildCount(); |
289 for (int i = num_children - 1; i >= 0; --i) | 370 for (int i = num_children - 1; i >= 0; --i) |
290 delete Remove(root, i); | 371 delete Remove(root, i); |
291 LoadCookiesWithFilter(filter); | 372 LoadCookiesWithFilter(filter); |
| 373 PopulateLocalStorageInfoWithFilter(filter); |
292 NotifyObserverTreeNodeChanged(root); | 374 NotifyObserverTreeNodeChanged(root); |
293 } | 375 } |
| 376 |
| 377 void CookiesTreeModel::OnStorageModelInfoLoaded( |
| 378 const LocalStorageInfoList& local_storage_info) { |
| 379 local_storage_info_list_ = local_storage_info; |
| 380 PopulateLocalStorageInfoWithFilter(L""); |
| 381 } |
| 382 |
| 383 void CookiesTreeModel::PopulateLocalStorageInfoWithFilter( |
| 384 const std::wstring& filter) { |
| 385 CookieTreeRootNode* root = static_cast<CookieTreeRootNode*>(GetRoot()); |
| 386 for (LocalStorageInfoList::iterator local_storage_info = |
| 387 local_storage_info_list_.begin(); |
| 388 local_storage_info != local_storage_info_list_.end(); |
| 389 ++local_storage_info) { |
| 390 std::string origin = |
| 391 !local_storage_info->host.empty() ? |
| 392 local_storage_info->host : |
| 393 local_storage_info->database_identifier; |
| 394 if (!filter.size() || |
| 395 (UTF8ToWide(origin).find(filter) != std::wstring::npos)) { |
| 396 CookieTreeOriginNode* origin_node = root->GetOrCreateOriginNode( |
| 397 UTF8ToWide(local_storage_info->host)); |
| 398 CookieTreeLocalStoragesNode* local_storages_node = |
| 399 origin_node->GetOrCreateLocalStoragesNode(); |
| 400 local_storages_node->AddLocalStorageNode( |
| 401 new CookieTreeLocalStorageNode(&(*local_storage_info))); |
| 402 } |
| 403 } |
| 404 NotifyObserverTreeNodeChanged(root); |
| 405 } |
OLD | NEW |