OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_TAG_MODEL_H_ |
| 6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_TAG_MODEL_H_ |
| 7 |
| 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model_observer.h" |
| 10 |
| 11 class BookmarkTagModelObserver; |
| 12 |
| 13 typedef string16 BookmarkTag; |
| 14 |
| 15 // BookmarTagModel provides a way to access and manipulate bookmarks in a |
| 16 // non-hierarchical way. BookmarkTagModel view the bookmarks as a flat list, |
| 17 // and each one can be marked with a collection of tags (tags are simply |
| 18 // strings). |
| 19 // |
| 20 // BookmarkTagModel converts on demand the data from an existing BookmarkModel |
| 21 // to its view of the world by considering all the titles of all the ancestors |
| 22 // as tags. This view is frozen on an individual bookmarks when the |
| 23 // BookmarkTagModel performs a change on the tags of this bookmarks. |
| 24 // |
| 25 // The Bookmark's meta info is used for storage. |
| 26 // |
| 27 // An observer may be attached to a BookmarkTagModel to observe relevant events. |
| 28 // |
| 29 // TODO(noyau): The meta info data is not preserved by copy/paste or drag and |
| 30 // drop, this means that bookmarks will loose their tag information if |
| 31 // manipulated that way. |
| 32 // |
| 33 class BookmarkTagModel : public BookmarkModelObserver { |
| 34 public: |
| 35 explicit BookmarkTagModel(BookmarkModel* bookmark_model); |
| 36 virtual ~BookmarkTagModel(); |
| 37 |
| 38 // Returns true if the model finished loading. |
| 39 bool loaded() const { return loaded_; } |
| 40 |
| 41 // Add and remove observers on this object. |
| 42 void AddObserver(BookmarkTagModelObserver* observer); |
| 43 void RemoveObserver(BookmarkTagModelObserver* observer); |
| 44 |
| 45 // Brackets an extensive set of changes, such as during import or sync, so |
| 46 // observers can delay any expensive UI updates until it's finished. |
| 47 class ExtensiveChanges { |
| 48 public: |
| 49 friend class BookmarkTagModel; |
| 50 explicit ExtensiveChanges(BookmarkTagModel* model) : model_(model) { |
| 51 model_->BeginExtensiveChanges(); |
| 52 } |
| 53 private: |
| 54 ~ExtensiveChanges() { |
| 55 model_->EndExtensiveChanges(); |
| 56 } |
| 57 |
| 58 BookmarkTagModel* model_; |
| 59 }; |
| 60 |
| 61 // Returns true if this bookmark model is currently in a mode where extensive |
| 62 // changes might happen, such as for import and sync. This is helpful for |
| 63 // observers that are created after the mode has started, and want to check |
| 64 // state during their own initializer. |
| 65 bool IsDoingExtensiveChanges() const; |
| 66 |
| 67 // Removes the given |BookmarkNode|. Observers are notified immediately. |
| 68 void Remove(const BookmarkNode* bookmark); |
| 69 |
| 70 // Removes all the bookmark nodes. Observers are only notified when all nodes |
| 71 // have been removed. There is no notification for individual node removals. |
| 72 void RemoveAll(); |
| 73 |
| 74 // Returns the favicon for |node|. If the favicon has not yet been |
| 75 // loaded it is loaded and the observer of the model notified when done. |
| 76 const gfx::Image& GetFavicon(const BookmarkNode* bookmark); |
| 77 |
| 78 // Sets the title of |node|. |
| 79 void SetTitle(const BookmarkNode* bookmark, const string16& title); |
| 80 |
| 81 // Sets the URL of |node|. |
| 82 void SetURL(const BookmarkNode* bookmark, const GURL& url); |
| 83 |
| 84 // Sets the date added time of |node|. |
| 85 void SetDateAdded(const BookmarkNode* bookmark, base::Time date_added); |
| 86 |
| 87 // Returns the most recently added bookmark for the |url|. Returns NULL if |
| 88 // |url| is not bookmarked. |
| 89 const BookmarkNode* GetMostRecentlyAddedBookmarkForURL(const GURL& url); |
| 90 |
| 91 // Creates a new bookmark. |
| 92 const BookmarkNode* AddURL(const string16& title, |
| 93 const GURL& url, |
| 94 const std::set<BookmarkTag>& tags); |
| 95 |
| 96 // Adds the |tags| to the tag list of the |bookmark|. |
| 97 void AddTagsToBookmark(const std::set<BookmarkTag>& tags, |
| 98 const BookmarkNode* bookmark); |
| 99 |
| 100 // Same but to a whole collection of |bookmarks|. |
| 101 void AddTagsToBookmarks(const std::set<BookmarkTag>& tags, |
| 102 const std::set<const BookmarkNode*>& bookmarks); |
| 103 |
| 104 // Remove the |tags| from the tag list of the |bookmark|. If the bookmark |
| 105 // is not tagged with one or more of the tags, these are ignored. |
| 106 void RemoveTagsFromBookmark(const std::set<BookmarkTag>& tags, |
| 107 const BookmarkNode* bookmark); |
| 108 |
| 109 // Same but to a whole collection of |bookmarks|. |
| 110 void RemoveTagsFromBookmarks(const std::set<BookmarkTag>& tags, |
| 111 const std::set<const BookmarkNode*>& bookmarks); |
| 112 |
| 113 // Returns all the tags set on a specific |bookmark|. |
| 114 std::set<BookmarkTag> GetTagsForBookmark(const BookmarkNode* bookmark); |
| 115 |
| 116 // Returns the bookmarks marked with all the given |tags|. |
| 117 std::set<const BookmarkNode*> BookmarksForTags( |
| 118 const std::set<BookmarkTag>& tags); |
| 119 |
| 120 // Returns the bookmarks marked with the given |tag|. |
| 121 std::set<const BookmarkNode*> BookmarksForTag(const BookmarkTag& tag); |
| 122 |
| 123 // Returns all tags related to the parent |tag|. If |tag| is null this method |
| 124 // will returns and sort all tags in the system. A related tag is a tag used |
| 125 // on one or more of the bookmarks tagged with |tag|. The returned tags are |
| 126 // ordered from the most common to the rarest one. |
| 127 std::vector<BookmarkTag> TagsRelatedToTag(const BookmarkTag& tag); |
| 128 |
| 129 // All the BookmarkModelObserver methods. See there for details. |
| 130 virtual void Loaded(BookmarkModel* model, bool ids_reassigned) OVERRIDE; |
| 131 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE; |
| 132 virtual void BookmarkNodeMoved(BookmarkModel* model, |
| 133 const BookmarkNode* old_parent, |
| 134 int old_index, |
| 135 const BookmarkNode* new_parent, |
| 136 int new_index) OVERRIDE; |
| 137 virtual void BookmarkNodeAdded(BookmarkModel* model, |
| 138 const BookmarkNode* parent, |
| 139 int index) OVERRIDE; |
| 140 virtual void OnWillRemoveBookmarks(BookmarkModel* model, |
| 141 const BookmarkNode* parent, |
| 142 int old_index, |
| 143 const BookmarkNode* node) OVERRIDE; |
| 144 virtual void BookmarkNodeRemoved(BookmarkModel* model, |
| 145 const BookmarkNode* parent, |
| 146 int old_index, |
| 147 const BookmarkNode* node) OVERRIDE; |
| 148 virtual void OnWillChangeBookmarkNode(BookmarkModel* model, |
| 149 const BookmarkNode* node) OVERRIDE; |
| 150 virtual void BookmarkNodeChanged(BookmarkModel* model, |
| 151 const BookmarkNode* node) OVERRIDE; |
| 152 virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel* model, |
| 153 const BookmarkNode* node) OVERRIDE; |
| 154 virtual void BookmarkMetaInfoChanged(BookmarkModel* model, |
| 155 const BookmarkNode* node) OVERRIDE; |
| 156 virtual void BookmarkNodeFaviconChanged(BookmarkModel* model, |
| 157 const BookmarkNode* node) OVERRIDE; |
| 158 virtual void OnWillReorderBookmarkNode(BookmarkModel* model, |
| 159 const BookmarkNode* node) OVERRIDE; |
| 160 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model, |
| 161 const BookmarkNode* node) OVERRIDE; |
| 162 virtual void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE; |
| 163 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE; |
| 164 virtual void OnWillRemoveAllBookmarks(BookmarkModel* model) OVERRIDE; |
| 165 virtual void BookmarkAllNodesRemoved(BookmarkModel* model) OVERRIDE; |
| 166 |
| 167 private: |
| 168 // Notifies the observers that an extensive set of changes is about to happen, |
| 169 // such as during import or sync, so they can delay any expensive UI updates |
| 170 // until it's finished. |
| 171 void BeginExtensiveChanges(); |
| 172 void EndExtensiveChanges(); |
| 173 |
| 174 // Encode the tags in a format suitable for the BookmarkNode's metaInfo and |
| 175 // set or replace the value. |
| 176 void SetTagsOnBookmark(const std::set<BookmarkTag>& tags, |
| 177 const BookmarkNode* bookmark); |
| 178 |
| 179 // Build the caches of tag to bookmarks and bookmarks to tag for faster |
| 180 // access to the data. Load() is called from the constructor if possible, or |
| 181 // as soon as possible after that. |
| 182 void Load(); |
| 183 |
| 184 // Discard tag information for all descendants of a given folder node and |
| 185 // rebuild the cache for them. |
| 186 void ReloadDescendants(const BookmarkNode* folder); |
| 187 |
| 188 // Clear the local cache of all mentions of |bookmark|. |
| 189 void RemoveBookmark(const BookmarkNode* bookmark); |
| 190 |
| 191 // Extract the tags from |bookmark| and insert it in the local cache. |
| 192 void LoadBookmark(const BookmarkNode* bookmark); |
| 193 |
| 194 // The model from where the data is permanently stored. |
| 195 BookmarkModel* bookmark_model_; |
| 196 |
| 197 // True if the model is fully loaded. |
| 198 bool loaded_; |
| 199 |
| 200 // The observers. |
| 201 ObserverList<BookmarkTagModelObserver> observers_; |
| 202 |
| 203 // Local cache for quick access. |
| 204 std::map<const BookmarkTag, std::set<const BookmarkNode*> > tag_to_bookmarks_; |
| 205 std::map<const BookmarkNode*, std::set<BookmarkTag> > bookmark_to_tags_; |
| 206 |
| 207 // Set to true during the creation of a new bookmark in order to send only the |
| 208 // proper notification. |
| 209 bool inhibit_change_notifications_; |
| 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(BookmarkTagModel); |
| 212 }; |
| 213 |
| 214 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_TAG_MODEL_H_ |
OLD | NEW |