Mass-rename of BookmarkModel::RemoveAll().
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_model.h
blob9e2a8be68c1037bbf32be76c76c0c8ffc8d9f418
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_BOOKMARKS_BROWSER_BOOKMARK_MODEL_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_MODEL_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/strings/string16.h"
18 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "components/bookmarks/browser/bookmark_client.h"
21 #include "components/bookmarks/browser/bookmark_node.h"
22 #include "ui/gfx/image/image.h"
23 #include "url/gurl.h"
25 class BookmarkExpandedStateTracker;
26 class BookmarkModelObserver;
27 struct BookmarkMatch;
28 class PrefService;
29 class ScopedGroupBookmarkActions;
31 namespace base {
32 class FilePath;
33 class SequencedTaskRunner;
36 namespace bookmarks {
37 class BookmarkIndex;
38 class BookmarkLoadDetails;
39 class BookmarkStorage;
40 class ScopedGroupBookmarkActions;
43 namespace favicon_base {
44 struct FaviconImageResult;
47 namespace test {
48 class TestBookmarkClient;
51 // BookmarkModel --------------------------------------------------------------
53 // BookmarkModel provides a directed acyclic graph of URLs and folders.
54 // Three graphs are provided for the three entry points: those on the 'bookmarks
55 // bar', those in the 'other bookmarks' folder and those in the 'mobile' folder.
57 // An observer may be attached to observe relevant events.
59 // You should NOT directly create a BookmarkModel, instead go through the
60 // BookmarkModelFactory.
61 class BookmarkModel {
62 public:
63 struct URLAndTitle {
64 GURL url;
65 base::string16 title;
68 // |index_urls| says whether URLs should be stored in the BookmarkIndex
69 // in addition to bookmark titles.
70 BookmarkModel(BookmarkClient* client, bool index_urls);
71 ~BookmarkModel();
73 // Invoked prior to destruction to release any necessary resources.
74 void Shutdown();
76 // Loads the bookmarks. This is called upon creation of the
77 // BookmarkModel. You need not invoke this directly.
78 // All load operations will be executed on |io_task_runner| and the completion
79 // callback will be called from |ui_task_runner|.
80 void Load(PrefService* pref_service,
81 const std::string& accept_languages,
82 const base::FilePath& profile_path,
83 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner,
84 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner);
86 // Returns true if the model finished loading.
87 bool loaded() const { return loaded_; }
89 // Returns the root node. The 'bookmark bar' node and 'other' node are
90 // children of the root node.
91 const BookmarkNode* root_node() const { return &root_; }
93 // Returns the 'bookmark bar' node. This is NULL until loaded.
94 const BookmarkNode* bookmark_bar_node() const { return bookmark_bar_node_; }
96 // Returns the 'other' node. This is NULL until loaded.
97 const BookmarkNode* other_node() const { return other_node_; }
99 // Returns the 'mobile' node. This is NULL until loaded.
100 const BookmarkNode* mobile_node() const { return mobile_node_; }
102 bool is_root_node(const BookmarkNode* node) const { return node == &root_; }
104 // Returns whether the given |node| is one of the permanent nodes - root node,
105 // 'bookmark bar' node, 'other' node or 'mobile' node, or one of the root
106 // nodes supplied by the |client_|.
107 bool is_permanent_node(const BookmarkNode* node) const {
108 return node && (node == &root_ || node->parent() == &root_);
111 // Returns the parent the last node was added to. This never returns NULL
112 // (as long as the model is loaded).
113 const BookmarkNode* GetParentForNewNodes();
115 void AddObserver(BookmarkModelObserver* observer);
116 void RemoveObserver(BookmarkModelObserver* observer);
118 // Notifies the observers that an extensive set of changes is about to happen,
119 // such as during import or sync, so they can delay any expensive UI updates
120 // until it's finished.
121 void BeginExtensiveChanges();
122 void EndExtensiveChanges();
124 // Returns true if this bookmark model is currently in a mode where extensive
125 // changes might happen, such as for import and sync. This is helpful for
126 // observers that are created after the mode has started, and want to check
127 // state during their own initializer, such as the NTP.
128 bool IsDoingExtensiveChanges() const { return extensive_changes_ > 0; }
130 // Removes the node at the given |index| from |parent|. Removing a folder node
131 // recursively removes all nodes. Observers are notified immediately.
132 void Remove(const BookmarkNode* parent, int index);
134 // Removes all the non-permanent bookmark nodes that are editable by the user.
135 // Observers are only notified when all nodes have been removed. There is no
136 // notification for individual node removals.
137 void RemoveAllUserBookmarks();
139 // DEPRECATED: use RemoveAllUserBookmarks instead. This method will be
140 // removed. http://crbug.com/49598
141 void RemoveAll();
143 // Moves |node| to |new_parent| and inserts it at the given |index|.
144 void Move(const BookmarkNode* node,
145 const BookmarkNode* new_parent,
146 int index);
148 // Inserts a copy of |node| into |new_parent| at |index|.
149 void Copy(const BookmarkNode* node,
150 const BookmarkNode* new_parent,
151 int index);
153 // Returns the favicon for |node|. If the favicon has not yet been
154 // loaded it is loaded and the observer of the model notified when done.
155 const gfx::Image& GetFavicon(const BookmarkNode* node);
157 // Returns the type of the favicon for |node|. If the favicon has not yet
158 // been loaded, it returns |favicon_base::INVALID_ICON|.
159 favicon_base::IconType GetFaviconType(const BookmarkNode* node);
161 // Sets the title of |node|.
162 void SetTitle(const BookmarkNode* node, const base::string16& title);
164 // Sets the URL of |node|.
165 void SetURL(const BookmarkNode* node, const GURL& url);
167 // Sets the date added time of |node|.
168 void SetDateAdded(const BookmarkNode* node, base::Time date_added);
170 // Returns the set of nodes with the |url|.
171 void GetNodesByURL(const GURL& url, std::vector<const BookmarkNode*>* nodes);
173 // Returns the most recently added user node for the |url|; urls from any
174 // nodes that are not editable by the user are never returned by this call.
175 // Returns NULL if |url| is not bookmarked.
176 const BookmarkNode* GetMostRecentlyAddedUserNodeForURL(const GURL& url);
178 // Returns true if there are bookmarks, otherwise returns false.
179 // This method is thread safe.
180 bool HasBookmarks();
182 // Returns true if the specified URL is bookmarked.
184 // If not on the main thread you *must* invoke BlockTillLoaded first.
185 bool IsBookmarked(const GURL& url);
187 // Returns, by reference in |bookmarks|, the set of bookmarked urls and their
188 // titles. This returns the unique set of URLs. For example, if two bookmarks
189 // reference the same URL only one entry is added not matter the titles are
190 // same or not.
192 // If not on the main thread you *must* invoke BlockTillLoaded first.
193 void GetBookmarks(std::vector<BookmarkModel::URLAndTitle>* urls);
195 // Blocks until loaded. This is intended for usage on a thread other than
196 // the main thread.
197 void BlockTillLoaded();
199 // Adds a new folder node at the specified position.
200 const BookmarkNode* AddFolder(const BookmarkNode* parent,
201 int index,
202 const base::string16& title);
204 // Adds a new folder with meta info.
205 const BookmarkNode* AddFolderWithMetaInfo(
206 const BookmarkNode* parent,
207 int index,
208 const base::string16& title,
209 const BookmarkNode::MetaInfoMap* meta_info);
211 // Adds a url at the specified position.
212 const BookmarkNode* AddURL(const BookmarkNode* parent,
213 int index,
214 const base::string16& title,
215 const GURL& url);
217 // Adds a url with a specific creation date and meta info.
218 const BookmarkNode* AddURLWithCreationTimeAndMetaInfo(
219 const BookmarkNode* parent,
220 int index,
221 const base::string16& title,
222 const GURL& url,
223 const base::Time& creation_time,
224 const BookmarkNode::MetaInfoMap* meta_info);
226 // Sorts the children of |parent|, notifying observers by way of the
227 // BookmarkNodeChildrenReordered method.
228 void SortChildren(const BookmarkNode* parent);
230 // Order the children of |parent| as specified in |ordered_nodes|. This
231 // function should only be used to reorder the child nodes of |parent| and
232 // is not meant to move nodes between different parent. Notifies observers
233 // using the BookmarkNodeChildrenReordered method.
234 void ReorderChildren(const BookmarkNode* parent,
235 const std::vector<const BookmarkNode*>& ordered_nodes);
237 // Sets the date when the folder was modified.
238 void SetDateFolderModified(const BookmarkNode* node, const base::Time time);
240 // Resets the 'date modified' time of the node to 0. This is used during
241 // importing to exclude the newly created folders from showing up in the
242 // combobox of most recently modified folders.
243 void ResetDateFolderModified(const BookmarkNode* node);
245 // Returns up to |max_count| of bookmarks containing each term from |text|
246 // in either the title or the URL.
247 void GetBookmarksMatching(
248 const base::string16& text,
249 size_t max_count,
250 std::vector<BookmarkMatch>* matches);
252 // Sets the store to NULL, making it so the BookmarkModel does not persist
253 // any changes to disk. This is only useful during testing to speed up
254 // testing.
255 void ClearStore();
257 // Returns the next node ID.
258 int64 next_node_id() const { return next_node_id_; }
260 // Returns the object responsible for tracking the set of expanded nodes in
261 // the bookmark editor.
262 BookmarkExpandedStateTracker* expanded_state_tracker() {
263 return expanded_state_tracker_.get();
266 // Sets the visibility of one of the permanent nodes (unless the node must
267 // always be visible, see |BookmarkClient::IsPermanentNodeVisible| for more
268 // details). This is set by sync.
269 void SetPermanentNodeVisible(BookmarkNode::Type type, bool value);
271 // Returns the permanent node of type |type|.
272 const BookmarkPermanentNode* PermanentNode(BookmarkNode::Type type);
274 // Sets/deletes meta info of |node|.
275 void SetNodeMetaInfo(const BookmarkNode* node,
276 const std::string& key,
277 const std::string& value);
278 void SetNodeMetaInfoMap(const BookmarkNode* node,
279 const BookmarkNode::MetaInfoMap& meta_info_map);
280 void DeleteNodeMetaInfo(const BookmarkNode* node,
281 const std::string& key);
283 // Sets the sync transaction version of |node|.
284 void SetNodeSyncTransactionVersion(const BookmarkNode* node,
285 int64 sync_transaction_version);
287 // Notify BookmarkModel that the favicons for |urls| have changed and have to
288 // be refetched. This notification is sent by BookmarkClient.
289 void OnFaviconChanged(const std::set<GURL>& urls);
291 // Returns the client used by this BookmarkModel.
292 BookmarkClient* client() const { return client_; }
294 private:
295 friend class BookmarkCodecTest;
296 friend class BookmarkModelTest;
297 friend class bookmarks::BookmarkStorage;
298 friend class bookmarks::ScopedGroupBookmarkActions;
299 friend class test::TestBookmarkClient;
301 // Used to order BookmarkNodes by URL.
302 class NodeURLComparator {
303 public:
304 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const {
305 return n1->url() < n2->url();
309 // Implementation of IsBookmarked. Before calling this the caller must obtain
310 // a lock on |url_lock_|.
311 bool IsBookmarkedNoLock(const GURL& url);
313 // Removes the node from internal maps and recurses through all children. If
314 // the node is a url, its url is added to removed_urls.
316 // This does NOT delete the node.
317 void RemoveNode(BookmarkNode* node, std::set<GURL>* removed_urls);
319 // Invoked when loading is finished. Sets |loaded_| and notifies observers.
320 // BookmarkModel takes ownership of |details|.
321 void DoneLoading(scoped_ptr<bookmarks::BookmarkLoadDetails> details);
323 // Populates |nodes_ordered_by_url_set_| from root.
324 void PopulateNodesByURL(BookmarkNode* node);
326 // Removes the node from its parent, but does not delete it. No notifications
327 // are sent. |removed_urls| is populated with the urls which no longer have
328 // any bookmarks associated with them.
329 // This method should be called after acquiring |url_lock_|.
330 void RemoveNodeAndGetRemovedUrls(BookmarkNode* node,
331 std::set<GURL>* removed_urls);
333 // Removes the node from its parent, sends notification, and deletes it.
334 // type specifies how the node should be removed.
335 void RemoveAndDeleteNode(BookmarkNode* delete_me);
337 // Remove |node| from |nodes_ordered_by_url_set_|.
338 void RemoveNodeFromURLSet(BookmarkNode* node);
340 // Adds the |node| at |parent| in the specified |index| and notifies its
341 // observers.
342 BookmarkNode* AddNode(BookmarkNode* parent,
343 int index,
344 BookmarkNode* node);
346 // Returns true if the parent and index are valid.
347 bool IsValidIndex(const BookmarkNode* parent, int index, bool allow_end);
349 // Creates one of the possible permanent nodes (bookmark bar node, other node
350 // and mobile node) from |type|.
351 BookmarkPermanentNode* CreatePermanentNode(BookmarkNode::Type type);
353 // Notification that a favicon has finished loading. If we can decode the
354 // favicon, FaviconLoaded is invoked.
355 void OnFaviconDataAvailable(
356 BookmarkNode* node,
357 favicon_base::IconType icon_type,
358 const favicon_base::FaviconImageResult& image_result);
360 // Invoked from the node to load the favicon. Requests the favicon from the
361 // favicon service.
362 void LoadFavicon(BookmarkNode* node, favicon_base::IconType icon_type);
364 // Called to notify the observers that the favicon has been loaded.
365 void FaviconLoaded(const BookmarkNode* node);
367 // If we're waiting on a favicon for node, the load request is canceled.
368 void CancelPendingFaviconLoadRequests(BookmarkNode* node);
370 // Notifies the observers that a set of changes initiated by a single user
371 // action is about to happen and has completed.
372 void BeginGroupedChanges();
373 void EndGroupedChanges();
375 // Generates and returns the next node ID.
376 int64 generate_next_node_id();
378 // Sets the maximum node ID to the given value.
379 // This is used by BookmarkCodec to report the maximum ID after it's done
380 // decoding since during decoding codec assigns node IDs.
381 void set_next_node_id(int64 id) { next_node_id_ = id; }
383 // Creates and returns a new BookmarkLoadDetails. It's up to the caller to
384 // delete the returned object.
385 scoped_ptr<bookmarks::BookmarkLoadDetails> CreateLoadDetails(
386 const std::string& accept_languages);
388 BookmarkClient* const client_;
390 // Whether the initial set of data has been loaded.
391 bool loaded_;
393 // The root node. This contains the bookmark bar node, the 'other' node and
394 // the mobile node as children.
395 BookmarkNode root_;
397 BookmarkPermanentNode* bookmark_bar_node_;
398 BookmarkPermanentNode* other_node_;
399 BookmarkPermanentNode* mobile_node_;
401 // The maximum ID assigned to the bookmark nodes in the model.
402 int64 next_node_id_;
404 // The observers.
405 ObserverList<BookmarkModelObserver> observers_;
407 // Set of nodes ordered by URL. This is not a map to avoid copying the
408 // urls.
409 // WARNING: |nodes_ordered_by_url_set_| is accessed on multiple threads. As
410 // such, be sure and wrap all usage of it around |url_lock_|.
411 typedef std::multiset<BookmarkNode*, NodeURLComparator> NodesOrderedByURLSet;
412 NodesOrderedByURLSet nodes_ordered_by_url_set_;
413 base::Lock url_lock_;
415 // Used for loading favicons.
416 base::CancelableTaskTracker cancelable_task_tracker_;
418 // Reads/writes bookmarks to disk.
419 scoped_refptr<bookmarks::BookmarkStorage> store_;
421 scoped_ptr<bookmarks::BookmarkIndex> index_;
423 // True if URLs are stored in the BookmarkIndex in addition to bookmark
424 // titles.
425 const bool index_urls_;
427 base::WaitableEvent loaded_signal_;
429 // See description of IsDoingExtensiveChanges above.
430 int extensive_changes_;
432 scoped_ptr<BookmarkExpandedStateTracker> expanded_state_tracker_;
434 DISALLOW_COPY_AND_ASSIGN(BookmarkModel);
437 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_MODEL_H_