summaryrefslogtreecommitdiff
path: root/chromium/components/sync_bookmarks/bookmark_model_type_controller.h
blob: 42451be5466d648216e82b83015597240e800455 (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
// 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.

#ifndef COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_CONTROLLER_H_
#define COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_CONTROLLER_H_

#include <memory>

#include "base/macros.h"
#include "components/sync/driver/data_type_controller.h"
#include "components/sync/engine/activation_context.h"

namespace syncer {
class SyncClient;
}  // namespace syncer

namespace sync_bookmarks {

class BookmarkModelTypeProcessor;

// A class that manages the startup and shutdown of bookmark sync implemented
// through USS APIs.
class BookmarkModelTypeController : public syncer::DataTypeController {
 public:
  explicit BookmarkModelTypeController(syncer::SyncClient* sync_client);
  ~BookmarkModelTypeController() override;

  // syncer::DataTypeController implementation.
  bool ShouldLoadModelBeforeConfigure() const override;
  void BeforeLoadModels(syncer::ModelTypeConfigurer* configurer) override;
  void LoadModels(const ModelLoadCallback& model_load_callback) override;
  void RegisterWithBackend(base::Callback<void(bool)> set_downloaded,
                           syncer::ModelTypeConfigurer* configurer) override;
  void StartAssociating(const StartCallback& start_callback) override;
  void ActivateDataType(syncer::ModelTypeConfigurer* configurer) override;
  void DeactivateDataType(syncer::ModelTypeConfigurer* configurer) override;
  void Stop() override;
  State state() const override;
  void GetAllNodes(const AllNodesCallback& callback) override;
  void GetStatusCounters(const StatusCountersCallback& callback) override;
  void RecordMemoryUsageHistogram() override;

 private:
  friend class BookmarkModelTypeControllerTest;

  // Returns true if both BookmarkModel and HistoryService are loaded.
  bool DependenciesLoaded();

  // Reads ModelTypeState from storage and creates BookmarkModelTypeProcessor.
  std::unique_ptr<syncer::ActivationContext> PrepareActivationContext();

  // SyncClient provides access to BookmarkModel, HistoryService and
  // SyncService.
  syncer::SyncClient* sync_client_;

  // State of this datatype controller.
  State state_;

  // BookmarkModelTypeProcessor handles communications between sync engine and
  // BookmarkModel/HistoryService.
  std::unique_ptr<BookmarkModelTypeProcessor> model_type_processor_;

  // This is a hack to prevent reconfigurations from crashing, because USS
  // activation is not idempotent. RegisterWithBackend only needs to actually do
  // something the first time after the type is enabled.
  // TODO(crbug.com/647505): Remove this once the DTM handles things better.
  bool activated_ = false;

  DISALLOW_COPY_AND_ASSIGN(BookmarkModelTypeController);
};

}  // namespace sync_bookmarks

#endif  // COMPONENTS_SYNC_BOOKMARKS_BOOKMARK_MODEL_TYPE_CONTROLLER_H_