summaryrefslogtreecommitdiffstats
path: root/components/enhanced_bookmarks/enhanced_bookmark_model.h
blob: 7eab2665b1c881959bf1ddbd3000f563493882ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_
#define COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_

#include <map>
#include <string>

#include "base/cancelable_callback.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/strings/string16.h"
#include "components/bookmarks/browser/base_bookmark_model_observer.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/keyed_service/core/keyed_service.h"

class BookmarkNode;
class GURL;

namespace base {
class Time;
}

namespace bookmarks {
class BookmarkModel;
}

FORWARD_DECLARE_TEST(EnhancedBookmarkModelTest, SetMultipleMetaInfo);

namespace enhanced_bookmarks {

class EnhancedBookmarkModelObserver;

// Wrapper around BookmarkModel providing utility functions for enhanced
// bookmarks.
class EnhancedBookmarkModel : public KeyedService,
                              public bookmarks::BaseBookmarkModelObserver {
 public:
  EnhancedBookmarkModel(bookmarks::BookmarkModel* bookmark_model,
                        const std::string& version);
  ~EnhancedBookmarkModel() override;

  void Shutdown() override;

  void AddObserver(EnhancedBookmarkModelObserver* observer);
  void RemoveObserver(EnhancedBookmarkModelObserver* observer);

  // Moves |node| to |new_parent| and inserts it at the given |index|.
  void Move(const BookmarkNode* node,
            const BookmarkNode* new_parent,
            int index);

  // Adds a new folder node at the specified position.
  const BookmarkNode* AddFolder(const BookmarkNode* parent,
                                int index,
                                const base::string16& title);

  // Adds a url at the specified position.
  const BookmarkNode* AddURL(const BookmarkNode* parent,
                             int index,
                             const base::string16& title,
                             const GURL& url,
                             const base::Time& creation_time);

  // Returns the remote id for a bookmark |node|.
  std::string GetRemoteId(const BookmarkNode* node);

  // Returns the bookmark node corresponding to the given |remote_id|, or NULL
  // if there is no node with the id.
  const BookmarkNode* BookmarkForRemoteId(const std::string& remote_id);

  // Sets the description of a bookmark |node|.
  void SetDescription(const BookmarkNode* node, const std::string& description);

  // Returns the description of a bookmark |node|.
  std::string GetDescription(const BookmarkNode* node);

  // Sets the URL of an image representative of a bookmark |node|.
  // Expects the URL to be valid and not empty.
  // Returns true if the metainfo is successfully populated.
  bool SetOriginalImage(const BookmarkNode* node,
                        const GURL& url,
                        int width,
                        int height);

  // Returns the url and dimensions of the original scraped image of a
  // bookmark |node|.
  // Returns true if the out variables are populated, false otherwise.
  bool GetOriginalImage(const BookmarkNode* node,
                        GURL* url,
                        int* width,
                        int* height);

  // Returns the url and dimensions of the server provided thumbnail image for
  // a given bookmark |node|.
  // Returns true if the out variables are populated, false otherwise.
  bool GetThumbnailImage(const BookmarkNode* node,
                         GURL* url,
                         int* width,
                         int* height);

  // Returns a brief server provided synopsis of the bookmarked page.
  // Returns the empty string if the snippet could not be extracted.
  std::string GetSnippet(const BookmarkNode* node);

  // Sets a custom suffix to be added to the version field when writing meta
  // info fields.
  void SetVersionSuffix(const std::string& version_suffix);

  // TODO(rfevang): Add method + enum for accessing/writing flags.

  // Used for testing, simulates the process that creates the thumbnails. Will
  // remove existing entries for empty urls or set them if the url is not empty.
  // Expects valid or empty urls. Returns true if the metainfo is successfully
  // populated.
  // TODO(rfevang): Move this to a testing only utility file.
  bool SetAllImages(const BookmarkNode* node,
                    const GURL& image_url,
                    int image_width,
                    int image_height,
                    const GURL& thumbnail_url,
                    int thumbnail_width,
                    int thumbnail_height);

  // TODO(rfevang): Ideally nothing should need the underlying bookmark model.
  // Remove when that is actually the case.
  bookmarks::BookmarkModel* bookmark_model() { return bookmark_model_; }

  // Returns true if the enhanced bookmark model is done loading.
  bool loaded() { return loaded_; }

  // Returns the version string to use when setting stars.version.
  std::string GetVersionString();

 private:
  FRIEND_TEST_ALL_PREFIXES(::EnhancedBookmarkModelTest, SetMultipleMetaInfo);

  typedef std::map<std::string, const BookmarkNode*> IdToNodeMap;
  typedef std::map<const BookmarkNode*, std::string> NodeToIdMap;

  // bookmarks::BaseBookmarkModelObserver:
  void BookmarkModelChanged() override;
  void BookmarkModelLoaded(bookmarks::BookmarkModel* model,
                           bool ids_reassigned) override;
  void BookmarkNodeAdded(bookmarks::BookmarkModel* model,
                         const BookmarkNode* parent,
                         int index) override;
  void BookmarkNodeRemoved(bookmarks::BookmarkModel* model,
                           const BookmarkNode* parent,
                           int old_index,
                           const BookmarkNode* node,
                           const std::set<GURL>& removed_urls) override;
  void BookmarkNodeChanged(bookmarks::BookmarkModel* model,
                           const BookmarkNode* node) override;
  void OnWillChangeBookmarkMetaInfo(bookmarks::BookmarkModel* model,
                                    const BookmarkNode* node) override;
  void BookmarkMetaInfoChanged(bookmarks::BookmarkModel* model,
                               const BookmarkNode* node) override;
  void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model,
                                   const std::set<GURL>& removed_urls) override;

