Add testing/scripts/OWNERS
[chromium-blink-merge.git] / components / enhanced_bookmarks / enhanced_bookmark_model.h
blob4196f647340c0726e09e86a0c2b76b2f82a05e51
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
8 #include <map>
9 #include <string>
11 #include "base/cancelable_callback.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/strings/string16.h"
16 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
17 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "components/keyed_service/core/keyed_service.h"
20 namespace base {
21 class Time;
22 } // namespace base
24 class BookmarkModel;
25 class BookmarkNode;
26 class GURL;
28 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo);
30 namespace enhanced_bookmarks {
32 class EnhancedBookmarkModelObserver;
34 // Wrapper around BookmarkModel providing utility functions for enhanced
35 // bookmarks.
36 class EnhancedBookmarkModel : public KeyedService,
37 public BaseBookmarkModelObserver {
38 public:
39 EnhancedBookmarkModel(BookmarkModel* bookmark_model,
40 const std::string& version);
41 ~EnhancedBookmarkModel() override;
43 void Shutdown() override;
45 void AddObserver(EnhancedBookmarkModelObserver* observer);
46 void RemoveObserver(EnhancedBookmarkModelObserver* observer);
48 // Moves |node| to |new_parent| and inserts it at the given |index|.
49 void Move(const BookmarkNode* node,
50 const BookmarkNode* new_parent,
51 int index);
53 // Adds a new folder node at the specified position.
54 const BookmarkNode* AddFolder(const BookmarkNode* parent,
55 int index,
56 const base::string16& title);
58 // Adds a url at the specified position.
59 const BookmarkNode* AddURL(const BookmarkNode* parent,
60 int index,
61 const base::string16& title,
62 const GURL& url,
63 const base::Time& creation_time);
65 // Returns the remote id for a bookmark |node|.
66 std::string GetRemoteId(const BookmarkNode* node);
68 // Returns the bookmark node corresponding to the given |remote_id|, or NULL
69 // if there is no node with the id.
70 const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id);
72 // Sets the description of a bookmark |node|.
73 void SetDescription(const BookmarkNode* node, const std::string& description);
75 // Returns the description of a bookmark |node|.
76 std::string GetDescription(const BookmarkNode* node);
78 // Sets the URL of an image representative of a bookmark |node|.
79 // Expects the URL to be valid and not empty.
80 // Returns true if the metainfo is successfully populated.
81 bool SetOriginalImage(const BookmarkNode* node,
82 const GURL& url,
83 int width,
84 int height);
86 // Returns the url and dimensions of the original scraped image of a
87 // bookmark |node|.
88 // Returns true if the out variables are populated, false otherwise.
89 bool GetOriginalImage(const BookmarkNode* node,
90 GURL* url,
91 int* width,
92 int* height);
94 // Returns the url and dimensions of the server provided thumbnail image for
95 // a given bookmark |node|.
96 // Returns true if the out variables are populated, false otherwise.
97 bool GetThumbnailImage(const BookmarkNode* node,
98 GURL* url,
99 int* width,
100 int* height);
102 // Returns a brief server provided synopsis of the bookmarked page.
103 // Returns the empty string if the snippet could not be extracted.
104 std::string GetSnippet(const BookmarkNode* node);
106 // Sets a custom suffix to be added to the version field when writing meta
107 // info fields.
108 void SetVersionSuffix(const std::string& version_suffix);
110 // TODO(rfevang): Add method + enum for accessing/writing flags.
112 // Used for testing, simulates the process that creates the thumbnails. Will
113 // remove existing entries for empty urls or set them if the url is not empty.
114 // Expects valid or empty urls. Returns true if the metainfo is successfully
115 // populated.
116 // TODO(rfevang): Move this to a testing only utility file.
117 bool SetAllImages(const BookmarkNode* node,
118 const GURL& image_url,
119 int image_width,
120 int image_height,
121 const GURL& thumbnail_url,
122 int thumbnail_width,
123 int thumbnail_height);
125 // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
126 // Remove when that is actually the case.
127 BookmarkModel* bookmark_model() { return bookmark_model_; }
129 // Returns true if the enhanced bookmark model is done loading.
130 bool loaded() { return loaded_; }
132 // Returns the version string to use when setting stars.version.
133 std::string GetVersionString();
135 private:
136 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo);
138 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap;
139 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap;
141 // BaseBookmarkModelObserver:
142 void BookmarkModelChanged() override;
143 void BookmarkModelLoaded(BookmarkModel* model, bool ids_reassigned) override;
144 void BookmarkNodeAdded(BookmarkModel* model,
145 const BookmarkNode* parent,
146 int index) override;
147 void BookmarkNodeRemoved(BookmarkModel* model,
148 const BookmarkNode* parent,
149 int old_index,
150 const BookmarkNode* node,
151 const std::set<GURL>& removed_urls) override;
152 void BookmarkNodeChanged(BookmarkModel* model,
153 const BookmarkNode* node) override;
154 void OnWillChangeBookmarkMetaInfo(BookmarkModel* model,
155 const BookmarkNode* node) override;
156 void BookmarkMetaInfoChanged(BookmarkModel* model,
157 const BookmarkNode* node) override;
158 void BookmarkAllUserNodesRemoved(BookmarkModel* model,
159 const std::set<GURL>& removed_urls) override;
161 // Initialize the mapping from remote ids to nodes.
162 void InitializeIdMap();
164 // Adds a node to the id map if it has a (unique) remote id. Must be followed
165 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes.
166 void AddToIdMap(const BookmarkNode* node);
168 // If there are nodes that needs to reset their remote ids, schedules
169 // ResetDuplicateRemoteIds to be run asynchronously.
170 void ScheduleResetDuplicateRemoteIds();
172 // Clears out any duplicate remote ids detected by AddToIdMap calls.
173 void ResetDuplicateRemoteIds();
175 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node.
176 void SetNeedsOfflineProcessing(const BookmarkNode* node);
178 // Helper method for setting a meta info field on a node. Also updates the
179 // version field.
180 void SetMetaInfo(const BookmarkNode* node,
181 const std::string& field,
182 const std::string& value);
184 // Helper method for setting multiple meta info fields at once. All the fields
185 // in |meta_info| will be set, but the method will not delete fields not
186 // present.
187 void SetMultipleMetaInfo(const BookmarkNode* node,
188 BookmarkNode::MetaInfoMap meta_info);
190 BookmarkModel* bookmark_model_;
191 bool loaded_;
193 ObserverList<EnhancedBookmarkModelObserver> observers_;
195 IdToNodeMap id_map_;
196 NodeToIdMap nodes_to_reset_;
198 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need
199 // to be cancelled if the node is removed.
200 std::map<const BookmarkNode*, linked_ptr<base::CancelableClosure>>
201 set_needs_offline_processing_tasks_;
203 // Caches the remote id of a node before its meta info changes.
204 std::string prev_remote_id_;
206 std::string version_;
207 std::string version_suffix_;
209 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_;
212 } // namespace enhanced_bookmarks
214 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_