bookmark_model_type_controller.cc 5.36 KB
Newer Older
1 2 3 4 5 6
// Copyright 2017 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 "components/sync_bookmarks/bookmark_model_type_controller.h"

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <utility>

#include "base/threading/thread_task_runner_handle.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/history/core/browser/history_service.h"
#include "components/sync/driver/sync_client.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/engine/model_type_configurer.h"
#include "components/sync/engine/model_type_processor_proxy.h"
#include "components/sync/model/sync_error.h"
#include "components/sync/protocol/model_type_state.pb.h"
#include "components/sync/protocol/sync.pb.h"
#include "components/sync/syncable/directory.h"
#include "components/sync/syncable/user_share.h"
#include "components/sync_bookmarks/bookmark_model_type_processor.h"

using syncer::SyncError;

25 26
namespace sync_bookmarks {

27 28 29 30 31 32 33
BookmarkModelTypeController::BookmarkModelTypeController(
    syncer::SyncClient* sync_client)
    : DataTypeController(syncer::BOOKMARKS),
      sync_client_(sync_client),
      state_(NOT_RUNNING) {}

BookmarkModelTypeController::~BookmarkModelTypeController() = default;
34 35

bool BookmarkModelTypeController::ShouldLoadModelBeforeConfigure() const {
36 37
  DCHECK(CalledOnValidThread());
  return true;
38 39 40 41
}

void BookmarkModelTypeController::BeforeLoadModels(
    syncer::ModelTypeConfigurer* configurer) {
42
  DCHECK(CalledOnValidThread());
43 44 45 46
}

void BookmarkModelTypeController::LoadModels(
    const ModelLoadCallback& model_load_callback) {
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  DCHECK(CalledOnValidThread());
  if (state() != NOT_RUNNING) {
    model_load_callback.Run(type(),
                            SyncError(FROM_HERE, SyncError::DATATYPE_ERROR,
                                      "Model already running", type()));
    return;
  }

  state_ = MODEL_STARTING;

  if (DependenciesLoaded()) {
    state_ = MODEL_LOADED;
    model_load_callback.Run(type(), SyncError());
  } else {
    // TODO(pavely): Subscribe for BookmarkModel and HistoryService
    // notifications.
    NOTIMPLEMENTED();
  }
65 66 67 68 69
}

void BookmarkModelTypeController::RegisterWithBackend(
    base::Callback<void(bool)> set_downloaded,
    syncer::ModelTypeConfigurer* configurer) {
70 71 72 73 74 75 76
  DCHECK(CalledOnValidThread());
  DCHECK(configurer);
  std::unique_ptr<syncer::ActivationContext> activation_context =
      PrepareActivationContext();
  set_downloaded.Run(activation_context->model_type_state.initial_sync_done());
  configurer->ActivateNonBlockingDataType(type(),
                                          std::move(activation_context));
77 78 79 80
}

void BookmarkModelTypeController::StartAssociating(
    const StartCallback& start_callback) {
81 82 83 84 85 86 87 88 89
  DCHECK(CalledOnValidThread());
  DCHECK(!start_callback.is_null());
  DCHECK_EQ(MODEL_LOADED, state_);

  state_ = RUNNING;

  // There is no association, just call back promptly.
  syncer::SyncMergeResult merge_result(type());
  start_callback.Run(OK, merge_result, merge_result);
90 91 92 93
}

void BookmarkModelTypeController::ActivateDataType(
    syncer::ModelTypeConfigurer* configurer) {
94
  DCHECK(CalledOnValidThread());
95 96 97 98 99
  NOTIMPLEMENTED();
}

void BookmarkModelTypeController::DeactivateDataType(
    syncer::ModelTypeConfigurer* configurer) {
100
  DCHECK(CalledOnValidThread());
101 102 103 104
  NOTIMPLEMENTED();
}

void BookmarkModelTypeController::Stop() {
105
  DCHECK(CalledOnValidThread());
106 107 108 109
  NOTIMPLEMENTED();
}

syncer::DataTypeController::State BookmarkModelTypeController::state() const {
110 111
  DCHECK(CalledOnValidThread());
  return state_;
112 113 114 115
}

void BookmarkModelTypeController::GetAllNodes(
    const AllNodesCallback& callback) {
116
  DCHECK(CalledOnValidThread());
117 118 119 120 121
  NOTIMPLEMENTED();
}

void BookmarkModelTypeController::GetStatusCounters(
    const StatusCountersCallback& callback) {
122
  DCHECK(CalledOnValidThread());
123 124 125 126
  NOTIMPLEMENTED();
}

void BookmarkModelTypeController::RecordMemoryUsageHistogram() {
127
  DCHECK(CalledOnValidThread());
128 129 130
  NOTIMPLEMENTED();
}

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
bool BookmarkModelTypeController::DependenciesLoaded() {
  DCHECK(CalledOnValidThread());
  bookmarks::BookmarkModel* bookmark_model = sync_client_->GetBookmarkModel();
  if (!bookmark_model || !bookmark_model->loaded())
    return false;

  history::HistoryService* history_service = sync_client_->GetHistoryService();
  if (!history_service || !history_service->BackendLoaded())
    return false;

  return true;
}

std::unique_ptr<syncer::ActivationContext>
BookmarkModelTypeController::PrepareActivationContext() {
  DCHECK(!model_type_processor_);

  syncer::UserShare* user_share =
      sync_client_->GetSyncService()->GetUserShare();
  syncer::syncable::Directory* directory = user_share->directory.get();

  std::unique_ptr<syncer::ActivationContext> activation_context =
      std::make_unique<syncer::ActivationContext>();

  directory->GetDownloadProgress(
      type(), activation_context->model_type_state.mutable_progress_marker());
  activation_context->model_type_state.set_initial_sync_done(
      directory->InitialSyncEndedForType(type()));
  // TODO(pavely): Populate model_type_state.type_context.

  model_type_processor_ = std::make_unique<BookmarkModelTypeProcessor>();
  activation_context->type_processor =
      std::make_unique<syncer::ModelTypeProcessorProxy>(
          model_type_processor_->GetWeakPtr(),
          base::ThreadTaskRunnerHandle::Get());
  return activation_context;
}

169
}  // namespace sync_bookmarks