| OLD | NEW |
| 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 #ifndef CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ | 5 #ifndef CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ |
| 6 #define CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ | 6 #define CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 15 #include "chrome/test/live_sync/sync_datatype_helper.h" | |
| 16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 | 18 |
| 20 class GURL; | 19 class GURL; |
| 21 class Profile; | 20 class Profile; |
| 22 | 21 |
| 23 class BookmarksHelper : public SyncDatatypeHelper { | 22 namespace bookmarks_helper { |
| 24 public: | |
| 25 // Used to access the bookmark model within a particular sync profile. | |
| 26 static BookmarkModel* GetBookmarkModel(int index) WARN_UNUSED_RESULT; | |
| 27 | 23 |
| 28 // Used to access the bookmark bar within a particular sync profile. | 24 // Used to access the bookmark model within a particular sync profile. |
| 29 static const BookmarkNode* GetBookmarkBarNode(int index) WARN_UNUSED_RESULT; | 25 BookmarkModel* GetBookmarkModel(int index) WARN_UNUSED_RESULT; |
| 30 | 26 |
| 31 // Used to access the "other bookmarks" node within a particular sync profile. | 27 // Used to access the bookmark bar within a particular sync profile. |
| 32 static const BookmarkNode* GetOtherNode(int index) WARN_UNUSED_RESULT; | 28 const BookmarkNode* GetBookmarkBarNode(int index) WARN_UNUSED_RESULT; |
| 33 | 29 |
| 34 // Used to access the bookmarks within the verifier sync profile. | 30 // Used to access the "other bookmarks" node within a particular sync profile. |
| 35 static BookmarkModel* GetVerifierBookmarkModel() WARN_UNUSED_RESULT; | 31 const BookmarkNode* GetOtherNode(int index) WARN_UNUSED_RESULT; |
| 36 | 32 |
| 37 // Encrypt Bookmarks datatype. | 33 // Used to access the bookmarks within the verifier sync profile. |
| 38 static bool EnableEncryption(int index); | 34 BookmarkModel* GetVerifierBookmarkModel() WARN_UNUSED_RESULT; |
| 39 | 35 |
| 40 // Check if Bookmarks are encrypted. | 36 // Encrypt Bookmarks datatype. |
| 41 static bool IsEncrypted(int index); | 37 bool EnableEncryption(int index); |
| 42 | 38 |
| 43 // Adds a URL with address |url| and title |title| to the bookmark bar of | 39 // Check if Bookmarks are encrypted. |
| 44 // profile |profile|. Returns a pointer to the node that was added. | 40 bool IsEncrypted(int index); |
| 45 static const BookmarkNode* AddURL( | |
| 46 int profile, | |
| 47 const std::wstring& title, | |
| 48 const GURL& url) WARN_UNUSED_RESULT; | |
| 49 | 41 |
| 50 // Adds a URL with address |url| and title |title| to the bookmark bar of | 42 // Adds a URL with address |url| and title |title| to the bookmark bar of |
| 51 // profile |profile| at position |index|. Returns a pointer to the node that | 43 // profile |profile|. Returns a pointer to the node that was added. |
| 52 // was added. | 44 const BookmarkNode* AddURL( |
| 53 static const BookmarkNode* AddURL( | 45 int profile, |
| 54 int profile, | 46 const std::wstring& title, |
| 55 int index, | 47 const GURL& url) WARN_UNUSED_RESULT; |
| 56 const std::wstring& title, | |
| 57 const GURL& url) WARN_UNUSED_RESULT; | |
| 58 | 48 |
| 59 // Adds a URL with address |url| and title |title| under the node |parent| of | 49 // Adds a URL with address |url| and title |title| to the bookmark bar of |
| 60 // profile |profile| at position |index|. Returns a pointer to the node that | 50 // profile |profile| at position |index|. Returns a pointer to the node that |
| 61 // was added. | 51 // was added. |
| 62 static const BookmarkNode* AddURL( | 52 const BookmarkNode* AddURL( |
| 63 int profile, | 53 int profile, |
| 64 const BookmarkNode* parent, | 54 int index, |
| 65 int index, | 55 const std::wstring& title, |
| 66 const std::wstring& title, | 56 const GURL& url) WARN_UNUSED_RESULT; |
| 67 const GURL& url) WARN_UNUSED_RESULT; | |
| 68 | 57 |
| 69 // Adds a folder named |title| to the bookmark bar of profile |profile|. | 58 // Adds a URL with address |url| and title |title| under the node |parent| of |
| 70 // Returns a pointer to the folder that was added. | 59 // profile |profile| at position |index|. Returns a pointer to the node that |
| 71 static const BookmarkNode* AddFolder( | 60 // was added. |
| 72 int profile, | 61 const BookmarkNode* AddURL( |
| 73 const std::wstring& title) WARN_UNUSED_RESULT; | 62 int profile, |
| 63 const BookmarkNode* parent, |
| 64 int index, |
| 65 const std::wstring& title, |
| 66 const GURL& url) WARN_UNUSED_RESULT; |
| 74 | 67 |
| 75 // Adds a folder named |title| to the bookmark bar of profile |profile| at | 68 // Adds a folder named |title| to the bookmark bar of profile |profile|. |
| 76 // position |index|. Returns a pointer to the folder that was added. | 69 // Returns a pointer to the folder that was added. |
| 77 static const BookmarkNode* AddFolder( | 70 const BookmarkNode* AddFolder( |
| 78 int profile, | 71 int profile, |
| 79 int index, | 72 const std::wstring& title) WARN_UNUSED_RESULT; |
| 80 const std::wstring& title) WARN_UNUSED_RESULT; | |
| 81 | 73 |
| 82 // Adds a folder named |title| to the node |parent| in the bookmark model of | 74 // Adds a folder named |title| to the bookmark bar of profile |profile| at |
| 83 // profile |profile| at position |index|. Returns a pointer to the node that | 75 // position |index|. Returns a pointer to the folder that was added. |
| 84 // was added. | 76 const BookmarkNode* AddFolder( |
| 85 static const BookmarkNode* AddFolder( | 77 int profile, |
| 86 int profile, | 78 int index, |
| 87 const BookmarkNode* parent, | 79 const std::wstring& title) WARN_UNUSED_RESULT; |
| 88 int index, | |
| 89 const std::wstring& title) WARN_UNUSED_RESULT; | |
| 90 | 80 |
| 91 // Changes the title of the node |node| in the bookmark model of profile | 81 // Adds a folder named |title| to the node |parent| in the bookmark model of |
| 92 // |profile| to |new_title|. | 82 // profile |profile| at position |index|. Returns a pointer to the node that |
| 93 static void SetTitle(int profile, | 83 // was added. |
| 94 const BookmarkNode* node, | 84 const BookmarkNode* AddFolder( |
| 95 const std::wstring& new_title); | 85 int profile, |
| 86 const BookmarkNode* parent, |
| 87 int index, |
| 88 const std::wstring& title) WARN_UNUSED_RESULT; |
| 96 | 89 |
| 97 // Sets the favicon of the node |node| (of type BookmarkNode::URL) in the | 90 // Changes the title of the node |node| in the bookmark model of profile |
| 98 // bookmark model of profile |profile| using the data in |icon_bytes_vector|. | 91 // |profile| to |new_title|. |
| 99 static void SetFavicon( | 92 void SetTitle(int profile, |
| 100 int profile, | 93 const BookmarkNode* node, |
| 101 const BookmarkNode* node, | 94 const std::wstring& new_title); |
| 102 const std::vector<unsigned char>& icon_bytes_vector); | |
| 103 | 95 |
| 104 // Changes the url of the node |node| in the bookmark model of profile | 96 // Sets the favicon of the node |node| (of type BookmarkNode::URL) in the |
| 105 // |profile| to |new_url|. Returns a pointer to the node with the changed url. | 97 // bookmark model of profile |profile| using the data in |icon_bytes_vector|. |
| 106 static const BookmarkNode* SetURL( | 98 void SetFavicon( |
| 107 int profile, | 99 int profile, |
| 108 const BookmarkNode* node, | 100 const BookmarkNode* node, |
| 109 const GURL& new_url) WARN_UNUSED_RESULT; | 101 const std::vector<unsigned char>& icon_bytes_vector); |
| 110 | 102 |
| 111 // Moves the node |node| in the bookmark model of profile |profile| so it ends | 103 // Changes the url of the node |node| in the bookmark model of profile |
| 112 // up under the node |new_parent| at position |index|. | 104 // |profile| to |new_url|. Returns a pointer to the node with the changed url. |
| 113 static void Move( | 105 const BookmarkNode* SetURL( |
| 114 int profile, | 106 int profile, |
| 115 const BookmarkNode* node, | 107 const BookmarkNode* node, |
| 116 const BookmarkNode* new_parent, | 108 const GURL& new_url) WARN_UNUSED_RESULT; |
| 117 int index); | |
| 118 | 109 |
| 119 // Removes the node in the bookmark model of profile |profile| under the node | 110 // Moves the node |node| in the bookmark model of profile |profile| so it ends |
| 120 // |parent| at position |index|. | 111 // up under the node |new_parent| at position |index|. |
| 121 static void Remove(int profile, const BookmarkNode* parent, int index); | 112 void Move( |
| 113 int profile, |
| 114 const BookmarkNode* node, |
| 115 const BookmarkNode* new_parent, |
| 116 int index); |
| 122 | 117 |
| 123 // Sorts the children of the node |parent| in the bookmark model of profile | 118 // Removes the node in the bookmark model of profile |profile| under the node |
| 124 // |profile|. | 119 // |parent| at position |index|. |
| 125 static void SortChildren(int profile, const BookmarkNode* parent); | 120 void Remove(int profile, const BookmarkNode* parent, int index); |
| 126 | 121 |
| 127 // Reverses the order of the children of the node |parent| in the bookmark | 122 // Sorts the children of the node |parent| in the bookmark model of profile |
| 128 // model of profile |profile|. | 123 // |profile|. |
| 129 static void ReverseChildOrder(int profile, const BookmarkNode* parent); | 124 void SortChildren(int profile, const BookmarkNode* parent); |
| 130 | 125 |
| 131 // Checks if the bookmark model of profile |profile| matches the verifier | 126 // Reverses the order of the children of the node |parent| in the bookmark |
| 132 // bookmark model. Returns true if they match. | 127 // model of profile |profile|. |
| 133 static bool ModelMatchesVerifier(int profile) WARN_UNUSED_RESULT; | 128 void ReverseChildOrder(int profile, const BookmarkNode* parent); |
| 134 | 129 |
| 135 // Checks if the bookmark models of all sync profiles match the verifier | 130 // Checks if the bookmark model of profile |profile| matches the verifier |
| 136 // bookmark model. Returns true if they match. | 131 // bookmark model. Returns true if they match. |
| 137 static bool AllModelsMatchVerifier() WARN_UNUSED_RESULT; | 132 bool ModelMatchesVerifier(int profile) WARN_UNUSED_RESULT; |
| 138 | 133 |
| 139 // Checks if the bookmark models of |profile_a| and |profile_b| match each | 134 // Checks if the bookmark models of all sync profiles match the verifier |
| 140 // other. Returns true if they match. | 135 // bookmark model. Returns true if they match. |
| 141 static bool ModelsMatch(int profile_a, int profile_b) WARN_UNUSED_RESULT; | 136 bool AllModelsMatchVerifier() WARN_UNUSED_RESULT; |
| 142 | 137 |
| 143 // Checks if the bookmark models of all sync profiles match each other. Does | 138 // Checks if the bookmark models of |profile_a| and |profile_b| match each |
| 144 // not compare them with the verifier bookmark model. Returns true if they | 139 // other. Returns true if they match. |
| 145 // match. | 140 bool ModelsMatch(int profile_a, int profile_b) WARN_UNUSED_RESULT; |
| 146 static bool AllModelsMatch() WARN_UNUSED_RESULT; | |
| 147 | 141 |
| 148 // Checks if the bookmark model of profile |profile| contains any instances of | 142 // Checks if the bookmark models of all sync profiles match each other. Does |
| 149 // two bookmarks with the same URL under the same parent folder. Returns true | 143 // not compare them with the verifier bookmark model. Returns true if they |
| 150 // if even one instance is found. | 144 // match. |
| 151 static bool ContainsDuplicateBookmarks(int profile); | 145 bool AllModelsMatch() WARN_UNUSED_RESULT; |
| 152 | 146 |
| 153 // Gets the node in the bookmark model of profile |profile| that has the url | 147 // Checks if the bookmark model of profile |profile| contains any instances of |
| 154 // |url|. Note: Only one instance of |url| is assumed to be present. | 148 // two bookmarks with the same URL under the same parent folder. Returns true |
| 155 static const BookmarkNode* GetUniqueNodeByURL( | 149 // if even one instance is found. |
| 156 int profile, | 150 bool ContainsDuplicateBookmarks(int profile); |
| 157 const GURL& url) WARN_UNUSED_RESULT; | |
| 158 | 151 |
| 159 // Returns the number of bookmarks in bookmark model of profile |profile| | 152 // Gets the node in the bookmark model of profile |profile| that has the url |
| 160 // whose titles match the string |title|. | 153 // |url|. Note: Only one instance of |url| is assumed to be present. |
| 161 static int CountBookmarksWithTitlesMatching( | 154 const BookmarkNode* GetUniqueNodeByURL( |
| 162 int profile, | 155 int profile, |
| 163 const std::wstring& title) WARN_UNUSED_RESULT; | 156 const GURL& url) WARN_UNUSED_RESULT; |
| 164 | 157 |
| 165 // Returns the number of bookmark folders in the bookmark model of profile | 158 // Returns the number of bookmarks in bookmark model of profile |profile| |
| 166 // |profile| whose titles contain the query string |title|. | 159 // whose titles match the string |title|. |
| 167 static int CountFoldersWithTitlesMatching( | 160 int CountBookmarksWithTitlesMatching( |
| 168 int profile, | 161 int profile, |
| 169 const std::wstring& title) WARN_UNUSED_RESULT; | 162 const std::wstring& title) WARN_UNUSED_RESULT; |
| 170 | 163 |
| 171 // Creates a unique favicon using |seed|. | 164 // Returns the number of bookmark folders in the bookmark model of profile |
| 172 static std::vector<unsigned char> CreateFavicon(int seed); | 165 // |profile| whose titles contain the query string |title|. |
| 166 int CountFoldersWithTitlesMatching( |
| 167 int profile, |
| 168 const std::wstring& title) WARN_UNUSED_RESULT; |
| 173 | 169 |
| 174 // Returns a URL identifiable by |i|. | 170 // Creates a unique favicon using |seed|. |
| 175 static std::string IndexedURL(int i); | 171 std::vector<unsigned char> CreateFavicon(int seed); |
| 176 | 172 |
| 177 // Returns a URL title identifiable by |i|. | 173 // Returns a URL identifiable by |i|. |
| 178 static std::wstring IndexedURLTitle(int i); | 174 std::string IndexedURL(int i); |
| 179 | 175 |
| 180 // Returns a folder name identifiable by |i|. | 176 // Returns a URL title identifiable by |i|. |
| 181 static std::wstring IndexedFolderName(int i); | 177 std::wstring IndexedURLTitle(int i); |
| 182 | 178 |
| 183 // Returns a subfolder name identifiable by |i|. | 179 // Returns a folder name identifiable by |i|. |
| 184 static std::wstring IndexedSubfolderName(int i); | 180 std::wstring IndexedFolderName(int i); |
| 185 | 181 |
| 186 // Returns a subsubfolder name identifiable by |i|. | 182 // Returns a subfolder name identifiable by |i|. |
| 187 static std::wstring IndexedSubsubfolderName(int i); | 183 std::wstring IndexedSubfolderName(int i); |
| 188 | 184 |
| 189 protected: | 185 // Returns a subsubfolder name identifiable by |i|. |
| 190 BookmarksHelper(); | 186 std::wstring IndexedSubsubfolderName(int i); |
| 191 virtual ~BookmarksHelper(); | |
| 192 | 187 |
| 193 private: | 188 } // namespace bookmarks_helper |
| 194 // Finds the node in the verifier bookmark model that corresponds to | |
| 195 // |foreign_node| in |foreign_model| and stores its address in |result|. | |
| 196 static void FindNodeInVerifier(BookmarkModel* foreign_model, | |
| 197 const BookmarkNode* foreign_node, | |
| 198 const BookmarkNode** result); | |
| 199 | |
| 200 // Returns the number of nodes of node type |node_type| in |model| whose | |
| 201 // titles match the string |title|. | |
| 202 static int CountNodesWithTitlesMatching(BookmarkModel* model, | |
| 203 BookmarkNode::Type node_type, | |
| 204 const string16& title); | |
| 205 | |
| 206 // Checks if the hierarchies in |model_a| and |model_b| are equivalent in | |
| 207 // terms of the data model and favicon. Returns true if they both match. | |
| 208 // Note: Some peripheral fields like creation times are allowed to mismatch. | |
| 209 static bool BookmarkModelsMatch(BookmarkModel* model_a, | |
| 210 BookmarkModel* model_b) WARN_UNUSED_RESULT; | |
| 211 | |
| 212 // Does a deep comparison of BookmarkNode fields in |model_a| and |model_b|. | |
| 213 // Returns true if they are all equal. | |
| 214 static bool NodesMatch(const BookmarkNode* model_a, | |
| 215 const BookmarkNode* model_b); | |
| 216 | |
| 217 // Checks if the favicon in |node_a| from |model_a| matches that of |node_b| | |
| 218 // from |model_b|. Returns true if they match. | |
| 219 static bool FaviconsMatch(BookmarkModel* model_a, | |
| 220 BookmarkModel* model_b, | |
| 221 const BookmarkNode* node_a, | |
| 222 const BookmarkNode* node_b); | |
| 223 | |
| 224 // Checks if the favicon data in |bitmap_a| and |bitmap_b| are equivalent. | |
| 225 // Returns true if they match. | |
| 226 static bool FaviconBitmapsMatch(const SkBitmap& bitmap_a, | |
| 227 const SkBitmap& bitmap_b); | |
| 228 | |
| 229 // Gets the favicon associated with |node| in |model|. | |
| 230 static const SkBitmap& GetFavicon(BookmarkModel* model, | |
| 231 const BookmarkNode* node); | |
| 232 | |
| 233 // A collection of URLs for which we have added favicons. Since loading a | |
| 234 // favicon is an asynchronous operation and doesn't necessarily invoke a | |
| 235 // callback, this collection is used to determine if we must wait for a URL's | |
| 236 // favicon to load or not. | |
| 237 static std::set<GURL>* urls_with_favicons_; | |
| 238 | |
| 239 DISALLOW_COPY_AND_ASSIGN(BookmarksHelper); | |
| 240 }; | |
| 241 | 189 |
| 242 #endif // CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ | 190 #endif // CHROME_TEST_LIVE_SYNC_BOOKMARKS_HELPER_H_ |
| OLD | NEW |