OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ | 5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ |
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ | 6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
20 #include "base/task/cancelable_task_tracker.h" | 20 #include "base/task/cancelable_task_tracker.h" |
| 21 #include "components/bookmarks/core/browser/bookmark_client.h" |
21 #include "components/bookmarks/core/browser/bookmark_node.h" | 22 #include "components/bookmarks/core/browser/bookmark_node.h" |
22 #include "components/bookmarks/core/browser/bookmark_service.h" | 23 #include "components/bookmarks/core/browser/bookmark_service.h" |
23 #include "components/favicon_base/favicon_types.h" | |
24 #include "components/keyed_service/core/keyed_service.h" | |
25 #include "content/public/browser/notification_observer.h" | |
26 #include "content/public/browser/notification_registrar.h" | |
27 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
28 #include "url/gurl.h" | 25 #include "url/gurl.h" |
29 | 26 |
30 class BookmarkExpandedStateTracker; | 27 class BookmarkExpandedStateTracker; |
31 class BookmarkIndex; | 28 class BookmarkIndex; |
32 class BookmarkLoadDetails; | 29 class BookmarkLoadDetails; |
33 class BookmarkModelObserver; | 30 class BookmarkModelObserver; |
34 class BookmarkStorage; | 31 class BookmarkStorage; |
35 struct BookmarkTitleMatch; | 32 struct BookmarkTitleMatch; |
36 class Profile; | 33 class PrefService; |
37 class ScopedGroupBookmarkActions; | 34 class ScopedGroupBookmarkActions; |
38 | 35 |
39 namespace base { | 36 namespace base { |
| 37 class FilePath; |
40 class SequencedTaskRunner; | 38 class SequencedTaskRunner; |
41 } | 39 } |
42 | 40 |
43 namespace chrome { | 41 namespace favicon_base { |
44 struct FaviconImageResult; | 42 struct FaviconImageResult; |
45 } | 43 } |
46 | 44 |
| 45 namespace test { |
| 46 class TestBookmarkClient; |
| 47 } |
| 48 |
47 // BookmarkModel -------------------------------------------------------------- | 49 // BookmarkModel -------------------------------------------------------------- |
48 | 50 |
49 // BookmarkModel provides a directed acyclic graph of URLs and folders. | 51 // BookmarkModel provides a directed acyclic graph of URLs and folders. |
50 // Three graphs are provided for the three entry points: those on the 'bookmarks | 52 // Three graphs are provided for the three entry points: those on the 'bookmarks |
51 // bar', those in the 'other bookmarks' folder and those in the 'mobile' folder. | 53 // bar', those in the 'other bookmarks' folder and those in the 'mobile' folder. |
52 // | 54 // |
53 // An observer may be attached to observe relevant events. | 55 // An observer may be attached to observe relevant events. |
54 // | 56 // |
55 // You should NOT directly create a BookmarkModel, instead go through the | 57 // You should NOT directly create a BookmarkModel, instead go through the |
56 // BookmarkModelFactory. | 58 // BookmarkModelFactory. |
57 class BookmarkModel : public content::NotificationObserver, | 59 class BookmarkModel : public BookmarkService { |
58 public BookmarkService, | |
59 public KeyedService { | |
60 public: | 60 public: |
61 explicit BookmarkModel(Profile* profile); | 61 explicit BookmarkModel(BookmarkClient* client); |
62 virtual ~BookmarkModel(); | 62 virtual ~BookmarkModel(); |
63 | 63 |
64 // Invoked prior to destruction to release any necessary resources. | 64 // Invoked prior to destruction to release any necessary resources. |
65 virtual void Shutdown() OVERRIDE; | 65 void Shutdown(); |
66 | 66 |
67 // Loads the bookmarks. This is called upon creation of the | 67 // Loads the bookmarks. This is called upon creation of the |
68 // BookmarkModel. You need not invoke this directly. | 68 // BookmarkModel. You need not invoke this directly. |
69 // All load operations will be executed on |task_runner|. | 69 // All load operations will be executed on |io_task_runner|, and callback |
70 void Load(const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 70 // will be called from |io_task_runner|. |
| 71 void Load(PrefService* pref_service, |
| 72 const base::FilePath& profile_path, |
| 73 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 74 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner); |
71 | 75 |
72 // Returns true if the model finished loading. | 76 // Returns true if the model finished loading. |
73 bool loaded() const { return loaded_; } | 77 bool loaded() const { return loaded_; } |
74 | 78 |
75 // Returns the root node. The 'bookmark bar' node and 'other' node are | 79 // Returns the root node. The 'bookmark bar' node and 'other' node are |
76 // children of the root node. | 80 // children of the root node. |
77 const BookmarkNode* root_node() const { return &root_; } | 81 const BookmarkNode* root_node() const { return &root_; } |
78 | 82 |
79 // Returns the 'bookmark bar' node. This is NULL until loaded. | 83 // Returns the 'bookmark bar' node. This is NULL until loaded. |
80 const BookmarkNode* bookmark_bar_node() const { return bookmark_bar_node_; } | 84 const BookmarkNode* bookmark_bar_node() const { return bookmark_bar_node_; } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 const std::string& value); | 242 const std::string& value); |
239 void SetNodeMetaInfoMap(const BookmarkNode* node, | 243 void SetNodeMetaInfoMap(const BookmarkNode* node, |
240 const BookmarkNode::MetaInfoMap& meta_info_map); | 244 const BookmarkNode::MetaInfoMap& meta_info_map); |
241 void DeleteNodeMetaInfo(const BookmarkNode* node, | 245 void DeleteNodeMetaInfo(const BookmarkNode* node, |
242 const std::string& key); | 246 const std::string& key); |
243 | 247 |
244 // Sets the sync transaction version of |node|. | 248 // Sets the sync transaction version of |node|. |
245 void SetNodeSyncTransactionVersion(const BookmarkNode* node, | 249 void SetNodeSyncTransactionVersion(const BookmarkNode* node, |
246 int64 sync_transaction_version); | 250 int64 sync_transaction_version); |
247 | 251 |
248 // Returns the profile that corresponds to this BookmarkModel. | 252 // Returns the BookmarkClient that corresponds to this BookmarkModel. |
249 Profile* profile() { return profile_; } | 253 BookmarkClient* client() { return client_; } |
| 254 |
| 255 void OnFaviconChanged(const std::set<GURL>& urls); |
250 | 256 |
251 private: | 257 private: |
252 friend class BookmarkCodecTest; | 258 friend class BookmarkCodecTest; |
253 friend class BookmarkModelTest; | 259 friend class BookmarkModelTest; |
254 friend class BookmarkStorage; | 260 friend class BookmarkStorage; |
255 friend class ScopedGroupBookmarkActions; | 261 friend class ScopedGroupBookmarkActions; |
| 262 friend class test::TestBookmarkClient; |
256 | 263 |
257 // Used to order BookmarkNodes by URL. | 264 // Used to order BookmarkNodes by URL. |
258 class NodeURLComparator { | 265 class NodeURLComparator { |
259 public: | 266 public: |
260 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const { | 267 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const { |
261 return n1->url() < n2->url(); | 268 return n1->url() < n2->url(); |
262 } | 269 } |
263 }; | 270 }; |
264 | 271 |
265 // Implementation of IsBookmarked. Before calling this the caller must obtain | 272 // Implementation of IsBookmarked. Before calling this the caller must obtain |
(...skipping 20 matching lines...) Expand all Loading... |
286 void RemoveNodeAndGetRemovedUrls(BookmarkNode* node, | 293 void RemoveNodeAndGetRemovedUrls(BookmarkNode* node, |
287 std::set<GURL>* removed_urls); | 294 std::set<GURL>* removed_urls); |
288 | 295 |
289 // Removes the node from its parent, sends notification, and deletes it. | 296 // Removes the node from its parent, sends notification, and deletes it. |
290 // type specifies how the node should be removed. | 297 // type specifies how the node should be removed. |
291 void RemoveAndDeleteNode(BookmarkNode* delete_me); | 298 void RemoveAndDeleteNode(BookmarkNode* delete_me); |
292 | 299 |
293 // Remove |node| from |nodes_ordered_by_url_set_|. | 300 // Remove |node| from |nodes_ordered_by_url_set_|. |
294 void RemoveNodeFromURLSet(BookmarkNode* node); | 301 void RemoveNodeFromURLSet(BookmarkNode* node); |
295 | 302 |
296 // Notifies the history backend about urls of removed bookmarks. | |
297 void NotifyHistoryAboutRemovedBookmarks( | |
298 const std::set<GURL>& removed_bookmark_urls) const; | |
299 | |
300 // Adds the |node| at |parent| in the specified |index| and notifies its | 303 // Adds the |node| at |parent| in the specified |index| and notifies its |
301 // observers. | 304 // observers. |
302 BookmarkNode* AddNode(BookmarkNode* parent, | 305 BookmarkNode* AddNode(BookmarkNode* parent, |
303 int index, | 306 int index, |
304 BookmarkNode* node); | 307 BookmarkNode* node); |
305 | 308 |
306 // Returns true if the parent and index are valid. | 309 // Returns true if the parent and index are valid. |
307 bool IsValidIndex(const BookmarkNode* parent, int index, bool allow_end); | 310 bool IsValidIndex(const BookmarkNode* parent, int index, bool allow_end); |
308 | 311 |
309 // Creates one of the possible permanent nodes (bookmark bar node, other node | 312 // Creates one of the possible permanent nodes (bookmark bar node, other node |
(...skipping 14 matching lines...) Expand all Loading... |
324 void FaviconLoaded(const BookmarkNode* node); | 327 void FaviconLoaded(const BookmarkNode* node); |
325 | 328 |
326 // If we're waiting on a favicon for node, the load request is canceled. | 329 // If we're waiting on a favicon for node, the load request is canceled. |
327 void CancelPendingFaviconLoadRequests(BookmarkNode* node); | 330 void CancelPendingFaviconLoadRequests(BookmarkNode* node); |
328 | 331 |
329 // Notifies the observers that a set of changes initiated by a single user | 332 // Notifies the observers that a set of changes initiated by a single user |
330 // action is about to happen and has completed. | 333 // action is about to happen and has completed. |
331 void BeginGroupedChanges(); | 334 void BeginGroupedChanges(); |
332 void EndGroupedChanges(); | 335 void EndGroupedChanges(); |
333 | 336 |
334 // content::NotificationObserver: | |
335 virtual void Observe(int type, | |
336 const content::NotificationSource& source, | |
337 const content::NotificationDetails& details) OVERRIDE; | |
338 | |
339 // Generates and returns the next node ID. | 337 // Generates and returns the next node ID. |
340 int64 generate_next_node_id(); | 338 int64 generate_next_node_id(); |
341 | 339 |
342 // Sets the maximum node ID to the given value. | 340 // Sets the maximum node ID to the given value. |
343 // This is used by BookmarkCodec to report the maximum ID after it's done | 341 // This is used by BookmarkCodec to report the maximum ID after it's done |
344 // decoding since during decoding codec assigns node IDs. | 342 // decoding since during decoding codec assigns node IDs. |
345 void set_next_node_id(int64 id) { next_node_id_ = id; } | 343 void set_next_node_id(int64 id) { next_node_id_ = id; } |
346 | 344 |
347 // Creates and returns a new BookmarkLoadDetails. It's up to the caller to | 345 // Creates and returns a new BookmarkLoadDetails. It's up to the caller to |
348 // delete the returned object. | 346 // delete the returned object. |
349 BookmarkLoadDetails* CreateLoadDetails(); | 347 BookmarkLoadDetails* CreateLoadDetails(); |
350 | 348 |
351 content::NotificationRegistrar registrar_; | 349 BookmarkClient* client_; |
352 | |
353 Profile* profile_; | |
354 | 350 |
355 // Whether the initial set of data has been loaded. | 351 // Whether the initial set of data has been loaded. |
356 bool loaded_; | 352 bool loaded_; |
357 | 353 |
358 // The root node. This contains the bookmark bar node and the 'other' node as | 354 // The root node. This contains the bookmark bar node and the 'other' node as |
359 // children. | 355 // children. |
360 BookmarkNode root_; | 356 BookmarkNode root_; |
361 | 357 |
362 BookmarkPermanentNode* bookmark_bar_node_; | 358 BookmarkPermanentNode* bookmark_bar_node_; |
363 BookmarkPermanentNode* other_node_; | 359 BookmarkPermanentNode* other_node_; |
(...skipping 25 matching lines...) Expand all Loading... |
389 | 385 |
390 // See description of IsDoingExtensiveChanges above. | 386 // See description of IsDoingExtensiveChanges above. |
391 int extensive_changes_; | 387 int extensive_changes_; |
392 | 388 |
393 scoped_ptr<BookmarkExpandedStateTracker> expanded_state_tracker_; | 389 scoped_ptr<BookmarkExpandedStateTracker> expanded_state_tracker_; |
394 | 390 |
395 DISALLOW_COPY_AND_ASSIGN(BookmarkModel); | 391 DISALLOW_COPY_AND_ASSIGN(BookmarkModel); |
396 }; | 392 }; |
397 | 393 |
398 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ | 394 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ |
OLD | NEW |