| 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/net/chrome_url_request_context.h" | 17 #include "chrome/browser/net/chrome_url_request_context.h" |
| 18 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| 19 #include "grit/app_resources.h" | 19 #include "grit/app_resources.h" |
| 20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 21 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 22 #include "net/base/cookie_monster.h" | 22 #include "net/base/cookie_monster.h" |
| 23 #include "net/base/registry_controlled_domain.h" |
| 23 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
| 24 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 25 | 26 |
| 26 | 27 |
| 27 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
| 28 // CookieTreeNode, public: | 29 // CookieTreeNode, public: |
| 29 | 30 |
| 30 void CookieTreeNode::DeleteStoredObjects() { | 31 void CookieTreeNode::DeleteStoredObjects() { |
| 31 std::for_each(children().begin(), | 32 std::for_each(children().begin(), |
| 32 children().end(), | 33 children().end(), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 52 void CookieTreeCookieNode::DeleteStoredObjects() { | 53 void CookieTreeCookieNode::DeleteStoredObjects() { |
| 53 GetModel()->DeleteCookie(*cookie_); | 54 GetModel()->DeleteCookie(*cookie_); |
| 54 } | 55 } |
| 55 | 56 |
| 56 namespace { | 57 namespace { |
| 57 // comparison functor, for use in CookieTreeRootNode | 58 // comparison functor, for use in CookieTreeRootNode |
| 58 class OriginNodeComparator { | 59 class OriginNodeComparator { |
| 59 public: | 60 public: |
| 60 bool operator() (const CookieTreeNode* lhs, | 61 bool operator() (const CookieTreeNode* lhs, |
| 61 const CookieTreeNode* rhs) { | 62 const CookieTreeNode* rhs) { |
| 62 return (lhs->GetTitle() < rhs->GetTitle()); | 63 // We want to order by registry controlled domain, so we would get |
| 64 // google.com, ad.google.com, www.google.com, |
| 65 // microsoft.com, ad.microsoft.com. CanonicalizeHost transforms the origins |
| 66 // into a form like google.com.www so that string comparisons work. |
| 67 return (CanonicalizeHost(lhs->GetTitle()) < |
| 68 CanonicalizeHost(rhs->GetTitle())); |
| 69 } |
| 70 |
| 71 private: |
| 72 std::string CanonicalizeHost(const std::wstring& host_w) { |
| 73 // The canonicalized representation makes the registry controlled domain |
| 74 // come first, and then adds subdomains in reverse order, e.g. |
| 75 // 1.mail.google.com would become google.com.mail.1, and then a standard |
| 76 // string comparison works to order hosts by registry controlled domain |
| 77 // first. Leading dots are ignored, ".google.com" is the same as |
| 78 // "google.com". |
| 79 |
| 80 std::string host = WideToUTF8(host_w); |
| 81 std::string retval = net::RegistryControlledDomainService:: |
| 82 GetDomainAndRegistry(host); |
| 83 if (!retval.length()) // Is an IP address or other special origin. |
| 84 return host; |
| 85 |
| 86 std::string::size_type position = host.rfind(retval); |
| 87 |
| 88 // The host may be the registry controlled domain, in which case fail fast. |
| 89 if (position == 0 || position == std::string::npos) |
| 90 return host; |
| 91 |
| 92 // If host is www.google.com, retval will contain google.com at this point. |
| 93 // Start operating to the left of the registry controlled domain, e.g. in |
| 94 // the www.google.com example, start at index 3. |
| 95 --position; |
| 96 |
| 97 // If position == 0, that means it's a dot; this will be ignored to treat |
| 98 // ".google.com" the same as "google.com". |
| 99 while (position > 0) { |
| 100 retval += std::string("."); |
| 101 // Copy up to the next dot. host[position] is a dot so start after it. |
| 102 std::string::size_type next_dot = host.rfind(".", position - 1); |
| 103 if (next_dot == std::string::npos) { |
| 104 retval += host.substr(0, position); |
| 105 break; |
| 106 } |
| 107 retval += host.substr(next_dot + 1, position - (next_dot + 1)); |
| 108 position = next_dot; |
| 109 } |
| 110 return retval; |
| 63 } | 111 } |
| 64 }; | 112 }; |
| 65 | 113 |
| 66 } // namespace | 114 } // namespace |
| 67 | 115 |
| 68 /////////////////////////////////////////////////////////////////////////////// | 116 /////////////////////////////////////////////////////////////////////////////// |
| 69 // CookieTreeRootNode, public: | 117 // CookieTreeRootNode, public: |
| 70 CookieTreeOriginNode* CookieTreeRootNode::GetOrCreateOriginNode( | 118 CookieTreeOriginNode* CookieTreeRootNode::GetOrCreateOriginNode( |
| 71 const std::wstring& origin) { | 119 const std::wstring& origin) { |
| 72 // Strip the trailing dot if it exists. | 120 // Strip the trailing dot if it exists. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 NotifyObserverTreeNodeChanged(root); | 270 NotifyObserverTreeNodeChanged(root); |
| 223 } | 271 } |
| 224 | 272 |
| 225 void CookiesTreeModel::DeleteCookieNode(CookieTreeNode* cookie_node) { | 273 void CookiesTreeModel::DeleteCookieNode(CookieTreeNode* cookie_node) { |
| 226 cookie_node->DeleteStoredObjects(); | 274 cookie_node->DeleteStoredObjects(); |
| 227 // find the parent and index | 275 // find the parent and index |
| 228 CookieTreeNode* parent_node = cookie_node->GetParent(); | 276 CookieTreeNode* parent_node = cookie_node->GetParent(); |
| 229 int cookie_node_index = parent_node->IndexOfChild(cookie_node); | 277 int cookie_node_index = parent_node->IndexOfChild(cookie_node); |
| 230 delete Remove(parent_node, cookie_node_index); | 278 delete Remove(parent_node, cookie_node_index); |
| 231 } | 279 } |
| OLD | NEW |