Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / password_syncable_service.h
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_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__
7
8 #if !defined(PASSWORD_MANAGER_ENABLE_SYNC)
9 #error "Only build this file when sync is enabled in Password Manager."
10 #endif
11
12 #include <string>
13 #include <vector>
14
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "components/password_manager/core/browser/password_store_change.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_data.h"
20 #include "sync/api/sync_error.h"
21 #include "sync/api/syncable_service.h"
22 #include "sync/protocol/password_specifics.pb.h"
23 #include "sync/protocol/sync.pb.h"
24
25 namespace autofill {
26 struct PasswordForm;
27 }
28
29 namespace syncer {
30 class SyncErrorFactory;
31 }
32
33 namespace password_manager {
34
35 class PasswordStoreSync;
36
37 // The implementation of the SyncableService API for passwords.
38 class PasswordSyncableService : public syncer::SyncableService,
39                                 public base::NonThreadSafe {
40  public:
41   // Since the constructed |PasswordSyncableService| is typically owned by the
42   // |password_store|, the constructor doesn't take ownership of the
43   // |password_store|.
44   explicit PasswordSyncableService(PasswordStoreSync* password_store);
45   ~PasswordSyncableService() override;
46
47   // syncer::SyncableService:
48   syncer::SyncMergeResult MergeDataAndStartSyncing(
49       syncer::ModelType type,
50       const syncer::SyncDataList& initial_sync_data,
51       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
52       scoped_ptr<syncer::SyncErrorFactory> error_handler) override;
53   void StopSyncing(syncer::ModelType type) override;
54   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override;
55   syncer::SyncError ProcessSyncChanges(
56       const tracked_objects::Location& from_here,
57       const syncer::SyncChangeList& change_list) override;
58
59   // Notifies the Sync engine of changes to the password database.
60   void ActOnPasswordStoreChanges(const PasswordStoreChangeList& changes);
61
62   // Provides a StartSyncFlare to the SyncableService. See
63   // chrome/browser/sync/glue/sync_start_util.h for more.
64   void InjectStartSyncFlare(
65       const syncer::SyncableService::StartSyncFlare& flare);
66
67  private:
68   typedef std::vector<autofill::PasswordForm*> PasswordForms;
69   // Map from password sync tag to password form.
70   typedef std::map<std::string, autofill::PasswordForm*> PasswordEntryMap;
71
72   // The type of PasswordStoreSync::AddLoginImpl,
73   // PasswordStoreSync::UpdateLoginImpl and PasswordStoreSync::RemoveLoginImpl.
74   typedef PasswordStoreChangeList(PasswordStoreSync::*DatabaseOperation)(
75       const autofill::PasswordForm& form);
76
77   struct SyncEntries;
78
79   // Retrieves the entries from password db and fills both |password_entries|
80   // and |passwords_entry_map|. |passwords_entry_map| can be NULL.
81   bool ReadFromPasswordStore(
82       ScopedVector<autofill::PasswordForm>* password_entries,
83       PasswordEntryMap* passwords_entry_map) const;
84
85   // Uses the |PasswordStore| APIs to change entries.
86   void WriteToPasswordStore(const SyncEntries& entries);
87
88   // Examines |data|, an entry in sync db, and updates |sync_entries| or
89   // |updated_db_entries| accordingly. An element is removed from
90   // |unmatched_data_from_password_db| if its tag is identical to |data|'s.
91   void CreateOrUpdateEntry(
92       const syncer::SyncData& data,
93       PasswordEntryMap* unmatched_data_from_password_db,
94       SyncEntries* sync_entries,
95       syncer::SyncChangeList* updated_db_entries);
96
97   // Calls |operation| for each element in |entries| and appends the changes to
98   // |all_changes|.
99   void WriteEntriesToDatabase(
100       DatabaseOperation operation,
101       const PasswordForms& entries,
102       PasswordStoreChangeList* all_changes);
103
104   // The factory that creates sync errors. |SyncError| has rich data
105   // suitable for debugging.
106   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
107
108   // |sync_processor_| will mirror the |PasswordStore| changes in the sync db.
109   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
110
111   // The password store that adds/updates/deletes password entries. Not owned.
112   PasswordStoreSync* const password_store_;
113
114   // A signal activated by this class to start sync as soon as possible.
115   syncer::SyncableService::StartSyncFlare flare_;
116
117   // True if processing sync changes is in progress.
118   bool is_processing_sync_changes_;
119
120   DISALLOW_COPY_AND_ASSIGN(PasswordSyncableService);
121 };
122
123 }  // namespace password_manager
124
125 #endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_SYNCABLE_SERVICE_H__