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