ozone: fix HDPMLegacy - do the PF after overlays, also clear old overlays
[chromium-blink-merge.git] / components / enhanced_bookmarks / enhanced_bookmark_model.h
blob7eab2665b1c881959bf1ddbd3000f563493882ee
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 class BookmarkNode;
21 class GURL;
23 namespace base {
24 class Time;
27 namespace bookmarks {
28 class BookmarkModel;
31 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo);
33 namespace enhanced_bookmarks {
35 class EnhancedBookmarkModelObserver;
37 // Wrapper around BookmarkModel providing utility functions for enhanced
38 // bookmarks.
39 class EnhancedBookmarkModel : public KeyedService,
40 public bookmarks::BaseBookmarkModelObserver {
41 public:
42 EnhancedBookmarkModel(bookmarks::BookmarkModel* bookmark_model,
43 const std::string& version);
44 ~EnhancedBookmarkModel() override;
46 void Shutdown() override;
48 void AddObserver(EnhancedBookmarkModelObserver* observer);
49 void RemoveObserver(EnhancedBookmarkModelObserver* observer);
51 // Moves |node| to |new_parent| and inserts it at the given |index|.
52 void Move(const BookmarkNode* node,
53 const BookmarkNode* new_parent,
54 int index);
56 // Adds a new folder node at the specified position.
57 const BookmarkNode* AddFolder(const BookmarkNode* parent,
58 int index,
59 const base::string16& title);
61 // Adds a url at the specified position.
62 const BookmarkNode* AddURL(const BookmarkNode* parent,
63 int index,
64 const base::string16& title,
65 const GURL& url,
66 const base::Time& creation_time);
68 // Returns the remote id for a bookmark |node|.
69 std::string GetRemoteId(const BookmarkNode* node);
71 // Returns the bookmark node corresponding to the given |remote_id|, or NULL
72 // if there is no node with the id.
73 const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id);
75 // Sets the description of a bookmark |node|.
76 void SetDescription(const BookmarkNode* node, const std::string& description);
78 // Returns the description of a bookmark |node|.
79 std::string GetDescription(const BookmarkNode* node);
81 // Sets the URL of an image representative of a bookmark |node|.
82 // Expects the URL to be valid and not empty.
83 // Returns true if the metainfo is successfully populated.
84 bool SetOriginalImage(const BookmarkNode* node,
85 const GURL& url,
86 int width,
87 int height);
89 // Returns the url and dimensions of the original scraped image of a
90 // bookmark |node|.
91 // Returns true if the out variables are populated, false otherwise.
92 bool GetOriginalImage(const BookmarkNode* node,
93 GURL* url,
94 int* width,
95 int* height);
97 // Returns the url and dimensions of the server provided thumbnail image for
98 // a given bookmark |node|.
99 // Returns true if the out variables are populated, false otherwise.
100 bool GetThumbnailImage(const BookmarkNode* node,
101 GURL* url,
102 int* width,
103 int* height);
105 // Returns a brief server provided synopsis of the bookmarked page.
106 // Returns the empty string if the snippet could not be extracted.
107 std::string GetSnippet(const BookmarkNode* node);
109 // Sets a custom suffix to be added to the version field when writing meta
110 // info fields.
111 void SetVersionSuffix(const std::string& version_suffix);
113 // TODO(rfevang): Add method + enum for accessing/writing flags.
115 // Used for testing, simulates the process that creates the thumbnails. Will
116 // remove existing entries for empty urls or set them if the url is not empty.
117 // Expects valid or empty urls. Returns true if the metainfo is successfully
118 // populated.
119 // TODO(rfevang): Move this to a testing only utility file.
120 bool SetAllImages(const BookmarkNode* node,
121 const GURL& image_url,
122 int image_width,
123 int image_height,
124 const GURL& thumbnail_url,
125 int thumbnail_width,
126 int thumbnail_height);
128 // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
129 // Remove when that is actually the case.
130 bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; }
132 // Returns true if the enhanced bookmark model is done loading.
133 bool loaded() { return loaded_; }
135 // Returns the version string to use when setting stars.version.
136 std::string GetVersionString();
138 private:
139 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo);
141 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap;
142 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap;
144 // bookmarks::BaseBookmarkModelObserver:
145 void BookmarkModelChanged() override;
146 void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
147 bool ids_reassigned) override;
148 void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
149 const BookmarkNode* parent,
150 int index) override;
151 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
152 const BookmarkNode* parent,
153 int old_index,
154 const BookmarkNode* node,
155 const std::set<GURL>& removed_urls) override;
156 void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
157 const BookmarkNode* node) override;
158 void OnWillChangeBookmarkMetaInfo(bookmarks::BookmarkModel* model,
159 const BookmarkNode* node) override;
160 void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
161 const BookmarkNode* node) override;
162 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
163 const std::set<GURL>& removed_urls) override;
165 // Initialize the mapping from remote ids to nodes.
166 void InitializeIdMap();
168 // Adds a node to the id map if it has a (unique) remote id. Must be followed
169 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes.
170 void AddToIdMap(const BookmarkNode* node);
172 // Recursively removes a node and all its children from the various maps.
173 void RemoveNodeFromMaps(const BookmarkNode* node);
175 // If there are nodes that needs to reset their remote ids, schedules
176 // ResetDuplicateRemoteIds to be run asynchronously.
177 void ScheduleResetDuplicateRemoteIds();
179 // Clears out any duplicate remote ids detected by AddToIdMap calls.
180 void ResetDuplicateRemoteIds();
182 // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node.
183 void SetNeedsOfflineProcessing(const BookmarkNode* node);
185 // Helper method for setting a meta info field on a node. Also updates the
186 // version field.
187 void SetMetaInfo(const BookmarkNode* node,
188 const std::string& field,
189 const std::string& value);
191 // Helper method for setting multiple meta info fields at once. All the fields
192 // in |meta_info| will be set, but the method will not delete fields not
193 // present.
194 void SetMultipleMetaInfo(const BookmarkNode* node,
195 BookmarkNode::MetaInfoMap meta_info);
197 bookmarks::BookmarkModel* bookmark_model_;
198 bool loaded_;
200 ObserverList<EnhancedBookmarkModelObserver> observers_;
202 IdToNodeMap id_map_;
203 NodeToIdMap nodes_to_reset_;
205 // Pending SetNeedsOfflineProcessing calls are stored here, as they may need
206 // to be cancelled if the node is removed.
207 std::map<const BookmarkNode*, linked_ptr<base::CancelableClosure>>
208 set_needs_offline_processing_tasks_;
210 // Caches the remote id of a node before its meta info changes.
211 std::string prev_remote_id_;
213 std::string version_;
214 std::string version_suffix_;
216 base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_;
219 } // namespace enhanced_bookmarks
221 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_