Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: components/enhanced_bookmarks/enhanced_bookmark_model.h

Issue 563363002: Only set remote id during url node creation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Dedup remote ids + server service changes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ 5 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ 6 #define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
7 7
8 #include <map>
8 #include <string> 9 #include <string>
9 10
10 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "components/bookmarks/browser/base_bookmark_model_observer.h"
11 #include "components/bookmarks/browser/bookmark_node.h" 13 #include "components/bookmarks/browser/bookmark_node.h"
12 #include "components/keyed_service/core/keyed_service.h" 14 #include "components/keyed_service/core/keyed_service.h"
13 15
14 namespace base { 16 namespace base {
15 class Time; 17 class Time;
16 } // namespace base 18 } // namespace base
17 19
18 class BookmarkModel; 20 class BookmarkModel;
21 class BookmarkNode;
19 class GURL; 22 class GURL;
20 23
21 namespace enhanced_bookmarks { 24 namespace enhanced_bookmarks {
22 // Wrapper around BookmarkModel providing utility functions for enhanced 25 // Wrapper around BookmarkModel providing utility functions for enhanced
23 // bookmarks. 26 // bookmarks.
24 class EnhancedBookmarkModel : public KeyedService { 27 class EnhancedBookmarkModel : public KeyedService,
28 public BaseBookmarkModelObserver {
25 public: 29 public:
26 EnhancedBookmarkModel(BookmarkModel* bookmark_model, 30 EnhancedBookmarkModel(BookmarkModel* bookmark_model,
27 const std::string& version); 31 const std::string& version);
28 virtual ~EnhancedBookmarkModel(); 32 virtual ~EnhancedBookmarkModel();
29 33
34 virtual void ShutDown();
35
30 // Moves |node| to |new_parent| and inserts it at the given |index|. 36 // Moves |node| to |new_parent| and inserts it at the given |index|.
31 void Move(const BookmarkNode* node, 37 void Move(const BookmarkNode* node,
32 const BookmarkNode* new_parent, 38 const BookmarkNode* new_parent,
33 int index); 39 int index);
34 40
35 // Adds a new folder node at the specified position. 41 // Adds a new folder node at the specified position.
36 const BookmarkNode* AddFolder(const BookmarkNode* parent, 42 const BookmarkNode* AddFolder(const BookmarkNode* parent,
37 int index, 43 int index,
38 const base::string16& title); 44 const base::string16& title);
39 45
40 // Adds a url at the specified position. 46 // Adds a url at the specified position.
41 const BookmarkNode* AddURL(const BookmarkNode* parent, 47 const BookmarkNode* AddURL(const BookmarkNode* parent,
42 int index, 48 int index,
43 const base::string16& title, 49 const base::string16& title,
44 const GURL& url, 50 const GURL& url,
45 const base::Time& creation_time); 51 const base::Time& creation_time);
46 52
47 // Returns the remote id for a bookmark |node|. 53 // Returns the remote id for a bookmark |node|.
48 std::string GetRemoteId(const BookmarkNode* node); 54 std::string GetRemoteId(const BookmarkNode* node);
49 55
56 // Returns the bookmark node corresponding to the given |remote_id|, or NULL
57 // if there is no node with the id.
58 const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id);
59
50 // Sets the description of a bookmark |node|. 60 // Sets the description of a bookmark |node|.
51 void SetDescription(const BookmarkNode* node, const std::string& description); 61 void SetDescription(const BookmarkNode* node, const std::string& description);
52 62
53 // Returns the description of a bookmark |node|. 63 // Returns the description of a bookmark |node|.
54 std::string GetDescription(const BookmarkNode* node); 64 std::string GetDescription(const BookmarkNode* node);
55 65
56 // Sets the URL of an image representative of a bookmark |node|. 66 // Sets the URL of an image representative of a bookmark |node|.
57 // Expects the URL to be valid and not empty. 67 // Expects the URL to be valid and not empty.
58 // Returns true if the metainfo is successfully populated. 68 // Returns true if the metainfo is successfully populated.
59 bool SetOriginalImage(const BookmarkNode* node, 69 bool SetOriginalImage(const BookmarkNode* node,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 int image_height, 108 int image_height,
99 const GURL& thumbnail_url, 109 const GURL& thumbnail_url,
100 int thumbnail_width, 110 int thumbnail_width,
101 int thumbnail_height); 111 int thumbnail_height);
102 112
103 // TODO(rfevang): Ideally nothing should need the underlying bookmark model. 113 // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
104 // Remove when that is actually the case. 114 // Remove when that is actually the case.
105 BookmarkModel* bookmark_model() { return bookmark_model_; } 115 BookmarkModel* bookmark_model() { return bookmark_model_; }
106 116
107 private: 117 private:
108 // Generates and sets a remote id for the given bookmark |node|. 118 typedef std::map<std::string, const BookmarkNode*> IdToNodeMap;
109 // Returns the id set. 119 typedef std::map<const BookmarkNode*, std::string> NodeToIdMap;
110 std::string SetRemoteId(const BookmarkNode* node); 120
121 // BaseBookmarkModelObserver:
122 virtual void BookmarkModelChanged() OVERRIDE;
123 virtual void BookmarkModelLoaded(BookmarkModel* model,
124 bool ids_reassigned) OVERRIDE;
125 virtual void BookmarkNodeAdded(BookmarkModel* model,
126 const BookmarkNode* parent,
127 int index) OVERRIDE;
128 virtual void BookmarkNodeRemoved(BookmarkModel* model,
129 const BookmarkNode* parent,
130 int old_index,
131 const BookmarkNode* node,
132 const std::set<GURL>& removed_urls) OVERRIDE;
133 virtual void OnWillChangeBookmarkMetaInfo(BookmarkModel* model,
134 const BookmarkNode* node) OVERRIDE;
135 virtual void BookmarkMetaInfoChanged(BookmarkModel* model,
136 const BookmarkNode* node) OVERRIDE;
137 virtual void BookmarkAllUserNodesRemoved(
138 BookmarkModel* model,
139 const std::set<GURL>& removed_urls) OVERRIDE;
140
141 // Initialize the mapping from remote ids to nodes.
142 void InitializeIdMap();
143
144 // Adds a node to the id map if it has a (unique) remote id. Must be followed
145 // by a ResetDuplicateRemoteIds call when done adding nodes.
146 void AddToIdMap(const BookmarkNode* node);
147
148 // Clears out any duplicate remote ids detected by AddToIdMap calls.
149 void ResetDuplicateRemoteIds();
111 150
112 // Helper method for setting a meta info field on a node. Also updates the 151 // Helper method for setting a meta info field on a node. Also updates the
113 // version and userEdits fields. 152 // version field.
114 void SetMetaInfo(const BookmarkNode* node, 153 void SetMetaInfo(const BookmarkNode* node,
115 const std::string& field, 154 const std::string& field,
116 const std::string& value, 155 const std::string& value);
117 bool user_edit); 156
157 // Helper method for setting multiple meta info fields at once. All the fields
158 // in |meta_info| will be set, but the method will not delete fields not
159 // present.
160 void SetMultipleMetaInfo(const BookmarkNode* node,
161 BookmarkNode::MetaInfoMap meta_info);
118 162
119 // Returns the version string to use when setting stars.version. 163 // Returns the version string to use when setting stars.version.
120 std::string GetVersionString(); 164 std::string GetVersionString();
121 165
122 BookmarkModel* bookmark_model_; 166 BookmarkModel* bookmark_model_;
167
168 IdToNodeMap id_map_;
169 NodeToIdMap nodes_to_reset_;
170
171 // Caches the remote id of a node before its meta info changes.
172 std::string prev_remote_id_;
173
123 std::string version_; 174 std::string version_;
124 std::string version_suffix_; 175 std::string version_suffix_;
125 }; 176 };
126 177
127 } // namespace enhanced_bookmarks 178 } // namespace enhanced_bookmarks
128 179
129 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_ 180 #endif // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698