Logo Search packages:      
Sourcecode: chromium-browser version File versions  Download package


// Copyright (c) 2010 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.


#include <map>
#include <set>
#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/scoped_ptr.h"
#include "base/task.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/sync/glue/model_associator.h"
#include "chrome/browser/sync/protocol/typed_url_specifics.pb.h"

class GURL;
class MessageLoop;
class ProfileSyncService;

namespace history {
class HistoryBackend;
class URLRow;

namespace sync_api {
class WriteNode;
class WriteTransaction;

namespace browser_sync {

class TypedUrlChangeProcessor;
class UnrecoverableErrorHandler;

extern const char kTypedUrlTag[];

// Contains all model association related logic:
// * Algorithm to associate typed_url model and sync model.
// * Persisting model associations and loading them back.
// We do not check if we have local data before this run; we always
// merge and sync.
class TypedUrlModelAssociator
  : public PerDataTypeAssociatorInterface<std::string, std::string> {
  typedef std::vector<std::pair<GURL, std::wstring> > TypedUrlTitleVector;
  typedef std::vector<history::URLRow> TypedUrlVector;
  typedef std::vector<std::pair<history::URLID, history::URLRow> >

  static syncable::ModelType model_type() { return syncable::TYPED_URLS; }
  TypedUrlModelAssociator(ProfileSyncService* sync_service,
                          history::HistoryBackend* history_backend,
                          UnrecoverableErrorHandler* error_handler);
  virtual ~TypedUrlModelAssociator() { }

  // PerDataTypeAssociatorInterface implementation.
  // Iterates through the sync model looking for matched pairs of items.
  virtual bool AssociateModels();

  // Delete all typed url nodes.
  bool DeleteAllNodes(sync_api::WriteTransaction* trans);

  // Clears all associations.
  virtual bool DisassociateModels();

  // The has_nodes out param is true if the sync model has nodes other
  // than the permanent tagged nodes.
  virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes);

  // The has_nodes out param is true if the autofill model has any
  // user-defined typed_url entries.
  virtual bool ChromeModelHasUserCreatedNodes(bool* has_nodes);

  virtual void AbortAssociation() {
    // TODO(zork): Implement this.

  // Not implemented.
  virtual const std::string* GetChromeNodeFromSyncId(int64 sync_id) {
    return NULL;

  // Not implemented.
  virtual bool InitSyncNodeFromChromeId(std::string node_id,
                                        sync_api::BaseNode* sync_node) {
    return false;

  // Returns the sync id for the given typed_url name, or sync_api::kInvalidId
  // if the typed_url name is not associated to any sync id.
  virtual int64 GetSyncIdFromChromeId(std::string node_id);

  // Associates the given typed_url name with the given sync id.
  virtual void Associate(const std::string* node, int64 sync_id);

  // Remove the association that corresponds to the given sync id.
  virtual void Disassociate(int64 sync_id);

  // Returns whether a node with the given permanent tag was found and update
  // |sync_id| with that node's id.
  virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id);

  void WriteToHistoryBackend(const TypedUrlTitleVector* titles,
                             const TypedUrlVector* new_urls,
                             const TypedUrlUpdateVector* updated_urls);

  enum {
    DIFF_NONE           = 0x0000,
    DIFF_NODE_CHANGED   = 0x0001,
    DIFF_TITLE_CHANGED  = 0x0002,
    DIFF_ROW_CHANGED    = 0x0004,

  static int MergeUrls(const sync_pb::TypedUrlSpecifics& typed_url,
                       const history::URLRow& url,
                       history::URLRow* new_url);
  static void WriteToSyncNode(const history::URLRow& url,
                              sync_api::WriteNode* node);

  // Returns sync service instance.
  ProfileSyncService* sync_service() { return sync_service_; }

  typedef std::map<std::string, int64> TypedUrlToSyncIdMap;
  typedef std::map<int64, std::string> SyncIdToTypedUrlMap;

  ProfileSyncService* sync_service_;
  history::HistoryBackend* history_backend_;
  UnrecoverableErrorHandler* error_handler_;
  int64 typed_url_node_id_;

  MessageLoop* expected_loop_;

  TypedUrlToSyncIdMap id_map_;
  SyncIdToTypedUrlMap id_map_inverse_;


}  // namespace browser_sync


Generated by  Doxygen 1.6.0   Back to index