Home | History | Annotate | Download | only in glue
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
      6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
     12 #include "chrome/browser/sync/glue/sync_backend_host.h"
     13 #include "components/bookmarks/browser/bookmark_model_observer.h"
     14 #include "components/bookmarks/browser/bookmark_node.h"
     15 #include "components/sync_driver/change_processor.h"
     16 #include "components/sync_driver/data_type_error_handler.h"
     17 
     18 class Profile;
     19 
     20 namespace base {
     21 class RefCountedMemory;
     22 }
     23 
     24 namespace syncer {
     25 class WriteNode;
     26 class WriteTransaction;
     27 }  // namespace syncer
     28 
     29 namespace browser_sync {
     30 
     31 // This class is responsible for taking changes from the BookmarkModel
     32 // and applying them to the sync API 'syncable' model, and vice versa.
     33 // All operations and use of this class are from the UI thread.
     34 // This is currently bookmarks specific.
     35 class BookmarkChangeProcessor : public BookmarkModelObserver,
     36                                 public ChangeProcessor {
     37  public:
     38   BookmarkChangeProcessor(Profile* profile,
     39                           BookmarkModelAssociator* model_associator,
     40                           DataTypeErrorHandler* error_handler);
     41   virtual ~BookmarkChangeProcessor();
     42 
     43   // BookmarkModelObserver implementation.
     44   // BookmarkModel -> sync API model change application.
     45   virtual void BookmarkModelLoaded(BookmarkModel* model,
     46                                    bool ids_reassigned) OVERRIDE;
     47   virtual void BookmarkModelBeingDeleted(BookmarkModel* model) OVERRIDE;
     48   virtual void BookmarkNodeMoved(BookmarkModel* model,
     49                                  const BookmarkNode* old_parent,
     50                                  int old_index,
     51                                  const BookmarkNode* new_parent,
     52                                  int new_index) OVERRIDE;
     53   virtual void BookmarkNodeAdded(BookmarkModel* model,
     54                                  const BookmarkNode* parent,
     55                                  int index) OVERRIDE;
     56   virtual void BookmarkNodeRemoved(BookmarkModel* model,
     57                                    const BookmarkNode* parent,
     58                                    int index,
     59                                    const BookmarkNode* node,
     60                                    const std::set<GURL>& removed_urls) OVERRIDE;
     61   virtual void BookmarkAllUserNodesRemoved(
     62       BookmarkModel* model,
     63       const std::set<GURL>& removed_urls) OVERRIDE;
     64   virtual void BookmarkNodeChanged(BookmarkModel* model,
     65                                    const BookmarkNode* node) OVERRIDE;
     66   virtual void BookmarkMetaInfoChanged(BookmarkModel* model,
     67                                        const BookmarkNode* node) OVERRIDE;
     68   virtual void BookmarkNodeFaviconChanged(BookmarkModel* model,
     69                                           const BookmarkNode* node) OVERRIDE;
     70   virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
     71                                              const BookmarkNode* node) OVERRIDE;
     72 
     73   // The change processor implementation, responsible for applying changes from
     74   // the sync model to the bookmarks model.
     75   virtual void ApplyChangesFromSyncModel(
     76       const syncer::BaseTransaction* trans,
     77       int64 model_version,
     78       const syncer::ImmutableChangeRecordList& changes) OVERRIDE;
     79 
     80   // The following methods are static and hence may be invoked at any time, and
     81   // do not depend on having a running ChangeProcessor.
     82 
     83   // Updates the title, URL, creation time and favicon of the bookmark |node|
     84   // with data taken from the |sync_node| sync node.
     85   static void UpdateBookmarkWithSyncData(
     86       const syncer::BaseNode& sync_node,
     87       BookmarkModel* model,
     88       const BookmarkNode* node,
     89       Profile* profile);
     90 
     91   // Creates a bookmark node under the given parent node from the given sync
     92   // node. Returns the newly created node.  The created node is placed at the
     93   // specified index among the parent's children.
     94   static const BookmarkNode* CreateBookmarkNode(
     95       syncer::BaseNode* sync_node,
     96       const BookmarkNode* parent,
     97       BookmarkModel* model,
     98       Profile* profile,
     99       int index);
    100 
    101   // Sets the favicon of the given bookmark node from the given sync node.
    102   // Returns whether the favicon was set in the bookmark node.
    103   // |profile| is the profile that contains the HistoryService and BookmarkModel
    104   // for the bookmark in question.
    105   static bool SetBookmarkFavicon(const syncer::BaseNode* sync_node,
    106                                  const BookmarkNode* bookmark_node,
    107                                  BookmarkModel* model,
    108                                  Profile* profile);
    109 
    110   // Applies the 1x favicon |bitmap_data| and |icon_url| to |bookmark_node|.
    111   // |profile| is the profile that contains the HistoryService and BookmarkModel
    112   // for the bookmark in question.
    113   static void ApplyBookmarkFavicon(
    114       const BookmarkNode* bookmark_node,
    115       Profile* profile,
    116       const GURL& icon_url,
    117       const scoped_refptr<base::RefCountedMemory>& bitmap_data);
    118 
    119   // Sets the favicon of the given sync node from the given bookmark node.
    120   static void SetSyncNodeFavicon(const BookmarkNode* bookmark_node,
    121                                  BookmarkModel* model,
    122                                  syncer::WriteNode* sync_node);
    123 
    124   // Treat the |index|th child of |parent| as a newly added node, and create a
    125   // corresponding node in the sync domain using |trans|.  All properties
    126   // will be transferred to the new node.  A node corresponding to |parent|
    127   // must already exist and be associated for this call to succeed.  Returns
    128   // the ID of the just-created node, or if creation fails, kInvalidID.
    129   static int64 CreateSyncNode(const BookmarkNode* parent,
    130                               BookmarkModel* model,
    131                               int index,
    132                               syncer::WriteTransaction* trans,
    133                               BookmarkModelAssociator* associator,
    134                               DataTypeErrorHandler* error_handler);
    135 
    136   // Update |bookmark_node|'s sync node.
    137   static int64 UpdateSyncNode(const BookmarkNode* bookmark_node,
    138                               BookmarkModel* model,
    139                               syncer::WriteTransaction* trans,
    140                               BookmarkModelAssociator* associator,
    141                               DataTypeErrorHandler* error_handler);
    142 
    143   // Update transaction version of |model| and |nodes| to |new_version| if
    144   // it's valid.
    145   static void UpdateTransactionVersion(
    146       int64 new_version,
    147       BookmarkModel* model,
    148       const std::vector<const BookmarkNode*>& nodes);
    149 
    150  protected:
    151   virtual void StartImpl() OVERRIDE;
    152 
    153  private:
    154   enum MoveOrCreate {
    155     MOVE,
    156     CREATE,
    157   };
    158 
    159   // Retrieves the meta info from the given sync node.
    160   static scoped_ptr<BookmarkNode::MetaInfoMap> GetBookmarkMetaInfo(
    161       const syncer::BaseNode* sync_node);
    162 
    163   // Sets the meta info of the given sync node from the given bookmark node.
    164   static void SetSyncNodeMetaInfo(const BookmarkNode* node,
    165                                   syncer::WriteNode* sync_node);
    166 
    167   // Helper function used to fix the position of a sync node so that it matches
    168   // the position of a corresponding bookmark model node. |parent| and
    169   // |index| identify the bookmark model position.  |dst| is the node whose
    170   // position is to be fixed.  If |operation| is CREATE, treat |dst| as an
    171   // uncreated node and set its position via InitByCreation(); otherwise,
    172   // |dst| is treated as an existing node, and its position will be set via
    173   // SetPosition().  |trans| is the transaction to which |dst| belongs. Returns
    174   // false on failure.
    175   static bool PlaceSyncNode(MoveOrCreate operation,
    176                             const BookmarkNode* parent,
    177                             int index,
    178                             syncer::WriteTransaction* trans,
    179                             syncer::WriteNode* dst,
    180                             BookmarkModelAssociator* associator);
    181 
    182   // Copy properties (but not position) from |src| to |dst|.
    183   static void UpdateSyncNodeProperties(const BookmarkNode* src,
    184                                        BookmarkModel* model,
    185                                        syncer::WriteNode* dst);
    186 
    187   // Helper function to encode a bookmark's favicon into raw PNG data.
    188   static void EncodeFavicon(const BookmarkNode* src,
    189                             BookmarkModel* model,
    190                             scoped_refptr<base::RefCountedMemory>* dst);
    191 
    192   // Remove |sync_node|. It should not have any children
    193   void RemoveOneSyncNode(syncer::WriteNode* sync_node);
    194 
    195   // Remove all sync nodes, except the permanent nodes.
    196   void RemoveAllSyncNodes();
    197 
    198   // Remove all children of the bookmark node with bookmark node id:
    199   // |topmost_node_id|.
    200   void RemoveAllChildNodes(syncer::WriteTransaction* trans,
    201                            const int64& topmost_node_id);
    202 
    203   // Remove all the sync nodes associated with |node| and its children.
    204   void RemoveSyncNodeHierarchy(const BookmarkNode* node);
    205 
    206   // Creates or updates a sync node associated with |node|.
    207   void CreateOrUpdateSyncNode(const BookmarkNode* node);
    208 
    209   // The bookmark model we are processing changes from.  Non-NULL when
    210   // |running_| is true.
    211   BookmarkModel* bookmark_model_;
    212 
    213   Profile* profile_;
    214 
    215   // The two models should be associated according to this ModelAssociator.
    216   BookmarkModelAssociator* model_associator_;
    217 
    218   DISALLOW_COPY_AND_ASSIGN(BookmarkChangeProcessor);
    219 };
    220 
    221 }  // namespace browser_sync
    222 
    223 #endif  // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_CHANGE_PROCESSOR_H_
    224