  // Initialize the mapping from remote ids to nodes.
  void InitializeIdMap();

  // Adds a node to the id map if it has a (unique) remote id. Must be followed
  // by a (Schedule)ResetDuplicateRemoteIds call when done adding nodes.
  void AddToIdMap(const BookmarkNode* node);

  // Recursively removes a node and all its children from the various maps.
  void RemoveNodeFromMaps(const BookmarkNode* node);

  // If there are nodes that needs to reset their remote ids, schedules
  // ResetDuplicateRemoteIds to be run asynchronously.
  void ScheduleResetDuplicateRemoteIds();

  // Clears out any duplicate remote ids detected by AddToIdMap calls.
  void ResetDuplicateRemoteIds();

  // Sets the NEEDS_OFFLINE_PROCESSING flag on the given node.
  void SetNeedsOfflineProcessing(const BookmarkNode* node);

  // Helper method for setting a meta info field on a node. Also updates the
  // version field.
  void SetMetaInfo(const BookmarkNode* node,
                   const std::string& field,
                   const std::string& value);

  // Helper method for setting multiple meta info fields at once. All the fields
  // in |meta_info| will be set, but the method will not delete fields not
  // present.
  void SetMultipleMetaInfo(const BookmarkNode* node,
                           BookmarkNode::MetaInfoMap meta_info);

  bookmarks::BookmarkModel* bookmark_model_;
  bool loaded_;

  ObserverList<EnhancedBookmarkModelObserver> observers_;

  IdToNodeMap id_map_;
  NodeToIdMap nodes_to_reset_;

  // Pending SetNeedsOfflineProcessing calls are stored here, as they may need
  // to be cancelled if the node is removed.
  std::map<const BookmarkNode*, linked_ptr<base::CancelableClosure>>
      set_needs_offline_processing_tasks_;

  // Caches the remote id of a node before its meta info changes.
  std::string prev_remote_id_;

  std::string version_;
  std::string version_suffix_;

  base::WeakPtrFactory<EnhancedBookmarkModel> weak_ptr_factory_;
};

}  // namespace enhanced_bookmarks

#endif  // COMPONENTS_ENHANCED_BOOKMARKS_ENHANCED_BOOKMARK_MODEL_H_