1 // Copyright 2014 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 COMPONENTS_SYNC_ENGINE_IMPL_MODEL_TYPE_REGISTRY_H_
6 #define COMPONENTS_SYNC_ENGINE_IMPL_MODEL_TYPE_REGISTRY_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "base/memory/weak_ptr.h"
15 #include "components/sync/base/model_type.h"
16 #include "components/sync/base/passphrase_enums.h"
17 #include "components/sync/engine/commit_and_get_updates_types.h"
18 #include "components/sync/engine/model_type_connector.h"
19 #include "components/sync/engine/sync_encryption_handler.h"
20 #include "components/sync/engine_impl/nudge_handler.h"
21 
22 namespace syncer {
23 
24 class CancelationSignal;
25 class CommitContributor;
26 class KeystoreKeysHandler;
27 class ModelTypeWorker;
28 class UpdateHandler;
29 
30 using UpdateHandlerMap = std::map<ModelType, UpdateHandler*>;
31 using CommitContributorMap = std::map<ModelType, CommitContributor*>;
32 
33 // Keeps track of the sets of active update handlers and commit contributors.
34 class ModelTypeRegistry : public ModelTypeConnector,
35                           public SyncEncryptionHandler::Observer {
36  public:
37   ModelTypeRegistry(NudgeHandler* nudge_handler,
38                     CancelationSignal* cancelation_signal,
39                     KeystoreKeysHandler* keystore_keys_handler);
40   ~ModelTypeRegistry() override;
41 
42   // Implementation of ModelTypeConnector.
43   void ConnectDataType(
44       ModelType type,
45       std::unique_ptr<DataTypeActivationResponse> activation_response) override;
46   void DisconnectDataType(ModelType type) override;
47   void ConnectProxyType(ModelType type) override;
48   void DisconnectProxyType(ModelType type) override;
49 
50   // Implementation of SyncEncryptionHandler::Observer.
51   void OnPassphraseRequired(
52       const KeyDerivationParams& key_derivation_params,
53       const sync_pb::EncryptedData& pending_keys) override;
54   void OnPassphraseAccepted() override;
55   void OnTrustedVaultKeyRequired() override;
56   void OnTrustedVaultKeyAccepted() override;
57   void OnBootstrapTokenUpdated(const std::string& bootstrap_token,
58                                BootstrapTokenType type) override;
59   void OnEncryptedTypesChanged(ModelTypeSet encrypted_types,
60                                bool encrypt_everything) override;
61   void OnCryptographerStateChanged(Cryptographer* cryptographer,
62                                    bool has_pending_keys) override;
63   void OnPassphraseTypeChanged(PassphraseType type,
64                                base::Time passphrase_time) override;
65 
66   // Gets the set of enabled types.
67   ModelTypeSet GetEnabledTypes() const;
68 
69   // Returns set of types for which initial set of updates was downloaded and
70   // applied.
71   ModelTypeSet GetInitialSyncEndedTypes() const;
72 
73   // Returns set of enabled types, i.e. types that has alive ModelTypeWorker.
74   ModelTypeSet GetEnabledDataTypes() const;
75 
76   // Returns the update handler for |type|.
77   const UpdateHandler* GetUpdateHandler(ModelType type) const;
78 
79   // Simple getters.
80   UpdateHandlerMap* update_handler_map();
81   CommitContributorMap* commit_contributor_map();
82   KeystoreKeysHandler* keystore_keys_handler();
83 
84   bool HasUnsyncedItems() const;
85 
86   base::WeakPtr<ModelTypeConnector> AsWeakPtr();
87 
88  private:
89   void OnEncryptionStateChanged();
90 
91   // Enabled proxy types, which don't have a worker.
92   ModelTypeSet enabled_proxy_types_;
93 
94   std::vector<std::unique_ptr<ModelTypeWorker>> model_type_workers_;
95 
96   // Maps of UpdateHandlers and CommitContributors.
97   // They do not own any of the objects they point to.
98   UpdateHandlerMap update_handler_map_;
99   CommitContributorMap commit_contributor_map_;
100 
101   // A copy of the most recent cryptographer.
102   std::unique_ptr<Cryptographer> cryptographer_;
103 
104   // A copy of the most recent passphrase type.
105   PassphraseType passphrase_type_ =
106       SyncEncryptionHandler::kInitialPassphraseType;
107 
108   // The set of encrypted types.
109   ModelTypeSet encrypted_types_;
110 
111   NudgeHandler* const nudge_handler_;
112 
113   // CancelationSignal is signalled on engine shutdown. It is passed to
114   // ModelTypeWorker to cancel blocking operation.
115   CancelationSignal* const cancelation_signal_;
116 
117   KeystoreKeysHandler* const keystore_keys_handler_;
118 
119   base::WeakPtrFactory<ModelTypeRegistry> weak_ptr_factory_{this};
120 
121   DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry);
122 };
123 
124 }  // namespace syncer
125 
126 #endif  // COMPONENTS_SYNC_ENGINE_IMPL_MODEL_TYPE_REGISTRY_H_
127