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 "components/bookmarks/core/browser/bookmark_client.h" |
21 #include "components/bookmarks/core/browser/bookmark_node.h" | 21 #include "components/bookmarks/core/browser/bookmark_node.h" |
22 #include "components/bookmarks/core/browser/bookmark_service.h" | 22 #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" | 23 #include "ui/gfx/image/image.h" |
28 #include "url/gurl.h" | 24 #include "url/gurl.h" |
29 | 25 |
30 class BookmarkExpandedStateTracker; | 26 class BookmarkExpandedStateTracker; |
31 class BookmarkIndex; | 27 class BookmarkIndex; |
32 class BookmarkLoadDetails; | 28 class BookmarkLoadDetails; |
33 class BookmarkModelObserver; | 29 class BookmarkModelObserver; |
34 class BookmarkStorage; | 30 class BookmarkStorage; |
35 struct BookmarkMatch; | 31 struct BookmarkMatch; |
36 class Profile; | 32 class PrefService; |
37 class ScopedGroupBookmarkActions; | 33 class ScopedGroupBookmarkActions; |
38 | 34 |
39 namespace base { | 35 namespace base { |
| 36 class FilePath; |
40 class SequencedTaskRunner; | 37 class SequencedTaskRunner; |
41 } | 38 } |
42 | 39 |
43 namespace chrome { | 40 namespace favicon_base { |
44 struct FaviconImageResult; | 41 struct FaviconImageResult; |
45 } | 42 } |
46 | 43 |
| 44 namespace test { |
| 45 class TestBookmarkClient; |
| 46 } |
| 47 |
47 // BookmarkModel -------------------------------------------------------------- | 48 // BookmarkModel -------------------------------------------------------------- |
48 | 49 |
49 // BookmarkModel provides a directed acyclic graph of URLs and folders. | 50 // BookmarkModel provides a directed acyclic graph of URLs and folders. |
50 // Three graphs are provided for the three entry points: those on the 'bookmarks | 51 // 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. | 52 // bar', those in the 'other bookmarks' folder and those in the 'mobile' folder. |
52 // | 53 // |
53 // An observer may be attached to observe relevant events. | 54 // An observer may be attached to observe relevant events. |
54 // | 55 // |
55 // You should NOT directly create a BookmarkModel, instead go through the | 56 // You should NOT directly create a BookmarkModel, instead go through the |
56 // BookmarkModelFactory. | 57 // BookmarkModelFactory. |
57 class BookmarkModel : public content::NotificationObserver, | 58 class BookmarkModel : public BookmarkService { |
58 public BookmarkService, | |
59 public KeyedService { | |
60 public: | 59 public: |
61 // |index_urls| says whether URLs should be stored in the BookmarkIndex | 60 // |index_urls| says whether URLs should be stored in the BookmarkIndex |
62 // in addition to bookmark titles. | 61 // in addition to bookmark titles. |
63 BookmarkModel(Profile* profile, bool index_urls); | 62 BookmarkModel(BookmarkClient* client, bool index_urls); |
64 virtual ~BookmarkModel(); | 63 virtual ~BookmarkModel(); |
65 | 64 |
66 // Invoked prior to destruction to release any necessary resources. | 65 // Invoked prior to destruction to release any necessary resources. |
67 virtual void Shutdown() OVERRIDE; | 66 void Shutdown(); |
68 | 67 |
69 // Loads the bookmarks. This is called upon creation of the | 68 // Loads the bookmarks. This is called upon creation of the |
70 // BookmarkModel. You need not invoke this directly. | 69 // BookmarkModel. You need not invoke this directly. |
71 // All load operations will be executed on |task_runner|. | 70 // All load operations will be executed on |io_task_runner| and the completion |
72 void Load(const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 71 // callback will be called from |ui_task_runner|. |
| 72 void Load(PrefService* pref_service, |
| 73 const std::string& accept_languages, |
| 74 const base::FilePath& profile_path, |
| 75 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner, |
| 76 const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner); |
73 | 77 |
74 // Returns true if the model finished loading. | 78 // Returns true if the model finished loading. |
75 bool loaded() const { return loaded_; } | 79 bool loaded() const { return loaded_; } |
76 | 80 |
77 // Returns the root node. The 'bookmark bar' node and 'other' node are | 81 // Returns the root node. The 'bookmark bar' node and 'other' node are |
78 // children of the root node. | 82 // children of the root node. |
79 const BookmarkNode* root_node() const { return &root_; } | 83 const BookmarkNode* root_node() const { return &root_; } |
80 | 84 |
81 // Returns the 'bookmark bar' node. This is NULL until loaded. | 85 // Returns the 'bookmark bar' node. This is NULL until loaded. |
82 const BookmarkNode* bookmark_bar_node() const { return bookmark_bar_node_; } | 86 const BookmarkNode* bookmark_bar_node() const { return bookmark_bar_node_; } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 const std::string& value); | 246 const std::string& value); |
243 void SetNodeMetaInfoMap(const BookmarkNode* node, | 247 void SetNodeMetaInfoMap(const BookmarkNode* node, |
244 const BookmarkNode::MetaInfoMap& meta_info_map); | 248 const BookmarkNode::MetaInfoMap& meta_info_map); |
245 void DeleteNodeMetaInfo(const BookmarkNode* node, | 249 void DeleteNodeMetaInfo(const BookmarkNode* node, |
246 const std::string& key); | 250 const std::string& key); |
247 | 251 |
248 // Sets the sync transaction version of |node|. | 252 // Sets the sync transaction version of |node|. |
249 void SetNodeSyncTransactionVersion(const BookmarkNode* node, | 253 void SetNodeSyncTransactionVersion(const BookmarkNode* node, |
250 int64 sync_transaction_version); | 254 int64 sync_transaction_version); |
251 | 255 |
252 // Returns the profile that corresponds to this BookmarkModel. | 256 // Notify BookmarkModel that the favicons for |urls| have changed and have to |
253 Profile* profile() { return profile_; } | 257 // be refetched. This notification is sent by BookmarkClient. |
| 258 void OnFaviconChanged(const std::set<GURL>& urls); |
| 259 |
| 260 // Returns the client used by this BookmarkModel. |
| 261 BookmarkClient* client() const { return client_; } |
254 | 262 |
255 private: | 263 private: |
256 friend class BookmarkCodecTest; | 264 friend class BookmarkCodecTest; |
257 friend class BookmarkModelTest; | 265 friend class BookmarkModelTest; |
258 friend class BookmarkStorage; | 266 friend class BookmarkStorage; |
259 friend class ScopedGroupBookmarkActions; | 267 friend class ScopedGroupBookmarkActions; |
| 268 friend class test::TestBookmarkClient; |
260 | 269 |
261 // Used to order BookmarkNodes by URL. | 270 // Used to order BookmarkNodes by URL. |
262 class NodeURLComparator { | 271 class NodeURLComparator { |
263 public: | 272 public: |
264 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const { | 273 bool operator()(const BookmarkNode* n1, const BookmarkNode* n2) const { |
265 return n1->url() < n2->url(); | 274 return n1->url() < n2->url(); |
266 } | 275 } |
267 }; | 276 }; |
268 | 277 |
269 // Implementation of IsBookmarked. Before calling this the caller must obtain | 278 // Implementation of IsBookmarked. Before calling this the caller must obtain |
(...skipping 20 matching lines...) Expand all Loading... |
290 void RemoveNodeAndGetRemovedUrls(BookmarkNode* node, | 299 void RemoveNodeAndGetRemovedUrls(BookmarkNode* node, |
291 std::set<GURL>* removed_urls); | 300 std::set<GURL>* removed_urls); |
292 | 301 |
293 // Removes the node from its parent, sends notification, and deletes it. | 302 // Removes the node from its parent, sends notification, and deletes it. |
294 // type specifies how the node should be removed. | 303 // type specifies how the node should be removed. |
295 void RemoveAndDeleteNode(BookmarkNode* delete_me); | 304 void RemoveAndDeleteNode(BookmarkNode* delete_me); |
296 | 305 |
297 // Remove |node| from |nodes_ordered_by_url_set_|. | 306 // Remove |node| from |nodes_ordered_by_url_set_|. |
298 void RemoveNodeFromURLSet(BookmarkNode* node); | 307 void RemoveNodeFromURLSet(BookmarkNode* node); |
299 | 308 |
300 // Notifies the history backend about urls of removed bookmarks. | |
301 void NotifyHistoryAboutRemovedBookmarks( | |
302 const std::set<GURL>& removed_bookmark_urls) const; | |
303 | |
304 // Adds the |node| at |parent| in the specified |index| and notifies its | 309 // Adds the |node| at |parent| in the specified |index| and notifies its |
305 // observers. | 310 // observers. |
306 BookmarkNode* AddNode(BookmarkNode* parent, | 311 BookmarkNode* AddNode(BookmarkNode* parent, |
307 int index, | 312 int index, |
308 BookmarkNode* node); | 313 BookmarkNode* node); |
309 | 314 |
310 // Returns true if the parent and index are valid. | 315 // Returns true if the parent and index are valid. |
311 bool IsValidIndex(const BookmarkNode* parent, int index, bool allow_end); | 316 bool IsValidIndex(const BookmarkNode* parent, int index, bool allow_end); |
312 | 317 |
313 // Creates one of the possible permanent nodes (bookmark bar node, other node | 318 // Creates one of the possible permanent nodes (bookmark bar node, other node |
(...skipping 14 matching lines...) Expand all Loading... |
328 void FaviconLoaded(const BookmarkNode* node); | 333 void FaviconLoaded(const BookmarkNode* node); |
329 | 334 |
330 // If we're waiting on a favicon for node, the load request is canceled. | 335 // If we're waiting on a favicon for node, the load request is canceled. |
331 void CancelPendingFaviconLoadRequests(BookmarkNode* node); | 336 void CancelPendingFaviconLoadRequests(BookmarkNode* node); |
332 | 337 |
333 // Notifies the observers that a set of changes initiated by a single user | 338 // Notifies the observers that a set of changes initiated by a single user |
334 // action is about to happen and has completed. | 339 // action is about to happen and has completed. |
335 void BeginGroupedChanges(); | 340 void BeginGroupedChanges(); |
336 void EndGroupedChanges(); | 341 void EndGroupedChanges(); |
337 | 342 |
338 // content::NotificationObserver: | |
339 virtual void Observe(int type, | |
340 const content::NotificationSource& source, | |
341 const content::NotificationDetails& details) OVERRIDE; | |
342 | |
343 // Generates and returns the next node ID. | 343 // Generates and returns the next node ID. |
344 int64 generate_next_node_id(); | 344 int64 generate_next_node_id(); |
345 | 345 |
346 // Sets the maximum node ID to the given value. | 346 // Sets the maximum node ID to the given value. |
347 // This is used by BookmarkCodec to report the maximum ID after it's done | 347 // This is used by BookmarkCodec to report the maximum ID after it's done |
348 // decoding since during decoding codec assigns node IDs. | 348 // decoding since during decoding codec assigns node IDs. |
349 void set_next_node_id(int64 id) { next_node_id_ = id; } | 349 void set_next_node_id(int64 id) { next_node_id_ = id; } |
350 | 350 |
351 // Creates and returns a new BookmarkLoadDetails. It's up to the caller to | 351 // Creates and returns a new BookmarkLoadDetails. It's up to the caller to |
352 // delete the returned object. | 352 // delete the returned object. |
353 BookmarkLoadDetails* CreateLoadDetails(); | 353 BookmarkLoadDetails* CreateLoadDetails(const std::string& accept_languages); |
354 | 354 |
355 content::NotificationRegistrar registrar_; | 355 BookmarkClient* const client_; |
356 | |
357 Profile* profile_; | |
358 | 356 |
359 // Whether the initial set of data has been loaded. | 357 // Whether the initial set of data has been loaded. |
360 bool loaded_; | 358 bool loaded_; |
361 | 359 |
362 // The root node. This contains the bookmark bar node and the 'other' node as | 360 // The root node. This contains the bookmark bar node and the 'other' node as |
363 // children. | 361 // children. |
364 BookmarkNode root_; | 362 BookmarkNode root_; |
365 | 363 |
366 BookmarkPermanentNode* bookmark_bar_node_; | 364 BookmarkPermanentNode* bookmark_bar_node_; |
367 BookmarkPermanentNode* other_node_; | 365 BookmarkPermanentNode* other_node_; |
(...skipping 29 matching lines...) Expand all Loading... |
397 | 395 |
398 // See description of IsDoingExtensiveChanges above. | 396 // See description of IsDoingExtensiveChanges above. |
399 int extensive_changes_; | 397 int extensive_changes_; |
400 | 398 |
401 scoped_ptr<BookmarkExpandedStateTracker> expanded_state_tracker_; | 399 scoped_ptr<BookmarkExpandedStateTracker> expanded_state_tracker_; |
402 | 400 |
403 DISALLOW_COPY_AND_ASSIGN(BookmarkModel); | 401 DISALLOW_COPY_AND_ASSIGN(BookmarkModel); |
404 }; | 402 }; |
405 | 403 |
406 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ | 404 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_MODEL_H_ |
OLD | NEW |