syncapi.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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// This file defines the "sync API", an interface to the syncer
6// backend that exposes (1) the core functionality of maintaining a consistent
7// local snapshot of a hierarchical object set; (2) a means to transactionally
8// access and modify those objects; (3) a means to control client/server
9// synchronization tasks, namely: pushing local object modifications to a
10// server, pulling nonlocal object modifications from a server to this client,
11// and resolving conflicts that may arise between the two; and (4) an
12// abstraction of some external functionality that is to be provided by the
13// host environment.
14//
15// This interface is used as the entry point into the syncer backend
16// when the backend is compiled as a library and embedded in another
17// application.  A goal for this interface layer is to depend on very few
18// external types, so that an application can use the sync backend
19// without introducing a dependency on specific types.  A non-goal is to
20// have binary compatibility across versions or compilers; this allows the
21// interface to use C++ classes.  An application wishing to use the sync API
22// should ideally compile the syncer backend and this API as part of the
23// application's own build, to avoid e.g. mismatches in calling convention,
24// structure padding, or name mangling that could arise if there were a
25// compiler mismatch.
26//
27// The schema of the objects in the sync domain is based on the model, which
28// is essentially a hierarchy of items and folders similar to a filesystem,
29// but with a few important differences.  The sync API contains fields
30// such as URL to easily allow the embedding application to store web
31// browser bookmarks.  Also, the sync API allows duplicate titles in a parent.
32// Consequently, it does not support looking up an object by title
33// and parent, since such a lookup is not uniquely determined.  Lastly,
34// unlike a filesystem model, objects in the Sync API model have a strict
35// ordering within a parent; the position is manipulable by callers, and
36// children of a node can be enumerated in the order of their position.
37
38#ifndef CHROME_BROWSER_SYNC_ENGINE_SYNCAPI_H_
39#define CHROME_BROWSER_SYNC_ENGINE_SYNCAPI_H_
40#pragma once
41
42#include <string>
43#include <vector>
44
45#include "base/basictypes.h"
46#include "base/gtest_prod_util.h"
47#include "base/scoped_ptr.h"
48#include "build/build_config.h"
49#include "chrome/browser/sync/protocol/password_specifics.pb.h"
50#include "chrome/browser/sync/syncable/autofill_migration.h"
51#include "chrome/browser/sync/syncable/model_type.h"
52#include "chrome/browser/sync/util/cryptographer.h"
53#include "chrome/common/net/gaia/google_service_auth_error.h"
54#include "googleurl/src/gurl.h"
55
56class DictionaryValue;
57class FilePath;
58
59namespace browser_sync {
60class JsBackend;
61class ModelSafeWorkerRegistrar;
62
63namespace sessions {
64struct SyncSessionSnapshot;
65}
66}
67
68namespace notifier {
69struct NotifierOptions;
70}
71
72// Forward declarations of internal class types so that sync API objects
73// may have opaque pointers to these types.
74namespace syncable {
75class BaseTransaction;
76class DirectoryManager;
77class Entry;
78class MutableEntry;
79class ReadTransaction;
80class ScopedDirLookup;
81class WriteTransaction;
82}
83
84namespace sync_pb {
85class AppSpecifics;
86class AutofillSpecifics;
87class AutofillProfileSpecifics;
88class BookmarkSpecifics;
89class EntitySpecifics;
90class ExtensionSpecifics;
91class SessionSpecifics;
92class NigoriSpecifics;
93class PasswordSpecifics;
94class PreferenceSpecifics;
95class PasswordSpecifics;
96class PasswordSpecificsData;
97class ThemeSpecifics;
98class TypedUrlSpecifics;
99}
100
101namespace sync_api {
102
103// Forward declarations of classes to be defined later in this file.
104class BaseTransaction;
105class HttpPostProviderFactory;
106class SyncManager;
107class WriteTransaction;
108
109// A UserShare encapsulates the syncable pieces that represent an authenticated
110// user and their data (share).
111// This encompasses all pieces required to build transaction objects on the
112// syncable share.
113struct UserShare {
114  UserShare();
115  ~UserShare();
116
117  // The DirectoryManager itself, which is the parent of Transactions and can
118  // be shared across multiple threads (unlike Directory).
119  scoped_ptr<syncable::DirectoryManager> dir_manager;
120
121  // The username of the sync user.
122  std::string name;
123};
124
125// Contains everything needed to talk to and identify a user account.
126struct SyncCredentials {
127  std::string email;
128  std::string sync_token;
129};
130
131// A valid BaseNode will never have an ID of zero.
132static const int64 kInvalidId = 0;
133
134// BaseNode wraps syncable::Entry, and corresponds to a single object's state.
135// This, like syncable::Entry, is intended for use on the stack.  A valid
136// transaction is necessary to create a BaseNode or any of its children.
137// Unlike syncable::Entry, a sync API BaseNode is identified primarily by its
138// int64 metahandle, which we call an ID here.
139class BaseNode {
140 public:
141  // All subclasses of BaseNode must provide a way to initialize themselves by
142  // doing an ID lookup.  Returns false on failure.  An invalid or deleted
143  // ID will result in failure.
144  virtual bool InitByIdLookup(int64 id) = 0;
145
146  // All subclasses of BaseNode must also provide a way to initialize themselves
147  // by doing a client tag lookup. Returns false on failure. A deleted node
148  // will return FALSE.
149  virtual bool InitByClientTagLookup(syncable::ModelType model_type,
150      const std::string& tag) = 0;
151
152  // Each object is identified by a 64-bit id (internally, the syncable
153  // metahandle).  These ids are strictly local handles.  They will persist
154  // on this client, but the same object on a different client may have a
155  // different ID value.
156  virtual int64 GetId() const;
157
158  // Returns the modification time of the object (in TimeTicks internal format).
159  int64 GetModificationTime() const;
160
161  // Nodes are hierarchically arranged into a single-rooted tree.
162  // InitByRootLookup on ReadNode allows access to the root. GetParentId is
163  // how you find a node's parent.
164  int64 GetParentId() const;
165
166  // Nodes are either folders or not.  This corresponds to the IS_DIR property
167  // of syncable::Entry.
168  bool GetIsFolder() const;
169
170  // Returns the title of the object.
171  // Uniqueness of the title is not enforced on siblings -- it is not an error
172  // for two children to share a title.
173  std::wstring GetTitle() const;
174
175  // Returns the model type of this object.  The model type is set at node
176  // creation time and is expected never to change.
177  syncable::ModelType GetModelType() const;
178
179  // Getter specific to the BOOKMARK datatype.  Returns protobuf
180  // data.  Can only be called if GetModelType() == BOOKMARK.
181  const sync_pb::BookmarkSpecifics& GetBookmarkSpecifics() const;
182
183  // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above.
184  // Returns the URL of a bookmark object.
185  // TODO(ncarter): Remove this datatype-specific accessor.
186  GURL GetURL() const;
187
188  // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above.
189  // Fill in a vector with the byte data of this node's favicon.  Assumes
190  // that the node is a bookmark.
191  // Favicons are expected to be PNG images, and though no verification is
192  // done on the syncapi client of this, the server may reject favicon updates
193  // that are invalid for whatever reason.
194  // TODO(ncarter): Remove this datatype-specific accessor.
195  void GetFaviconBytes(std::vector<unsigned char>* output) const;
196
197  // Getter specific to the APPS datatype.  Returns protobuf
198  // data.  Can only be called if GetModelType() == APPS.
199  const sync_pb::AppSpecifics& GetAppSpecifics() const;
200
201  // Getter specific to the AUTOFILL datatype.  Returns protobuf
202  // data.  Can only be called if GetModelType() == AUTOFILL.
203  const sync_pb::AutofillSpecifics& GetAutofillSpecifics() const;
204
205  virtual const sync_pb::AutofillProfileSpecifics&
206      GetAutofillProfileSpecifics() const;
207
208  // Getter specific to the NIGORI datatype.  Returns protobuf
209  // data.  Can only be called if GetModelType() == NIGORI.
210  const sync_pb::NigoriSpecifics& GetNigoriSpecifics() const;
211
212  // Getter specific to the PASSWORD datatype.  Returns protobuf
213  // data.  Can only be called if GetModelType() == PASSWORD.
214  const sync_pb::PasswordSpecificsData& GetPasswordSpecifics() const;
215
216  // Getter specific to the PREFERENCE datatype.  Returns protobuf
217  // data.  Can only be called if GetModelType() == PREFERENCE.
218  const sync_pb::PreferenceSpecifics& GetPreferenceSpecifics() const;
219
220  // Getter specific to the THEME datatype.  Returns protobuf
221  // data.  Can only be called if GetModelType() == THEME.
222  const sync_pb::ThemeSpecifics& GetThemeSpecifics() const;
223
224  // Getter specific to the TYPED_URLS datatype.  Returns protobuf
225  // data.  Can only be called if GetModelType() == TYPED_URLS.
226  const sync_pb::TypedUrlSpecifics& GetTypedUrlSpecifics() const;
227
228  // Getter specific to the EXTENSIONS datatype.  Returns protobuf
229  // data.  Can only be called if GetModelType() == EXTENSIONS.
230  const sync_pb::ExtensionSpecifics& GetExtensionSpecifics() const;
231
232  // Getter specific to the SESSIONS datatype.  Returns protobuf
233  // data.  Can only be called if GetModelType() == SESSIONS.
234  const sync_pb::SessionSpecifics& GetSessionSpecifics() const;
235
236  // Returns the local external ID associated with the node.
237  int64 GetExternalId() const;
238
239  // Return the ID of the node immediately before this in the sibling order.
240  // For the first node in the ordering, return 0.
241  int64 GetPredecessorId() const;
242
243  // Return the ID of the node immediately after this in the sibling order.
244  // For the last node in the ordering, return 0.
245  virtual int64 GetSuccessorId() const;
246
247  // Return the ID of the first child of this node.  If this node has no
248  // children, return 0.
249  virtual int64 GetFirstChildId() const;
250
251  // These virtual accessors provide access to data members of derived classes.
252  virtual const syncable::Entry* GetEntry() const = 0;
253  virtual const BaseTransaction* GetTransaction() const = 0;
254
255  // Dumps all node info into a DictionaryValue and returns it.
256  // Transfers ownership of the DictionaryValue to the caller.
257  DictionaryValue* ToValue() const;
258
259 protected:
260  BaseNode();
261  virtual ~BaseNode();
262  // The server has a size limit on client tags, so we generate a fixed length
263  // hash locally. This also ensures that ModelTypes have unique namespaces.
264  static std::string GenerateSyncableHash(syncable::ModelType model_type,
265      const std::string& client_tag);
266
267  // Determines whether part of the entry is encrypted, and if so attempts to
268  // decrypt it. Unless decryption is necessary and fails, this will always
269  // return |true|. If the contents are encrypted, the decrypted data will be
270  // stored in |unencrypted_data_|.
271  // This method is invoked once when the BaseNode is initialized.
272  bool DecryptIfNecessary(syncable::Entry* entry);
273
274  // Returns the unencrypted specifics associated with |entry|. If |entry| was
275  // not encrypted, it directly returns |entry|'s EntitySpecifics. Otherwise,
276  // returns |unencrypted_data_|.
277  // This method is invoked by the datatype specific Get<datatype>Specifics
278  // methods.
279  const sync_pb::EntitySpecifics& GetUnencryptedSpecifics(
280      const syncable::Entry* entry) const;
281
282 private:
283  void* operator new(size_t size);  // Node is meant for stack use only.
284
285  // A holder for the unencrypted data stored in an encrypted node.
286  sync_pb::EntitySpecifics unencrypted_data_;
287
288  // Same as |unencrypted_data_|, but for legacy password encryption.
289  scoped_ptr<sync_pb::PasswordSpecificsData> password_data_;
290
291  friend class SyncApiTest;
292  FRIEND_TEST_ALL_PREFIXES(SyncApiTest, GenerateSyncableHash);
293
294  DISALLOW_COPY_AND_ASSIGN(BaseNode);
295};
296
297// WriteNode extends BaseNode to add mutation, and wraps
298// syncable::MutableEntry. A WriteTransaction is needed to create a WriteNode.
299class WriteNode : public BaseNode {
300 public:
301  // Create a WriteNode using the given transaction.
302  explicit WriteNode(WriteTransaction* transaction);
303  virtual ~WriteNode();
304
305  // A client must use one (and only one) of the following Init variants to
306  // populate the node.
307
308  // BaseNode implementation.
309  virtual bool InitByIdLookup(int64 id);
310  virtual bool InitByClientTagLookup(syncable::ModelType model_type,
311      const std::string& tag);
312
313  // Create a new node with the specified parent and predecessor.  |model_type|
314  // dictates the type of the item, and controls which EntitySpecifics proto
315  // extension can be used with this item.  Use a NULL |predecessor|
316  // to indicate that this is to be the first child.
317  // |predecessor| must be a child of |new_parent| or NULL. Returns false on
318  // failure.
319  bool InitByCreation(syncable::ModelType model_type,
320                      const BaseNode& parent,
321                      const BaseNode* predecessor);
322
323  // Create nodes using this function if they're unique items that
324  // you want to fetch using client_tag. Note that the behavior of these
325  // items is slightly different than that of normal items.
326  // Most importantly, if it exists locally, this function will
327  // actually undelete it
328  // Client unique tagged nodes must NOT be folders.
329  bool InitUniqueByCreation(syncable::ModelType model_type,
330                            const BaseNode& parent,
331                            const std::string& client_tag);
332
333  // Each server-created permanent node is tagged with a unique string.
334  // Look up the node with the particular tag.  If it does not exist,
335  // return false.
336  bool InitByTagLookup(const std::string& tag);
337
338  // These Set() functions correspond to the Get() functions of BaseNode.
339  void SetIsFolder(bool folder);
340  void SetTitle(const std::wstring& title);
341
342  // External ID is a client-only field, so setting it doesn't cause the item to
343  // be synced again.
344  void SetExternalId(int64 external_id);
345
346  // Remove this node and its children.
347  void Remove();
348
349  // Set a new parent and position.  Position is specified by |predecessor|; if
350  // it is NULL, the node is moved to the first position.  |predecessor| must
351  // be a child of |new_parent| or NULL.  Returns false on failure..
352  bool SetPosition(const BaseNode& new_parent, const BaseNode* predecessor);
353
354  // Set the bookmark specifics (url and favicon).
355  // Should only be called if GetModelType() == BOOKMARK.
356  void SetBookmarkSpecifics(const sync_pb::BookmarkSpecifics& specifics);
357
358  // Legacy, bookmark-specific setters that wrap SetBookmarkSpecifics() above.
359  // Should only be called if GetModelType() == BOOKMARK.
360  // TODO(ncarter): Remove these two datatype-specific accessors.
361  void SetURL(const GURL& url);
362  void SetFaviconBytes(const std::vector<unsigned char>& bytes);
363
364  // Set the app specifics (id, update url, enabled state, etc).
365  // Should only be called if GetModelType() == APPS.
366  void SetAppSpecifics(const sync_pb::AppSpecifics& specifics);
367
368  // Set the autofill specifics (name and value).
369  // Should only be called if GetModelType() == AUTOFILL.
370  void SetAutofillSpecifics(const sync_pb::AutofillSpecifics& specifics);
371
372  void SetAutofillProfileSpecifics(
373      const sync_pb::AutofillProfileSpecifics& specifics);
374
375  // Set the nigori specifics.
376  // Should only be called if GetModelType() == NIGORI.
377  void SetNigoriSpecifics(const sync_pb::NigoriSpecifics& specifics);
378
379  // Set the password specifics.
380  // Should only be called if GetModelType() == PASSWORD.
381  void SetPasswordSpecifics(const sync_pb::PasswordSpecificsData& specifics);
382
383  // Set the preference specifics (name and value).
384  // Should only be called if GetModelType() == PREFERENCE.
385  void SetPreferenceSpecifics(const sync_pb::PreferenceSpecifics& specifics);
386
387  // Set the theme specifics (name and value).
388  // Should only be called if GetModelType() == THEME.
389  void SetThemeSpecifics(const sync_pb::ThemeSpecifics& specifics);
390
391  // Set the typed_url specifics (url, title, typed_count, etc).
392  // Should only be called if GetModelType() == TYPED_URLS.
393  void SetTypedUrlSpecifics(const sync_pb::TypedUrlSpecifics& specifics);
394
395  // Set the extension specifics (id, update url, enabled state, etc).
396  // Should only be called if GetModelType() == EXTENSIONS.
397  void SetExtensionSpecifics(const sync_pb::ExtensionSpecifics& specifics);
398
399  // Set the session specifics (windows, tabs, navigations etc.).
400  // Should only be called if GetModelType() == SESSIONS.
401  void SetSessionSpecifics(const sync_pb::SessionSpecifics& specifics);
402
403  // Resets the EntitySpecifics for this node based on the unencrypted data.
404  // Will encrypt if necessary.
405  void ResetFromSpecifics();
406
407  // Implementation of BaseNode's abstract virtual accessors.
408  virtual const syncable::Entry* GetEntry() const;
409
410  virtual const BaseTransaction* GetTransaction() const;
411
412 private:
413  void* operator new(size_t size);  // Node is meant for stack use only.
414
415  // Helper to set model type. This will clear any specifics data.
416  void PutModelType(syncable::ModelType model_type);
417
418  // Helper to set the previous node.
419  void PutPredecessor(const BaseNode* predecessor);
420
421  // Private helpers to set type-specific protobuf data.  These don't
422  // do any checking on the previous modeltype, so they can be used
423  // for internal initialization (you can use them to set the modeltype).
424  // Additionally, they will mark for syncing if the underlying value
425  // changes.
426  void PutAppSpecificsAndMarkForSyncing(
427      const sync_pb::AppSpecifics& new_value);
428  void PutAutofillSpecificsAndMarkForSyncing(
429      const sync_pb::AutofillSpecifics& new_value);
430  void PutAutofillProfileSpecificsAndMarkForSyncing(
431      const sync_pb::AutofillProfileSpecifics& new_value);
432  void PutBookmarkSpecificsAndMarkForSyncing(
433      const sync_pb::BookmarkSpecifics& new_value);
434  void PutNigoriSpecificsAndMarkForSyncing(
435      const sync_pb::NigoriSpecifics& new_value);
436  void PutPasswordSpecificsAndMarkForSyncing(
437      const sync_pb::PasswordSpecifics& new_value);
438  void PutPreferenceSpecificsAndMarkForSyncing(
439      const sync_pb::PreferenceSpecifics& new_value);
440  void PutThemeSpecificsAndMarkForSyncing(
441      const sync_pb::ThemeSpecifics& new_value);
442  void PutTypedUrlSpecificsAndMarkForSyncing(
443      const sync_pb::TypedUrlSpecifics& new_value);
444  void PutExtensionSpecificsAndMarkForSyncing(
445      const sync_pb::ExtensionSpecifics& new_value);
446  void PutSessionSpecificsAndMarkForSyncing(
447      const sync_pb::SessionSpecifics& new_value);
448  void PutSpecificsAndMarkForSyncing(
449      const sync_pb::EntitySpecifics& specifics);
450
451  // Sets IS_UNSYNCED and SYNCING to ensure this entry is considered in an
452  // upcoming commit pass.
453  void MarkForSyncing();
454
455  // Encrypt the specifics if the datatype requries it.
456  void EncryptIfNecessary(sync_pb::EntitySpecifics* new_value);
457
458  // The underlying syncable object which this class wraps.
459  syncable::MutableEntry* entry_;
460
461  // The sync API transaction that is the parent of this node.
462  WriteTransaction* transaction_;
463
464  DISALLOW_COPY_AND_ASSIGN(WriteNode);
465};
466
467// ReadNode wraps a syncable::Entry to provide the functionality of a
468// read-only BaseNode.
469class ReadNode : public BaseNode {
470 public:
471  // Create an unpopulated ReadNode on the given transaction.  Call some flavor
472  // of Init to populate the ReadNode with a database entry.
473  explicit ReadNode(const BaseTransaction* transaction);
474  virtual ~ReadNode();
475
476  // A client must use one (and only one) of the following Init variants to
477  // populate the node.
478
479  // BaseNode implementation.
480  virtual bool InitByIdLookup(int64 id);
481  virtual bool InitByClientTagLookup(syncable::ModelType model_type,
482      const std::string& tag);
483
484  // There is always a root node, so this can't fail.  The root node is
485  // never mutable, so root lookup is only possible on a ReadNode.
486  void InitByRootLookup();
487
488  // Each server-created permanent node is tagged with a unique string.
489  // Look up the node with the particular tag.  If it does not exist,
490  // return false.
491  bool InitByTagLookup(const std::string& tag);
492
493  // Implementation of BaseNode's abstract virtual accessors.
494  virtual const syncable::Entry* GetEntry() const;
495  virtual const BaseTransaction* GetTransaction() const;
496
497 protected:
498  ReadNode();
499
500 private:
501  void* operator new(size_t size);  // Node is meant for stack use only.
502
503  // The underlying syncable object which this class wraps.
504  syncable::Entry* entry_;
505
506  // The sync API transaction that is the parent of this node.
507  const BaseTransaction* transaction_;
508
509  DISALLOW_COPY_AND_ASSIGN(ReadNode);
510};
511
512// Sync API's BaseTransaction, ReadTransaction, and WriteTransaction allow for
513// batching of several read and/or write operations.  The read and write
514// operations are performed by creating ReadNode and WriteNode instances using
515// the transaction. These transaction classes wrap identically named classes in
516// syncable, and are used in a similar way. Unlike syncable::BaseTransaction,
517// whose construction requires an explicit syncable::ScopedDirLookup, a sync
518// API BaseTransaction creates its own ScopedDirLookup implicitly.
519class BaseTransaction {
520 public:
521  // Provide access to the underlying syncable.h objects from BaseNode.
522  virtual syncable::BaseTransaction* GetWrappedTrans() const = 0;
523  const syncable::ScopedDirLookup& GetLookup() const { return *lookup_; }
524  browser_sync::Cryptographer* GetCryptographer() const {
525    return cryptographer_;
526  }
527
528 protected:
529  // The ScopedDirLookup is created in the constructor and destroyed
530  // in the destructor.  Creation of the ScopedDirLookup is not expected
531  // to fail.
532  explicit BaseTransaction(UserShare* share);
533  virtual ~BaseTransaction();
534
535  BaseTransaction() { lookup_= NULL; }
536
537 private:
538  // A syncable ScopedDirLookup, which is the parent of syncable transactions.
539  syncable::ScopedDirLookup* lookup_;
540
541  browser_sync::Cryptographer* cryptographer_;
542
543  DISALLOW_COPY_AND_ASSIGN(BaseTransaction);
544};
545
546// Sync API's ReadTransaction is a read-only BaseTransaction.  It wraps
547// a syncable::ReadTransaction.
548class ReadTransaction : public BaseTransaction {
549 public:
550  // Start a new read-only transaction on the specified repository.
551  explicit ReadTransaction(UserShare* share);
552
553  // Resume the middle of a transaction. Will not close transaction.
554  ReadTransaction(UserShare* share, syncable::BaseTransaction* trans);
555
556  virtual ~ReadTransaction();
557
558  // BaseTransaction override.
559  virtual syncable::BaseTransaction* GetWrappedTrans() const;
560 private:
561  void* operator new(size_t size);  // Transaction is meant for stack use only.
562
563  // The underlying syncable object which this class wraps.
564  syncable::BaseTransaction* transaction_;
565  bool close_transaction_;
566
567  DISALLOW_COPY_AND_ASSIGN(ReadTransaction);
568};
569
570// Sync API's WriteTransaction is a read/write BaseTransaction.  It wraps
571// a syncable::WriteTransaction.
572class WriteTransaction : public BaseTransaction {
573 public:
574  // Start a new read/write transaction.
575  explicit WriteTransaction(UserShare* share);
576  virtual ~WriteTransaction();
577
578  // Provide access to the syncable.h transaction from the API WriteNode.
579  virtual syncable::BaseTransaction* GetWrappedTrans() const;
580  syncable::WriteTransaction* GetWrappedWriteTrans() { return transaction_; }
581
582 protected:
583  WriteTransaction() {}
584
585  void SetTransaction(syncable::WriteTransaction* trans) {
586      transaction_ = trans;}
587
588 private:
589  void* operator new(size_t size);  // Transaction is meant for stack use only.
590
591  // The underlying syncable object which this class wraps.
592  syncable::WriteTransaction* transaction_;
593
594  DISALLOW_COPY_AND_ASSIGN(WriteTransaction);
595};
596
597// SyncManager encapsulates syncable::DirectoryManager and serves as the parent
598// of all other objects in the sync API.  SyncManager is thread-safe.  If
599// multiple threads interact with the same local sync repository (i.e. the
600// same sqlite database), they should share a single SyncManager instance.  The
601// caller should typically create one SyncManager for the lifetime of a user
602// session.
603class SyncManager {
604 public:
605  // SyncInternal contains the implementation of SyncManager, while abstracting
606  // internal types from clients of the interface.
607  class SyncInternal;
608
609  // TODO(zea): One day get passwords playing nicely with the rest of encryption
610  // and get rid of this.
611  class ExtraPasswordChangeRecordData {
612   public:
613    ExtraPasswordChangeRecordData();
614    explicit ExtraPasswordChangeRecordData(
615        const sync_pb::PasswordSpecificsData& data);
616    virtual ~ExtraPasswordChangeRecordData();
617
618    // Transfers ownership of the DictionaryValue to the caller.
619    virtual DictionaryValue* ToValue() const;
620
621    const sync_pb::PasswordSpecificsData& unencrypted() const;
622   private:
623    sync_pb::PasswordSpecificsData unencrypted_;
624  };
625
626  // ChangeRecord indicates a single item that changed as a result of a sync
627  // operation.  This gives the sync id of the node that changed, and the type
628  // of change.  To get the actual property values after an ADD or UPDATE, the
629  // client should get the node with InitByIdLookup(), using the provided id.
630  struct ChangeRecord {
631    enum Action {
632      ACTION_ADD,
633      ACTION_DELETE,
634      ACTION_UPDATE,
635    };
636    ChangeRecord();
637    ~ChangeRecord();
638
639    // Transfers ownership of the DictionaryValue to the caller.
640    DictionaryValue* ToValue(const BaseTransaction* trans) const;
641
642    int64 id;
643    Action action;
644    sync_pb::EntitySpecifics specifics;
645    linked_ptr<ExtraPasswordChangeRecordData> extra;
646  };
647
648  // Status encapsulates detailed state about the internals of the SyncManager.
649  struct Status {
650    // Summary is a distilled set of important information that the end-user may
651    // wish to be informed about (through UI, for example). Note that if a
652    // summary state requires user interaction (such as auth failures), more
653    // detailed information may be contained in additional status fields.
654    enum Summary {
655      // The internal instance is in an unrecognizable state. This should not
656      // happen.
657      INVALID = 0,
658      // Can't connect to server, but there are no pending changes in
659      // our local cache.
660      OFFLINE,
661      // Can't connect to server, and there are pending changes in our
662      // local cache.
663      OFFLINE_UNSYNCED,
664      // Connected and syncing.
665      SYNCING,
666      // Connected, no pending changes.
667      READY,
668      // Internal sync error.
669      CONFLICT,
670      // Can't connect to server, and we haven't completed the initial
671      // sync yet.  So there's nothing we can do but wait for the server.
672      OFFLINE_UNUSABLE,
673
674      SUMMARY_STATUS_COUNT,
675    };
676
677    Summary summary;
678    bool authenticated;      // Successfully authenticated via GAIA.
679    bool server_up;          // True if we have received at least one good
680                             // reply from the server.
681    bool server_reachable;   // True if we received any reply from the server.
682    bool server_broken;      // True of the syncer is stopped because of server
683                             // issues.
684    bool notifications_enabled;  // True only if subscribed for notifications.
685
686    // Notifications counters updated by the actions in synapi.
687    int notifications_received;
688    int notifications_sent;
689
690    // The max number of consecutive errors from any component.
691    int max_consecutive_errors;
692
693    int unsynced_count;
694
695    int conflicting_count;
696    bool syncing;
697    // True after a client has done a first sync.
698    bool initial_sync_ended;
699    // True if any syncer is stuck.
700    bool syncer_stuck;
701
702    // Total updates available.  If zero, nothing left to download.
703    int64 updates_available;
704    // Total updates received by the syncer since browser start.
705    int updates_received;
706
707    // Of updates_received, how many were tombstones.
708    int tombstone_updates_received;
709    bool disk_full;
710  };
711
712  // An interface the embedding application implements to receive notifications
713  // from the SyncManager.  Register an observer via SyncManager::AddObserver.
714  // This observer is an event driven model as the events may be raised from
715  // different internal threads, and simply providing an "OnStatusChanged" type
716  // notification complicates things such as trying to determine "what changed",
717  // if different members of the Status object are modified from different
718  // threads.  This way, the event is explicit, and it is safe for the Observer
719  // to dispatch to a native thread or synchronize accordingly.
720  class Observer {
721   public:
722    // Notify the observer that changes have been applied to the sync model.
723    //
724    // This will be invoked on the same thread as on which ApplyChanges was
725    // called. |changes| is an array of size |change_count|, and contains the
726    // ID of each individual item that was changed. |changes| exists only for
727    // the duration of the call. If items of multiple data types change at
728    // the same time, this method is invoked once per data type and |changes|
729    // is restricted to items of the ModelType indicated by |model_type|.
730    // Because the observer is passed a |trans|, the observer can assume a
731    // read lock on the sync model that will be released after the function
732    // returns.
733    //
734    // The SyncManager constructs |changes| in the following guaranteed order:
735    //
736    // 1. Deletions, from leaves up to parents.
737    // 2. Updates to existing items with synced parents & predecessors.
738    // 3. New items with synced parents & predecessors.
739    // 4. Items with parents & predecessors in |changes|.
740    // 5. Repeat #4 until all items are in |changes|.
741    //
742    // Thus, an implementation of OnChangesApplied should be able to
743    // process the change records in the order without having to worry about
744    // forward dependencies.  But since deletions come before reparent
745    // operations, a delete may temporarily orphan a node that is
746    // updated later in the list.
747    virtual void OnChangesApplied(syncable::ModelType model_type,
748                                  const BaseTransaction* trans,
749                                  const ChangeRecord* changes,
750                                  int change_count) = 0;
751
752    // OnChangesComplete gets called when the TransactionComplete event is
753    // posted (after OnChangesApplied finishes), after the transaction lock
754    // and the change channel mutex are released.
755    //
756    // The purpose of this function is to support processors that require
757    // split-transactions changes. For example, if a model processor wants to
758    // perform blocking I/O due to a change, it should calculate the changes
759    // while holding the transaction lock (from within OnChangesApplied), buffer
760    // those changes, let the transaction fall out of scope, and then commit
761    // those changes from within OnChangesComplete (postponing the blocking
762    // I/O to when it no longer holds any lock).
763    virtual void OnChangesComplete(syncable::ModelType model_type) = 0;
764
765    // A round-trip sync-cycle took place and the syncer has resolved any
766    // conflicts that may have arisen.
767    virtual void OnSyncCycleCompleted(
768        const browser_sync::sessions::SyncSessionSnapshot* snapshot) = 0;
769
770    // Called when user interaction may be required due to an auth problem.
771    virtual void OnAuthError(const GoogleServiceAuthError& auth_error) = 0;
772
773    // Called when a new auth token is provided by the sync server.
774    virtual void OnUpdatedToken(const std::string& token) = 0;
775
776    // Called when user interaction is required to obtain a valid passphrase.
777    // If the passphrase is required to decrypt something that has
778    // already been encrypted (and thus has to match the existing key),
779    // |for_decryption| will be true.  If the passphrase is needed for
780    // encryption, |for_decryption| will be false.
781    virtual void OnPassphraseRequired(bool for_decryption) = 0;
782
783    // Called only by SyncInternal::SetPassphrase to indiciate that an attempted
784    // passphrase failed to decrypt pending keys. This is different from
785    // OnPassphraseRequired in that it denotes we finished an attempt to set
786    // a passphrase. OnPassphraseRequired means we have data we could not
787    // decrypt yet, and can come from numerous places.
788    virtual void OnPassphraseFailed() = 0;
789
790    // Called when the passphrase provided by the user has been accepted and is
791    // now used to encrypt sync data.  |bootstrap_token| is an opaque base64
792    // encoded representation of the key generated by the accepted passphrase,
793    // and is provided to the observer for persistence purposes and use in a
794    // future initialization of sync (e.g. after restart).
795    virtual void OnPassphraseAccepted(const std::string& bootstrap_token) = 0;
796
797    // Called when initialization is complete to the point that SyncManager can
798    // process changes. This does not necessarily mean authentication succeeded
799    // or that the SyncManager is online.
800    // IMPORTANT: Creating any type of transaction before receiving this
801    // notification is illegal!
802    // WARNING: Calling methods on the SyncManager before receiving this
803    // message, unless otherwise specified, produces undefined behavior.
804    virtual void OnInitializationComplete() = 0;
805
806    // The syncer thread has been paused.
807    virtual void OnPaused() = 0;
808
809    // The syncer thread has been resumed.
810    virtual void OnResumed() = 0;
811
812    // We are no longer permitted to communicate with the server. Sync should
813    // be disabled and state cleaned up at once.  This can happen for a number
814    // of reasons, e.g. swapping from a test instance to production, or a
815    // global stop syncing operation has wiped the store.
816    virtual void OnStopSyncingPermanently() = 0;
817
818    // After a request to clear server data, these callbacks are invoked to
819    // indicate success or failure.
820    virtual void OnClearServerDataSucceeded() = 0;
821    virtual void OnClearServerDataFailed() = 0;
822
823    // Called after we finish encrypting all appropriate datatypes.
824    virtual void OnEncryptionComplete(
825        const syncable::ModelTypeSet& encrypted_types) = 0;
826
827   protected:
828    virtual ~Observer();
829  };
830
831  // Create an uninitialized SyncManager.  Callers must Init() before using.
832  SyncManager();
833  virtual ~SyncManager();
834
835  // Initialize the sync manager.  |database_location| specifies the path of
836  // the directory in which to locate a sqlite repository storing the syncer
837  // backend state. Initialization will open the database, or create it if it
838  // does not already exist. Returns false on failure.
839  // |sync_server_and_path| and |sync_server_port| represent the Chrome sync
840  // server to use, and |use_ssl| specifies whether to communicate securely;
841  // the default is false.
842  // |post_factory| will be owned internally and used to create
843  // instances of an HttpPostProvider.
844  // |model_safe_worker| ownership is given to the SyncManager.
845  // |user_agent| is a 7-bit ASCII string suitable for use as the User-Agent
846  // HTTP header. Used internally when collecting stats to classify clients.
847  // |notifier_options| contains options specific to sync notifications.
848  bool Init(const FilePath& database_location,
849            const char* sync_server_and_path,
850            int sync_server_port,
851            bool use_ssl,
852            HttpPostProviderFactory* post_factory,
853            browser_sync::ModelSafeWorkerRegistrar* registrar,
854            const char* user_agent,
855            const SyncCredentials& credentials,
856            const notifier::NotifierOptions& notifier_options,
857            const std::string& restored_key_for_bootstrapping,
858            bool setup_for_test_mode);
859
860  // Returns the username last used for a successful authentication.
861  // Returns empty if there is no such username.
862  const std::string& GetAuthenticatedUsername();
863
864  // Check if the database has been populated with a full "initial" download of
865  // sync items for each data type currently present in the routing info.
866  // Prerequisite for calling this is that OnInitializationComplete has been
867  // called.
868  bool InitialSyncEndedForAllEnabledTypes();
869
870  syncable::AutofillMigrationState GetAutofillMigrationState();
871
872  void SetAutofillMigrationState(
873    syncable::AutofillMigrationState state);
874
875  syncable::AutofillMigrationDebugInfo GetAutofillMigrationDebugInfo();
876
877  void SetAutofillMigrationDebugInfo(
878      syncable::AutofillMigrationDebugInfo::PropertyToSet property_to_set,
879      const syncable::AutofillMigrationDebugInfo& info);
880
881  // Migrate tokens from user settings DB to the token service.
882  void MigrateTokens();
883
884  // Update tokens that we're using in Sync. Email must stay the same.
885  void UpdateCredentials(const SyncCredentials& credentials);
886
887  // Update the set of enabled sync types. Usually called when the user disables
888  // or enables a sync type.
889  void UpdateEnabledTypes(const syncable::ModelTypeSet& types);
890
891  // Start the SyncerThread.
892  // TODO(tim): With the new impl, this would mean starting "NORMAL" operation.
893  // Rename this when switched over or at least update comment.
894  void StartSyncing();
895
896  // Attempt to set the passphrase. If the passphrase is valid,
897  // OnPassphraseAccepted will be fired to notify the ProfileSyncService and the
898  // syncer will be nudged so that any update that was waiting for this
899  // passphrase gets applied as soon as possible.
900  // If the passphrase in invalid, OnPassphraseRequired will be fired.
901  // Calling this metdod again is the appropriate course of action to "retry"
902  // with a new passphrase.
903  // |is_explicit| is true if the call is in response to the user explicitly
904  // setting a passphrase as opposed to implicitly (from the users' perspective)
905  // using their Google Account password.  An implicit SetPassphrase will *not*
906  // *not* override an explicit passphrase set previously.
907  void SetPassphrase(const std::string& passphrase, bool is_explicit);
908
909  // Set the datatypes we want to encrypt and encrypt any nodes as necessary.
910  // Note: |encrypted_types| will be unioned with the current set of encrypted
911  // types, as we do not currently support decrypting datatypes.
912  void EncryptDataTypes(const syncable::ModelTypeSet& encrypted_types);
913
914  // Requests the syncer thread to pause.  The observer's OnPause
915  // method will be called when the syncer thread is paused.  Returns
916  // false if the syncer thread can not be paused (e.g. if it is not
917  // started).
918  // TODO(tim): Deprecated.
919  bool RequestPause();
920
921  // Requests the syncer thread to resume.  The observer's OnResume
922  // method will be called when the syncer thread is resumed.  Returns
923  // false if the syncer thread can not be resumed (e.g. if it is not
924  // paused).
925  // TODO(tim): Deprecated.
926  bool RequestResume();
927
928  // For the new SyncerThread impl, this switches the mode of operation to
929  // CONFIGURATION_MODE and schedules a config task to fetch updates for
930  // |types|. It is an error to call this with legacy SyncerThread in use.
931  void RequestConfig(const syncable::ModelTypeBitSet& types);
932
933  // Request a nudge of the syncer, which will cause the syncer thread
934  // to run at the next available opportunity.
935  void RequestNudge();
936
937  // Request a clearing of all data on the server
938  void RequestClearServerData();
939
940  // Adds a listener to be notified of sync events.
941  // NOTE: It is OK (in fact, it's probably a good idea) to call this before
942  // having received OnInitializationCompleted.
943  void AddObserver(Observer* observer);
944
945  // Remove the given observer.  Make sure to call this if the
946  // Observer is being destroyed so the SyncManager doesn't
947  // potentially dereference garbage.
948  void RemoveObserver(Observer* observer);
949
950  // Returns a pointer to the JsBackend (which is owned by the sync
951  // manager).  Never returns NULL.  The following events are sent by
952  // the returned backend:
953  //
954  // onSyncNotificationStateChange(boolean notificationsEnabled):
955  //   Sent when notifications are enabled or disabled.
956  //
957  // onSyncIncomingNotification(array changedTypes):
958  //   Sent when an incoming notification arrives.  |changedTypes|
959  //   contains a list of sync types (strings) which have changed.
960  //
961  // The following messages are processed by the returned backend:
962  //
963  // getNotificationState():
964  //   If there is a parent router, sends the
965  //   onGetNotificationStateFinished(boolean notificationsEnabled)
966  //   event to |sender| via the parent router with whether or not
967  //   notifications are enabled.
968  //
969  // getRootNode():
970  //   If there is a parent router, sends the
971  //   onGetRootNodeFinished(dictionary nodeInfo) event to |sender|
972  //   via the parent router with information on the root node.
973  //
974  // getNodeById(string id):
975  //   If there is a parent router, sends the
976  //   onGetNodeByIdFinished(dictionary nodeInfo) event to |sender|
977  //   via the parent router with information on the node with the
978  //   given id (metahandle), if the id is valid and a node with that
979  //   id exists.  Otherwise, calls onGetNodeByIdFinished(null).
980  //
981  // All other messages are dropped.
982  browser_sync::JsBackend* GetJsBackend();
983
984  // Status-related getters. Typically GetStatusSummary will suffice, but
985  // GetDetailedSyncStatus can be useful for gathering debug-level details of
986  // the internals of the sync engine.
987  Status::Summary GetStatusSummary() const;
988  Status GetDetailedStatus() const;
989
990  // Whether or not the Nigori node is encrypted using an explicit passphrase.
991  bool IsUsingExplicitPassphrase();
992
993  // Get the internal implementation for use by BaseTransaction, etc.
994  SyncInternal* GetImpl() const;
995
996  // Call periodically from a database-safe thread to persist recent changes
997  // to the syncapi model.
998  void SaveChanges();
999
1000  // Issue a final SaveChanges, close sqlite handles, and stop running threads.
1001  // Must be called from the same thread that called Init().
1002  void Shutdown();
1003
1004  UserShare* GetUserShare() const;
1005
1006  // Uses a read-only transaction to determine if the directory being synced has
1007  // any remaining unsynced items.
1008  bool HasUnsyncedItems() const;
1009
1010  // Functions used for testing.
1011
1012  void TriggerOnNotificationStateChangeForTest(
1013      bool notifications_enabled);
1014
1015  void TriggerOnIncomingNotificationForTest(
1016      const syncable::ModelTypeBitSet& model_types);
1017
1018 private:
1019  // An opaque pointer to the nested private class.
1020  SyncInternal* data_;
1021
1022  DISALLOW_COPY_AND_ASSIGN(SyncManager);
1023};
1024
1025// An interface the embedding application (e.g. Chromium) implements to
1026// provide required HTTP POST functionality to the syncer backend.
1027// This interface is designed for one-time use. You create one, use it, and
1028// create another if you want to make a subsequent POST.
1029// TODO(timsteele): Bug 1482576. Consider splitting syncapi.h into two files:
1030// one for the API defining the exports, which doesn't need to be included from
1031// anywhere internally, and another file for the interfaces like this one.
1032class HttpPostProviderInterface {
1033 public:
1034  HttpPostProviderInterface() { }
1035  virtual ~HttpPostProviderInterface() { }
1036
1037  // Use specified user agent string when POSTing. If not called a default UA
1038  // may be used.
1039  virtual void SetUserAgent(const char* user_agent) = 0;
1040
1041  // Add additional headers to the request.
1042  virtual void SetExtraRequestHeaders(const char * headers) = 0;
1043
1044  // Set the URL to POST to.
1045  virtual void SetURL(const char* url, int port) = 0;
1046
1047  // Set the type, length and content of the POST payload.
1048  // |content_type| is a null-terminated MIME type specifier.
1049  // |content| is a data buffer; Do not interpret as a null-terminated string.
1050  // |content_length| is the total number of chars in |content|. It is used to
1051  // assign/copy |content| data.
1052  virtual void SetPostPayload(const char* content_type, int content_length,
1053                              const char* content) = 0;
1054
1055  // Returns true if the URL request succeeded. If the request failed,
1056  // os_error() may be non-zero and hence contain more information.
1057  virtual bool MakeSynchronousPost(int* os_error_code, int* response_code) = 0;
1058
1059  // Get the length of the content returned in the HTTP response.
1060  // This does not count the trailing null-terminating character returned
1061  // by GetResponseContent, so it is analogous to calling string.length.
1062  virtual int GetResponseContentLength() const = 0;
1063
1064  // Get the content returned in the HTTP response.
1065  // This is a null terminated string of characters.
1066  // Value should be copied.
1067  virtual const char* GetResponseContent() const = 0;
1068
1069  // Get the value of a header returned in the HTTP response.
1070  // If the header is not present, returns the empty string.
1071  virtual const std::string GetResponseHeaderValue(
1072      const std::string& name) const = 0;
1073
1074 private:
1075  DISALLOW_COPY_AND_ASSIGN(HttpPostProviderInterface);
1076};
1077
1078// A factory to create HttpPostProviders to hide details about the
1079// implementations and dependencies.
1080// A factory instance itself should be owned by whomever uses it to create
1081// HttpPostProviders.
1082class HttpPostProviderFactory {
1083 public:
1084  // Obtain a new HttpPostProviderInterface instance, owned by caller.
1085  virtual HttpPostProviderInterface* Create() = 0;
1086
1087  // When the interface is no longer needed (ready to be cleaned up), clients
1088  // must call Destroy().
1089  // This allows actual HttpPostProvider subclass implementations to be
1090  // reference counted, which is useful if a particular implementation uses
1091  // multiple threads to serve network requests.
1092  virtual void Destroy(HttpPostProviderInterface* http) = 0;
1093  virtual ~HttpPostProviderFactory() { }
1094};
1095
1096}  // namespace sync_api
1097
1098#endif  // CHROME_BROWSER_SYNC_ENGINE_SYNCAPI_H_
1099