| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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_BOOKMARK_BAR_MODEL_H_ | |
| 6 #define CHROME_BROWSER_BOOKMARK_BAR_MODEL_H_ | |
| 7 | |
| 8 #include "base/lock.h" | |
| 9 #include "base/observer_list.h" | |
| 10 #include "base/scoped_handle.h" | |
| 11 #include "chrome/browser/bookmarks/bookmark_service.h" | |
| 12 #include "chrome/browser/bookmark_storage.h" | |
| 13 #include "chrome/browser/cancelable_request.h" | |
| 14 #include "chrome/browser/history/history.h" | |
| 15 #include "chrome/browser/history/history_types.h" | |
| 16 #include "chrome/common/notification_service.h" | |
| 17 #include "chrome/views/tree_node_model.h" | |
| 18 #include "googleurl/src/gurl.h" | |
| 19 #include "skia/include/SkBitmap.h" | |
| 20 | |
| 21 class BookmarkBarModel; | |
| 22 class BookmarkCodec; | |
| 23 class Profile; | |
| 24 | |
| 25 namespace history { | |
| 26 class StarredURLDatabase; | |
| 27 } | |
| 28 | |
| 29 // BookmarkBarNode ------------------------------------------------------------ | |
| 30 | |
| 31 // BookmarkBarNode contains information about a starred entry: title, URL, | |
| 32 // favicon, star id and type. BookmarkBarNodes are returned from a | |
| 33 // BookmarkBarModel. | |
| 34 // | |
| 35 class BookmarkBarNode : public ChromeViews::TreeNode<BookmarkBarNode> { | |
| 36 friend class BookmarkBarModel; | |
| 37 friend class BookmarkCodec; | |
| 38 friend class history::StarredURLDatabase; | |
| 39 FRIEND_TEST(BookmarkBarModelTest, MostRecentlyAddedEntries); | |
| 40 | |
| 41 public: | |
| 42 BookmarkBarNode(BookmarkBarModel* model, const GURL& url); | |
| 43 virtual ~BookmarkBarNode() {} | |
| 44 | |
| 45 // Returns the favicon for the this node. If the favicon has not yet been | |
| 46 // loaded it is loaded and the observer of the model notified when done. | |
| 47 const SkBitmap& GetFavIcon(); | |
| 48 | |
| 49 // Returns the URL. | |
| 50 const GURL& GetURL() const { return url_; } | |
| 51 | |
| 52 // Returns a unique id for this node. | |
| 53 // | |
| 54 // NOTE: this id is only unique for the session and NOT unique across | |
| 55 // sessions. Don't persist it! | |
| 56 int id() const { return id_; } | |
| 57 | |
| 58 // Returns the type of this node. | |
| 59 history::StarredEntry::Type GetType() const { return type_; } | |
| 60 | |
| 61 // Called when the favicon becomes invalid. | |
| 62 void InvalidateFavicon() { | |
| 63 loaded_favicon_ = false; | |
| 64 favicon_ = SkBitmap(); | |
| 65 } | |
| 66 | |
| 67 // Returns the time the bookmark/group was added. | |
| 68 Time date_added() const { return date_added_; } | |
| 69 | |
| 70 // Returns the last time the group was modified. This is only maintained | |
| 71 // for folders (including the bookmark and other folder). | |
| 72 Time date_group_modified() const { return date_group_modified_; } | |
| 73 | |
| 74 // Convenience for testing if this nodes represents a group. A group is | |
| 75 // a node whose type is not URL. | |
| 76 bool is_folder() const { return type_ != history::StarredEntry::URL; } | |
| 77 | |
| 78 // Is this a URL? | |
| 79 bool is_url() const { return type_ == history::StarredEntry::URL; } | |
| 80 | |
| 81 // TODO(sky): Consider adding last visit time here, it'll greatly simplify | |
| 82 // HistoryContentsProvider. | |
| 83 | |
| 84 private: | |
| 85 // Resets the properties of the node from the supplied entry. | |
| 86 void Reset(const history::StarredEntry& entry); | |
| 87 | |
| 88 // The model. This is NULL when created by StarredURLDatabase for migration. | |
| 89 BookmarkBarModel* model_; | |
| 90 | |
| 91 // Unique identifier for this node. | |
| 92 const int id_; | |
| 93 | |
| 94 // Whether the favicon has been loaded. | |
| 95 bool loaded_favicon_; | |
| 96 | |
| 97 // The favicon. | |
| 98 SkBitmap favicon_; | |
| 99 | |
| 100 // If non-zero, it indicates we're loading the favicon and this is the handle | |
| 101 // from the HistoryService. | |
| 102 HistoryService::Handle favicon_load_handle_; | |
| 103 | |
| 104 // The URL. BookmarkBarModel maintains maps off this URL, it is important that | |
| 105 // it not change once the node has been created. | |
| 106 const GURL url_; | |
| 107 | |
| 108 // Type of node. | |
| 109 // TODO(sky): bug 1256202, convert this into a type defined here. | |
| 110 history::StarredEntry::Type type_; | |
| 111 | |
| 112 // Date we were created. | |
| 113 Time date_added_; | |
| 114 | |
| 115 // Time last modified. Only used for groups. | |
| 116 Time date_group_modified_; | |
| 117 | |
| 118 DISALLOW_EVIL_CONSTRUCTORS(BookmarkBarNode); | |
| 119 }; | |
| 120 | |
| 121 | |
| 122 // BookmarkBarModelObserver --------------------------------------------------- | |
| 123 | |
| 124 // Observer for the BookmarkBarModel. | |
| 125 // | |
| 126 class BookmarkBarModelObserver { | |
| 127 public: | |
| 128 // Invoked when the model has finished loading. | |
| 129 virtual void Loaded(BookmarkBarModel* model) = 0; | |
| 130 | |
| 131 // Invoked from the destructor of the BookmarkBarModel. | |
| 132 virtual void BookmarkModelBeingDeleted(BookmarkBarModel* model) { } | |
| 133 | |
| 134 // Invoked when a node has moved. | |
| 135 virtual void BookmarkNodeMoved(BookmarkBarModel* model, | |
| 136 BookmarkBarNode* old_parent, | |
| 137 int old_index, | |
| 138 BookmarkBarNode* new_parent, | |
| 139 int new_index) = 0; | |
| 140 | |
| 141 // Invoked when a node has been added. | |
| 142 virtual void BookmarkNodeAdded(BookmarkBarModel* model, | |
| 143 BookmarkBarNode* parent, | |
| 144 int index) = 0; | |
| 145 | |
| 146 // Invoked when a node has been removed, the item may still be starred though. | |
| 147 virtual void BookmarkNodeRemoved(BookmarkBarModel* model, | |
| 148 BookmarkBarNode* parent, | |
| 149 int index) = 0; | |
| 150 | |
| 151 // Invoked when the title or favicon of a node has changed. | |
| 152 virtual void BookmarkNodeChanged(BookmarkBarModel* model, | |
| 153 BookmarkBarNode* node) = 0; | |
| 154 | |
| 155 // Invoked when a favicon has finished loading. | |
| 156 virtual void BookmarkNodeFavIconLoaded(BookmarkBarModel* model, | |
| 157 BookmarkBarNode* node) = 0; | |
| 158 }; | |
| 159 | |
| 160 // BookmarkBarModel ----------------------------------------------------------- | |
| 161 | |
| 162 // BookmarkBarModel provides a directed acyclic graph of the starred entries | |
| 163 // and groups. Two graphs are provided for the two entry points: those on | |
| 164 // the bookmark bar, and those in the other folder. | |
| 165 // | |
| 166 // The methods of BookmarkBarModel update the internal structure immediately | |
| 167 // and update the backend in the background. | |
| 168 // | |
| 169 // An observer may be attached to observer relevant events. | |
| 170 // | |
| 171 // You should NOT directly create a BookmarkBarModel, instead go through the | |
| 172 // Profile. | |
| 173 | |
| 174 // TODO(sky): rename to BookmarkModel. | |
| 175 class BookmarkBarModel : public NotificationObserver, public BookmarkService { | |
| 176 friend class BookmarkBarNode; | |
| 177 friend class BookmarkBarModelTest; | |
| 178 friend class BookmarkStorage; | |
| 179 | |
| 180 public: | |
| 181 explicit BookmarkBarModel(Profile* profile); | |
| 182 virtual ~BookmarkBarModel(); | |
| 183 | |
| 184 // Loads the bookmarks. This is called by Profile upon creation of the | |
| 185 // BookmarkBarModel. You need not invoke this directly. | |
| 186 void Load(); | |
| 187 | |
| 188 // Returns the root node. The bookmark bar node and other node are children of | |
| 189 // the root node. | |
| 190 BookmarkBarNode* root_node() { return &root_; } | |
| 191 | |
| 192 // Returns the bookmark bar node. | |
| 193 BookmarkBarNode* GetBookmarkBarNode() { return bookmark_bar_node_; } | |
| 194 | |
| 195 // Returns the 'other' node. | |
| 196 BookmarkBarNode* other_node() { return other_node_; } | |
| 197 | |
| 198 // Returns the parent the last node was added to. This never returns NULL | |
| 199 // (as long as the model is loaded). | |
| 200 BookmarkBarNode* GetParentForNewNodes(); | |
| 201 | |
| 202 // Returns a vector containing up to |max_count| of the most recently | |
| 203 // modified groups. This never returns an empty vector. | |
| 204 std::vector<BookmarkBarNode*> GetMostRecentlyModifiedGroups(size_t max_count); | |
| 205 | |
| 206 // Returns the most recently added bookmarks. | |
| 207 void GetMostRecentlyAddedEntries(size_t count, | |
| 208 std::vector<BookmarkBarNode*>* nodes); | |
| 209 | |
| 210 // Used by GetBookmarksMatchingText to return a matching node and the location | |
| 211 // of the match in the title. | |
| 212 struct TitleMatch { | |
| 213 BookmarkBarNode* node; | |
| 214 | |
| 215 // Location of the matching words in the title of the node. | |
| 216 Snippet::MatchPositions match_positions; | |
| 217 }; | |
| 218 | |
| 219 // Returns the bookmarks whose title contains text. At most |max_count| | |
| 220 // matches are returned in |matches|. | |
| 221 void GetBookmarksMatchingText(const std::wstring& text, | |
| 222 size_t max_count, | |
| 223 std::vector<TitleMatch>* matches); | |
| 224 | |
| 225 void AddObserver(BookmarkBarModelObserver* observer) { | |
| 226 observers_.AddObserver(observer); | |
| 227 } | |
| 228 | |
| 229 void RemoveObserver(BookmarkBarModelObserver* observer) { | |
| 230 observers_.RemoveObserver(observer); | |
| 231 } | |
| 232 | |
| 233 // Unstars or deletes the specified entry. Removing a group entry recursively | |
| 234 // unstars all nodes. Observers are notified immediately. | |
| 235 void Remove(BookmarkBarNode* parent, int index); | |
| 236 | |
| 237 // Moves the specified entry to a new location. | |
| 238 void Move(BookmarkBarNode* node, BookmarkBarNode* new_parent, int index); | |
| 239 | |
| 240 // Sets the title of the specified node. | |
| 241 void SetTitle(BookmarkBarNode* node, const std::wstring& title); | |
| 242 | |
| 243 // Returns true if the model finished loading. | |
| 244 bool IsLoaded() { return loaded_; } | |
| 245 | |
| 246 // Returns the node with the specified URL, or NULL if there is no node with | |
| 247 // the specified URL. This method is thread safe. | |
| 248 BookmarkBarNode* GetNodeByURL(const GURL& url); | |
| 249 | |
| 250 // Returns all the bookmarked urls. This method is thread safe. | |
| 251 virtual void GetBookmarks(std::vector<GURL>* urls); | |
| 252 | |
| 253 // Returns true if there is a bookmark for the specified URL. This method is | |
| 254 // thread safe. See BookmarkService for more details on this. | |
| 255 virtual bool IsBookmarked(const GURL& url) { | |
| 256 return GetNodeByURL(url) != NULL; | |
| 257 } | |
| 258 | |
| 259 // Blocks until loaded; this is NOT invoked on the main thread. See | |
| 260 // BookmarkService for more details on this. | |
| 261 virtual void BlockTillLoaded(); | |
| 262 | |
| 263 // Returns the node with the specified id, or NULL if there is no node with | |
| 264 // the specified id. | |
| 265 BookmarkBarNode* GetNodeByID(int id); | |
| 266 | |
| 267 // Adds a new group node at the specified position. | |
| 268 BookmarkBarNode* AddGroup(BookmarkBarNode* parent, | |
| 269 int index, | |
| 270 const std::wstring& title); | |
| 271 | |
| 272 // Adds a url at the specified position. If there is already a node with the | |
| 273 // specified URL, it is moved to the new position. | |
| 274 BookmarkBarNode* AddURL(BookmarkBarNode* parent, | |
| 275 int index, | |
| 276 const std::wstring& title, | |
| 277 const GURL& url); | |
| 278 | |
| 279 // Adds a url with a specific creation date. | |
| 280 BookmarkBarNode* AddURLWithCreationTime(BookmarkBarNode* parent, | |
| 281 int index, | |
| 282 const std::wstring& title, | |
| 283 const GURL& url, | |
| 284 const Time& creation_time); | |
| 285 | |
| 286 // This is the convenience that makes sure the url is starred or not | |
| 287 // starred. If the URL is not currently starred, it is added to the | |
| 288 // most recent parent. | |
| 289 void SetURLStarred(const GURL& url, | |
| 290 const std::wstring& title, | |
| 291 bool is_starred); | |
| 292 | |
| 293 // Resets the 'date modified' time of the node to 0. This is used during | |
| 294 // importing to exclude the newly created groups from showing up in the | |
| 295 // combobox of most recently modified groups. | |
| 296 void ResetDateGroupModified(BookmarkBarNode* node); | |
| 297 | |
| 298 private: | |
| 299 // Used to order BookmarkBarNodes by URL. | |
| 300 class NodeURLComparator { | |
| 301 public: | |
| 302 bool operator()(BookmarkBarNode* n1, BookmarkBarNode* n2) const { | |
| 303 return n1->GetURL() < n2->GetURL(); | |
| 304 } | |
| 305 }; | |
| 306 | |
| 307 // Overriden to notify the observer the favicon has been loaded. | |
| 308 void FavIconLoaded(BookmarkBarNode* node); | |
| 309 | |
| 310 // Removes the node from internal maps and recurces through all children. If | |
| 311 // the node is a url, its url is added to removed_urls. | |
| 312 // | |
| 313 // This does NOT delete the node. | |
| 314 void RemoveNode(BookmarkBarNode* node, std::set<GURL>* removed_urls); | |
| 315 | |
| 316 // Callback from BookmarkStorage that it has finished loading. This method | |
| 317 // may be hit twice. In particular, on construction BookmarkBarModel asks | |
| 318 // BookmarkStorage to load the bookmarks. BookmarkStorage invokes this method | |
| 319 // with loaded_from_history false and file_exists indicating whether the | |
| 320 // bookmarks file exists. If the file doesn't exist, we query history. When | |
| 321 // history calls us back (OnHistoryDone) we then ask BookmarkStorage to load | |
| 322 // from the migration file. BookmarkStorage again invokes this method, but | |
| 323 // with |loaded_from_history| true. | |
| 324 void OnBookmarkStorageLoadedBookmarks(bool file_exists, | |
| 325 bool loaded_from_history); | |
| 326 | |
| 327 // Used for migrating bookmarks from history to standalone file. | |
| 328 // | |
| 329 // Callback from history that it is done with an empty request. This is used | |
| 330 // if there is no bookmarks file. Once done, we attempt to load from the | |
| 331 // temporary file creating during migration. | |
| 332 void OnHistoryDone(); | |
| 333 | |
| 334 // Invoked when loading is finished. Sets loaded_ and notifies observers. | |
| 335 void DoneLoading(); | |
| 336 | |
| 337 // Populates nodes_ordered_by_url_set_ from root. | |
| 338 void PopulateNodesByURL(BookmarkBarNode* node); | |
| 339 | |
| 340 // Removes the node from its parent, sends notification, and deletes it. | |
| 341 // type specifies how the node should be removed. | |
| 342 void RemoveAndDeleteNode(BookmarkBarNode* delete_me); | |
| 343 | |
| 344 // Adds the node at the specified position, and sends notification. | |
| 345 BookmarkBarNode* AddNode(BookmarkBarNode* parent, | |
| 346 int index, | |
| 347 BookmarkBarNode* node); | |
| 348 | |
| 349 // Implementation of GetNodeByID. | |
| 350 BookmarkBarNode* GetNodeByID(BookmarkBarNode* node, int id); | |
| 351 | |
| 352 // Returns true if the parent and index are valid. | |
| 353 bool IsValidIndex(BookmarkBarNode* parent, int index, bool allow_end); | |
| 354 | |
| 355 // Sets the date modified time of the specified node. | |
| 356 void SetDateGroupModified(BookmarkBarNode* parent, const Time time); | |
| 357 | |
| 358 // Creates the bookmark bar/other nodes. These call into | |
| 359 // CreateRootNodeFromStarredEntry. | |
| 360 void CreateBookmarkBarNode(); | |
| 361 void CreateOtherBookmarksNode(); | |
| 362 | |
| 363 // Creates a root node (either the bookmark bar node or other node) from the | |
| 364 // specified starred entry. | |
| 365 BookmarkBarNode* CreateRootNodeFromStarredEntry( | |
| 366 const history::StarredEntry& entry); | |
| 367 | |
| 368 // Notification that a favicon has finished loading. If we can decode the | |
| 369 // favicon, FaviconLoaded is invoked. | |
| 370 void OnFavIconDataAvailable( | |
| 371 HistoryService::Handle handle, | |
| 372 bool know_favicon, | |
| 373 scoped_refptr<RefCountedBytes> data, | |
| 374 bool expired, | |
| 375 GURL icon_url); | |
| 376 | |
| 377 // Invoked from the node to load the favicon. Requests the favicon from the | |
| 378 // history service. | |
| 379 void LoadFavIcon(BookmarkBarNode* node); | |
| 380 | |
| 381 // If we're waiting on a favicon for node, the load request is canceled. | |
| 382 void CancelPendingFavIconLoadRequests(BookmarkBarNode* node); | |
| 383 | |
| 384 // Returns up to count of the most recently modified groups. This may not | |
| 385 // add anything. | |
| 386 void GetMostRecentlyModifiedGroupNodes(BookmarkBarNode* parent, | |
| 387 size_t count, | |
| 388 std::vector<BookmarkBarNode*>* nodes); | |
| 389 | |
| 390 // NotificationObserver. | |
| 391 virtual void Observe(NotificationType type, | |
| 392 const NotificationSource& source, | |
| 393 const NotificationDetails& details); | |
| 394 | |
| 395 Profile* profile_; | |
| 396 | |
| 397 // Whether the initial set of data has been loaded. | |
| 398 bool loaded_; | |
| 399 | |
| 400 // The root node. This contains the bookmark bar node and the 'other' node as | |
| 401 // children. | |
| 402 BookmarkBarNode root_; | |
| 403 | |
| 404 BookmarkBarNode* bookmark_bar_node_; | |
| 405 BookmarkBarNode* other_node_; | |
| 406 | |
| 407 // The observers. | |
| 408 ObserverList<BookmarkBarModelObserver> observers_; | |
| 409 | |
| 410 // Set of nodes ordered by URL. This is not a map to avoid copying the | |
| 411 // urls. | |
| 412 // WARNING: nodes_ordered_by_url_set_ is accessed on multiple threads. As | |
| 413 // such, be sure and wrap all usage of it around url_lock_. | |
| 414 typedef std::set<BookmarkBarNode*,NodeURLComparator> NodesOrderedByURLSet; | |
| 415 NodesOrderedByURLSet nodes_ordered_by_url_set_; | |
| 416 Lock url_lock_; | |
| 417 | |
| 418 // Used for loading favicons and the empty history request. | |
| 419 CancelableRequestConsumerT<BookmarkBarNode*, NULL> load_consumer_; | |
| 420 | |
| 421 // Reads/writes bookmarks to disk. | |
| 422 scoped_refptr<BookmarkStorage> store_; | |
| 423 | |
| 424 // Have we installed a listener on the NotificationService for | |
| 425 // NOTIFY_HISTORY_LOADED? A listener is installed if the bookmarks file | |
| 426 // doesn't exist and the history service hasn't finished loading. | |
| 427 bool waiting_for_history_load_; | |
| 428 | |
| 429 // Handle to event signaled when loading is done. | |
| 430 ScopedHandle loaded_signal_; | |
| 431 | |
| 432 DISALLOW_EVIL_CONSTRUCTORS(BookmarkBarModel); | |
| 433 }; | |
| 434 | |
| 435 #endif // CHROME_BROWSER_BOOKMARK_BAR_MODEL_H_ | |
| 436 | |
| OLD | NEW |