1 // Copyright 2014 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.
5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/strings/string16.h"
14 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
15 #include "components/bookmarks/browser/bookmark_node.h"
16 #include "components/keyed_service/core/keyed_service.h"
26 FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest
, SetMultipleMetaInfo
);
28 namespace enhanced_bookmarks
{
30 class EnhancedBookmarkModelObserver
;
32 // Wrapper around BookmarkModel providing utility functions for enhanced
34 class EnhancedBookmarkModel
: public KeyedService
,
35 public BaseBookmarkModelObserver
{
37 EnhancedBookmarkModel(BookmarkModel
* bookmark_model
,
38 const std::string
& version
);
39 virtual ~EnhancedBookmarkModel();
41 virtual void Shutdown() OVERRIDE
;
43 void AddObserver(EnhancedBookmarkModelObserver
* observer
);
44 void RemoveObserver(EnhancedBookmarkModelObserver
* observer
);
46 // Moves |node| to |new_parent| and inserts it at the given |index|.
47 void Move(const BookmarkNode
* node
,
48 const BookmarkNode
* new_parent
,
51 // Adds a new folder node at the specified position.
52 const BookmarkNode
* AddFolder(const BookmarkNode
* parent
,
54 const base::string16
& title
);
56 // Adds a url at the specified position.
57 const BookmarkNode
* AddURL(const BookmarkNode
* parent
,
59 const base::string16
& title
,
61 const base::Time
& creation_time
);
63 // Returns the remote id for a bookmark |node|.
64 std::string
GetRemoteId(const BookmarkNode
* node
);
66 // Returns the bookmark node corresponding to the given |remote_id|, or NULL
67 // if there is no node with the id.
68 const BookmarkNode
* BookmarkForRemoteId(const std::string
& remote_id
);
70 // Sets the description of a bookmark |node|.
71 void SetDescription(const BookmarkNode
* node
, const std::string
& description
);
73 // Returns the description of a bookmark |node|.
74 std::string
GetDescription(const BookmarkNode
* node
);
76 // Sets the URL of an image representative of a bookmark |node|.
77 // Expects the URL to be valid and not empty.
78 // Returns true if the metainfo is successfully populated.
79 bool SetOriginalImage(const BookmarkNode
* node
,
84 // Returns the url and dimensions of the original scraped image of a
86 // Returns true if the out variables are populated, false otherwise.
87 bool GetOriginalImage(const BookmarkNode
* node
,
92 // Returns the url and dimensions of the server provided thumbnail image for
93 // a given bookmark |node|.
94 // Returns true if the out variables are populated, false otherwise.
95 bool GetThumbnailImage(const BookmarkNode
* node
,
100 // Returns a brief server provided synopsis of the bookmarked page.
101 // Returns the empty string if the snippet could not be extracted.
102 std::string
GetSnippet(const BookmarkNode
* node
);
104 // Sets a custom suffix to be added to the version field when writing meta
106 void SetVersionSuffix(const std::string
& version_suffix
);
108 // TODO(rfevang): Add method + enum for accessing/writing flags.
110 // Used for testing, simulates the process that creates the thumbnails. Will
111 // remove existing entries for empty urls or set them if the url is not empty.
112 // Expects valid or empty urls. Returns true if the metainfo is successfully
114 // TODO(rfevang): Move this to a testing only utility file.
115 bool SetAllImages(const BookmarkNode
* node
,
116 const GURL
& image_url
,
119 const GURL
& thumbnail_url
,
121 int thumbnail_height
);
123 // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
124 // Remove when that is actually the case.
125 BookmarkModel
* bookmark_model() { return bookmark_model_
; }
127 // Returns true if the enhanced bookmark model is done loading.
128 bool loaded() { return loaded_
; }
131 FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest
, SetMultipleMetaInfo
);
133 typedef std::map
<std::string
, const BookmarkNode
*> IdToNodeMap
;
134 typedef std::map
<const BookmarkNode
*, std::string
> NodeToIdMap
;
136 // BaseBookmarkModelObserver:
137 virtual void BookmarkModelChanged() OVERRIDE
;
138 virtual void BookmarkModelLoaded(BookmarkModel
* model
,
139 bool ids_reassigned
) OVERRIDE
;
140 virtual void BookmarkNodeAdded(BookmarkModel
* model
,
141 const BookmarkNode
* parent
,
143 virtual void BookmarkNodeRemoved(BookmarkModel
* model
,
144 const BookmarkNode
* parent
,
146 const BookmarkNode
* node
,
147 const std::set
<GURL
>& removed_urls
) OVERRIDE
;
148 virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel
* model
,
149 const BookmarkNode
* node
) OVERRIDE
;
150 virtual void BookmarkMetaInfoChanged(BookmarkModel
* model
,
151 const BookmarkNode
* node
) OVERRIDE
;
152 virtual void BookmarkAllUserNodesRemoved(
153 BookmarkModel
* model
,
154 const std::set
<GURL
>& removed_urls
) OVERRIDE
;
156 // Initialize the mapping from remote ids to nodes.
157 void InitializeIdMap();
159 // Adds a node to the id map if it has a (unique) remote id. Must be followed
160 // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes.
161 void AddToIdMap(const BookmarkNode
* node
);
163 // If there are nodes that needs to reset their remote ids, schedules
164 // ResetDuplicateRemoteIds to be run asynchronously.
165 void ScheduleResetDuplicateRemoteIds();
167 // Clears out any duplicate remote ids detected by AddToIdMap calls.
168 void ResetDuplicateRemoteIds();
170 // Helper method for setting a meta info field on a node. Also updates the
172 void SetMetaInfo(const BookmarkNode
* node
,
173 const std::string
& field
,
174 const std::string
& value
);
176 // Helper method for setting multiple meta info fields at once. All the fields
177 // in |meta_info| will be set, but the method will not delete fields not
179 void SetMultipleMetaInfo(const BookmarkNode
* node
,
180 BookmarkNode::MetaInfoMap meta_info
);
182 // Returns the version string to use when setting stars.version.
183 std::string
GetVersionString();
185 BookmarkModel
* bookmark_model_
;
188 ObserverList
<EnhancedBookmarkModelObserver
> observers_
;
190 base::WeakPtrFactory
<EnhancedBookmarkModel
> weak_ptr_factory_
;
193 NodeToIdMap nodes_to_reset_
;
195 // Caches the remote id of a node before its meta info changes.
196 std::string prev_remote_id_
;
198 std::string version_
;
199 std::string version_suffix_
;
202 } // namespace enhanced_bookmarks
204 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_