| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 | |
| 41 #include "base/basictypes.h" | |
| 42 | |
| 43 #if (defined(OS_WIN) || defined(OS_WINDOWS)) | |
| 44 typedef wchar_t sync_char16; | |
| 45 #else | |
| 46 typedef uint16 sync_char16; | |
| 47 #endif | |
| 48 | |
| 49 // The MSVC compiler for Windows requires that any classes exported by, or | |
| 50 // imported from, a dynamic library be decorated with the following fanciness. | |
| 51 #if (defined(OS_WIN) || defined(OS_WINDOWS)) | |
| 52 #if COMPILING_SYNCAPI_LIBRARY | |
| 53 #define SYNC_EXPORT __declspec(dllexport) | |
| 54 #else | |
| 55 #define SYNC_EXPORT __declspec(dllimport) | |
| 56 #endif | |
| 57 #else | |
| 58 #define SYNC_EXPORT | |
| 59 #endif // OS_WIN || OS_WINDOWS | |
| 60 | |
| 61 // Forward declarations of internal class types so that sync API objects | |
| 62 // may have opaque pointers to these types. | |
| 63 namespace syncable { | |
| 64 class BaseTransaction; | |
| 65 class DirectoryManager; | |
| 66 class Entry; | |
| 67 class MutableEntry; | |
| 68 class ReadTransaction; | |
| 69 class ScopedDirLookup; | |
| 70 class WriteTransaction; | |
| 71 } | |
| 72 | |
| 73 namespace sync_api { | |
| 74 | |
| 75 // Forward declarations of classes to be defined later in this file. | |
| 76 class BaseTransaction; | |
| 77 class HttpPostProviderFactory; | |
| 78 class ModelSafeWorkerInterface; | |
| 79 class SyncManager; | |
| 80 class WriteTransaction; | |
| 81 struct UserShare; | |
| 82 | |
| 83 // A valid BaseNode will never have an ID of zero. | |
| 84 static const int64 kInvalidId = 0; | |
| 85 | |
| 86 // BaseNode wraps syncable::Entry, and corresponds to a single object's state. | |
| 87 // This, like syncable::Entry, is intended for use on the stack. A valid | |
| 88 // transaction is necessary to create a BaseNode or any of its children. | |
| 89 // Unlike syncable::Entry, a sync API BaseNode is identified primarily by its | |
| 90 // int64 metahandle, which we call an ID here. | |
| 91 class SYNC_EXPORT BaseNode { | |
| 92 public: | |
| 93 // All subclasses of BaseNode must provide a way to initialize themselves by | |
| 94 // doing an ID lookup. Returns false on failure. An invalid or deleted | |
| 95 // ID will result in failure. | |
| 96 virtual bool InitByIdLookup(int64 id) = 0; | |
| 97 | |
| 98 // Each object is identified by a 64-bit id (internally, the syncable | |
| 99 // metahandle). These ids are strictly local handles. They will persist | |
| 100 // on this client, but the same object on a different client may have a | |
| 101 // different ID value. | |
| 102 int64 GetId() const; | |
| 103 | |
| 104 // Nodes are hierarchically arranged into a single-rooted tree. | |
| 105 // InitByRootLookup on ReadNode allows access to the root. GetParentId is | |
| 106 // how you find a node's parent. | |
| 107 int64 GetParentId() const; | |
| 108 | |
| 109 // Nodes are either folders or not. This corresponds to the IS_DIR property | |
| 110 // of syncable::Entry. | |
| 111 bool GetIsFolder() const; | |
| 112 | |
| 113 // Returns the title of the object as a C string. The memory is owned by | |
| 114 // BaseNode and becomes invalid if GetTitle() is called a second time on this | |
| 115 // node, or when the node is destroyed. A caller should convert this | |
| 116 // immediately into e.g. a std::string. Uniqueness of the title is not | |
| 117 // enforced on siblings -- it is not an error for two children to share | |
| 118 // a title. | |
| 119 const sync_char16* GetTitle() const; | |
| 120 | |
| 121 // Returns the URL of a bookmark object as a C string. The memory is owned | |
| 122 // by BaseNode and becomes invalid if GetURL() is called a second time on | |
| 123 // this node, or when the node is destroyed. A caller should convert this | |
| 124 // immediately into e.g. a std::string. | |
| 125 const sync_char16* GetURL() const; | |
| 126 | |
| 127 // Return a pointer to the byte data of the favicon image for this node. | |
| 128 // Will return NULL if there is no favicon data associated with this node. | |
| 129 // The length of the array is returned to the caller via |size_in_bytes|. | |
| 130 // Favicons are expected to be PNG images, and though no verification is | |
| 131 // done on the syncapi client of this, the server may reject favicon updates | |
| 132 // that are invalid for whatever reason. | |
| 133 const unsigned char* GetFaviconBytes(size_t* size_in_bytes); | |
| 134 | |
| 135 // Returns the local external ID associated with the node. | |
| 136 int64 GetExternalId() const; | |
| 137 | |
| 138 // Return the ID of the node immediately before this in the sibling order. | |
| 139 // For the first node in the ordering, return 0. | |
| 140 int64 GetPredecessorId() const; | |
| 141 | |
| 142 // Return the ID of the node immediately after this in the sibling order. | |
| 143 // For the last node in the ordering, return 0. | |
| 144 int64 GetSuccessorId() const; | |
| 145 | |
| 146 // Return the ID of the first child of this node. If this node has no | |
| 147 // children, return 0. | |
| 148 int64 GetFirstChildId() const; | |
| 149 | |
| 150 // Get an array containing the IDs of this node's children. The memory is | |
| 151 // owned by BaseNode and becomes invalid if GetChildIds() is called a second | |
| 152 // time on this node, or when the node is destroyed. Return the array size | |
| 153 // in the child_count parameter. | |
| 154 const int64* GetChildIds(size_t* child_count) const; | |
| 155 | |
| 156 // These virtual accessors provide access to data members of derived classes. | |
| 157 virtual const syncable::Entry* GetEntry() const = 0; | |
| 158 virtual const BaseTransaction* GetTransaction() const = 0; | |
| 159 | |
| 160 protected: | |
| 161 BaseNode(); | |
| 162 virtual ~BaseNode(); | |
| 163 | |
| 164 private: | |
| 165 struct BaseNodeInternal; | |
| 166 | |
| 167 // Node is meant for stack use only. | |
| 168 void* operator new(size_t size); | |
| 169 | |
| 170 // Provides storage for member functions that return pointers to class | |
| 171 // memory, e.g. C strings returned by GetTitle(). | |
| 172 BaseNodeInternal* data_; | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(BaseNode); | |
| 175 }; | |
| 176 | |
| 177 // WriteNode extends BaseNode to add mutation, and wraps | |
| 178 // syncable::MutableEntry. A WriteTransaction is needed to create a WriteNode. | |
| 179 class SYNC_EXPORT WriteNode : public BaseNode { | |
| 180 public: | |
| 181 // Create a WriteNode using the given transaction. | |
| 182 explicit WriteNode(WriteTransaction* transaction); | |
| 183 virtual ~WriteNode(); | |
| 184 | |
| 185 // A client must use one (and only one) of the following Init variants to | |
| 186 // populate the node. | |
| 187 | |
| 188 // BaseNode implementation. | |
| 189 virtual bool InitByIdLookup(int64 id); | |
| 190 | |
| 191 // Create a new node with the specified parent and predecessor. Use a NULL | |
| 192 // |predecessor| to indicate that this is to be the first child. | |
| 193 // |predecessor| must be a child of |new_parent| or NULL. Returns false on | |
| 194 // failure. | |
| 195 bool InitByCreation(const BaseNode& parent, const BaseNode* predecessor); | |
| 196 | |
| 197 // These Set() functions correspond to the Get() functions of BaseNode. | |
| 198 void SetIsFolder(bool folder); | |
| 199 void SetTitle(const sync_char16* title); | |
| 200 void SetURL(const sync_char16* url); | |
| 201 void SetFaviconBytes(const unsigned char* bytes, size_t size_in_bytes); | |
| 202 // External ID is a client-only field, so setting it doesn't cause the item to | |
| 203 // be synced again. | |
| 204 void SetExternalId(int64 external_id); | |
| 205 | |
| 206 // Remove this node and its children. | |
| 207 void Remove(); | |
| 208 | |
| 209 // Set a new parent and position. Position is specified by |predecessor|; if | |
| 210 // it is NULL, the node is moved to the first position. |predecessor| must | |
| 211 // be a child of |new_parent| or NULL. Returns false on failure.. | |
| 212 bool SetPosition(const BaseNode& new_parent, const BaseNode* predecessor); | |
| 213 | |
| 214 // Implementation of BaseNode's abstract virtual accessors. | |
| 215 virtual const syncable::Entry* GetEntry() const; | |
| 216 | |
| 217 virtual const BaseTransaction* GetTransaction() const; | |
| 218 | |
| 219 private: | |
| 220 void* operator new(size_t size); // Node is meant for stack use only. | |
| 221 | |
| 222 // Helper to set the previous node. | |
| 223 void PutPredecessor(const BaseNode* predecessor); | |
| 224 | |
| 225 // Sets IS_UNSYNCED and SYNCING to ensure this entry is considered in an | |
| 226 // upcoming commit pass. | |
| 227 void MarkForSyncing(); | |
| 228 | |
| 229 // The underlying syncable object which this class wraps. | |
| 230 syncable::MutableEntry* entry_; | |
| 231 | |
| 232 // The sync API transaction that is the parent of this node. | |
| 233 WriteTransaction* transaction_; | |
| 234 | |
| 235 DISALLOW_COPY_AND_ASSIGN(WriteNode); | |
| 236 }; | |
| 237 | |
| 238 // ReadNode wraps a syncable::Entry to provide the functionality of a | |
| 239 // read-only BaseNode. | |
| 240 class SYNC_EXPORT ReadNode : public BaseNode { | |
| 241 public: | |
| 242 // Create an unpopulated ReadNode on the given transaction. Call some flavor | |
| 243 // of Init to populate the ReadNode with a database entry. | |
| 244 explicit ReadNode(const BaseTransaction* transaction); | |
| 245 virtual ~ReadNode(); | |
| 246 | |
| 247 // A client must use one (and only one) of the following Init variants to | |
| 248 // populate the node. | |
| 249 | |
| 250 // BaseNode implementation. | |
| 251 virtual bool InitByIdLookup(int64 id); | |
| 252 | |
| 253 // There is always a root node, so this can't fail. The root node is | |
| 254 // never mutable, so root lookup is only possible on a ReadNode. | |
| 255 void InitByRootLookup(); | |
| 256 | |
| 257 // Each server-created permanent node is tagged with a unique string. | |
| 258 // Look up the node with the particular tag. If it does not exist, | |
| 259 // return false. Since these nodes are special, lookup is only | |
| 260 // provided only through ReadNode. | |
| 261 bool InitByTagLookup(const sync_char16* tag); | |
| 262 | |
| 263 // Implementation of BaseNode's abstract virtual accessors. | |
| 264 virtual const syncable::Entry* GetEntry() const; | |
| 265 virtual const BaseTransaction* GetTransaction() const; | |
| 266 | |
| 267 private: | |
| 268 void* operator new(size_t size); // Node is meant for stack use only. | |
| 269 | |
| 270 // The underlying syncable object which this class wraps. | |
| 271 syncable::Entry* entry_; | |
| 272 | |
| 273 // The sync API transaction that is the parent of this node. | |
| 274 const BaseTransaction* transaction_; | |
| 275 | |
| 276 DISALLOW_COPY_AND_ASSIGN(ReadNode); | |
| 277 }; | |
| 278 | |
| 279 // Sync API's BaseTransaction, ReadTransaction, and WriteTransaction allow for | |
| 280 // batching of several read and/or write operations. The read and write | |
| 281 // operations are performed by creating ReadNode and WriteNode instances using | |
| 282 // the transaction. These transaction classes wrap identically named classes in | |
| 283 // syncable, and are used in a similar way. Unlike syncable::BaseTransaction, | |
| 284 // whose construction requires an explicit syncable::ScopedDirLookup, a sync | |
| 285 // API BaseTransaction creates its own ScopedDirLookup implicitly. | |
| 286 class SYNC_EXPORT BaseTransaction { | |
| 287 public: | |
| 288 // Provide access to the underlying syncable.h objects from BaseNode. | |
| 289 virtual syncable::BaseTransaction* GetWrappedTrans() const = 0; | |
| 290 const syncable::ScopedDirLookup& GetLookup() const { return *lookup_; } | |
| 291 | |
| 292 protected: | |
| 293 // The ScopedDirLookup is created in the constructor and destroyed | |
| 294 // in the destructor. Creation of the ScopedDirLookup is not expected | |
| 295 // to fail. | |
| 296 explicit BaseTransaction(UserShare* share); | |
| 297 virtual ~BaseTransaction(); | |
| 298 | |
| 299 private: | |
| 300 // A syncable ScopedDirLookup, which is the parent of syncable transactions. | |
| 301 syncable::ScopedDirLookup* lookup_; | |
| 302 | |
| 303 DISALLOW_COPY_AND_ASSIGN(BaseTransaction); | |
| 304 }; | |
| 305 | |
| 306 // Sync API's ReadTransaction is a read-only BaseTransaction. It wraps | |
| 307 // a syncable::ReadTransaction. | |
| 308 class SYNC_EXPORT ReadTransaction : public BaseTransaction { | |
| 309 public: | |
| 310 // Start a new read-only transaction on the specified repository. | |
| 311 explicit ReadTransaction(UserShare* share); | |
| 312 virtual ~ReadTransaction(); | |
| 313 | |
| 314 // BaseTransaction override. | |
| 315 virtual syncable::BaseTransaction* GetWrappedTrans() const; | |
| 316 private: | |
| 317 void* operator new(size_t size); // Transaction is meant for stack use only. | |
| 318 | |
| 319 // The underlying syncable object which this class wraps. | |
| 320 syncable::ReadTransaction* transaction_; | |
| 321 | |
| 322 DISALLOW_COPY_AND_ASSIGN(ReadTransaction); | |
| 323 }; | |
| 324 | |
| 325 // Sync API's WriteTransaction is a read/write BaseTransaction. It wraps | |
| 326 // a syncable::WriteTransaction. | |
| 327 class SYNC_EXPORT WriteTransaction : public BaseTransaction { | |
| 328 public: | |
| 329 // Start a new read/write transaction. | |
| 330 explicit WriteTransaction(UserShare* share); | |
| 331 virtual ~WriteTransaction(); | |
| 332 | |
| 333 // Provide access to the syncable.h transaction from the API WriteNode. | |
| 334 virtual syncable::BaseTransaction* GetWrappedTrans() const; | |
| 335 syncable::WriteTransaction* GetWrappedWriteTrans() { return transaction_; } | |
| 336 | |
| 337 private: | |
| 338 void* operator new(size_t size); // Transaction is meant for stack use only. | |
| 339 | |
| 340 // The underlying syncable object which this class wraps. | |
| 341 syncable::WriteTransaction* transaction_; | |
| 342 | |
| 343 DISALLOW_COPY_AND_ASSIGN(WriteTransaction); | |
| 344 }; | |
| 345 | |
| 346 // SyncManager encapsulates syncable::DirectoryManager and serves as the parent | |
| 347 // of all other objects in the sync API. SyncManager is thread-safe. If | |
| 348 // multiple threads interact with the same local sync repository (i.e. the | |
| 349 // same sqlite database), they should share a single SyncManager instance. The | |
| 350 // caller should typically create one SyncManager for the lifetime of a user | |
| 351 // session. | |
| 352 class SYNC_EXPORT SyncManager { | |
| 353 public: | |
| 354 // SyncInternal contains the implementation of SyncManager, while abstracting | |
| 355 // internal types from clients of the interface. | |
| 356 class SyncInternal; | |
| 357 | |
| 358 // ChangeRecord indicates a single item that changed as a result of a sync | |
| 359 // operation. This gives the sync id of the node that changed, and the type | |
| 360 // of change. To get the actual property values after an ADD or UPDATE, the | |
| 361 // client should get the node with InitByIdLookup(), using the provided id. | |
| 362 struct ChangeRecord { | |
| 363 enum Action { | |
| 364 ACTION_ADD, | |
| 365 ACTION_DELETE, | |
| 366 ACTION_UPDATE, | |
| 367 }; | |
| 368 ChangeRecord() : id(kInvalidId), action(ACTION_ADD) {} | |
| 369 int64 id; | |
| 370 Action action; | |
| 371 }; | |
| 372 | |
| 373 // When the SyncManager is unable to initiate the syncing process due to a | |
| 374 // failure during authentication, AuthProblem describes the actual problem | |
| 375 // more precisely. | |
| 376 enum AuthProblem { | |
| 377 AUTH_PROBLEM_NONE = 0, | |
| 378 // The credentials supplied to GAIA were either invalid, or the locally | |
| 379 // cached credentials have expired. If this happens, the sync system | |
| 380 // will continue as if offline until authentication is reattempted. | |
| 381 AUTH_PROBLEM_INVALID_GAIA_CREDENTIALS, | |
| 382 // The GAIA user is not authorized to use the sync service. | |
| 383 AUTH_PROBLEM_USER_NOT_SIGNED_UP, | |
| 384 // Could not connect to server to verify credentials. This could be in | |
| 385 // response to either failure to connect to GAIA or failure to connect to | |
| 386 // the sync service during authentication. | |
| 387 AUTH_PROBLEM_CONNECTION_FAILED, | |
| 388 }; | |
| 389 | |
| 390 // Status encapsulates detailed state about the internals of the SyncManager. | |
| 391 struct Status { | |
| 392 // Summary is a distilled set of important information that the end-user may | |
| 393 // wish to be informed about (through UI, for example). Note that if a | |
| 394 // summary state requires user interaction (such as auth failures), more | |
| 395 // detailed information may be contained in additional status fields. | |
| 396 enum Summary { | |
| 397 // The internal instance is in an unrecognizable state. This should not | |
| 398 // happen. | |
| 399 INVALID = 0, | |
| 400 // Can't connect to server, but there are no pending changes in | |
| 401 // our local cache. | |
| 402 OFFLINE, | |
| 403 // Can't connect to server, and there are pending changes in our | |
| 404 // local cache. | |
| 405 OFFLINE_UNSYNCED, | |
| 406 // Connected and syncing. | |
| 407 SYNCING, | |
| 408 // Connected, no pending changes. | |
| 409 READY, | |
| 410 // User has chosen to pause syncing. | |
| 411 PAUSED, | |
| 412 // Internal sync error. | |
| 413 CONFLICT, | |
| 414 // Can't connect to server, and we haven't completed the initial | |
| 415 // sync yet. So there's nothing we can do but wait for the server. | |
| 416 OFFLINE_UNUSABLE, | |
| 417 }; | |
| 418 Summary summary; | |
| 419 | |
| 420 // Various server related information. | |
| 421 bool authenticated; // Successfully authenticated via GAIA. | |
| 422 bool server_up; // True if we have received at least one good | |
| 423 // reply from the server. | |
| 424 bool server_reachable; // True if we received any reply from the server. | |
| 425 bool server_broken; // True of the syncer is stopped because of server | |
| 426 // issues. | |
| 427 | |
| 428 bool notifications_enabled; // True only if subscribed for notifications. | |
| 429 int notifications_received; | |
| 430 int notifications_sent; | |
| 431 | |
| 432 // Various Syncer data. | |
| 433 int unsynced_count; | |
| 434 int conflicting_count; | |
| 435 bool syncing; | |
| 436 bool syncer_paused; | |
| 437 bool initial_sync_ended; | |
| 438 bool syncer_stuck; | |
| 439 int64 updates_available; | |
| 440 int64 updates_received; | |
| 441 bool disk_full; | |
| 442 bool invalid_store; | |
| 443 int max_consecutive_errors; // The max number of errors from any component. | |
| 444 }; | |
| 445 | |
| 446 // An interface the embedding application implements to receive notifications | |
| 447 // from the SyncManager. Register an observer via SyncManager::AddObserver. | |
| 448 // This observer is an event driven model as the events may be raised from | |
| 449 // different internal threads, and simply providing an "OnStatusChanged" type | |
| 450 // notification complicates things such as trying to determine "what changed", | |
| 451 // if different members of the Status object are modified from different | |
| 452 // threads. This way, the event is explicit, and it is safe for the Observer | |
| 453 // to dispatch to a native thread or synchronize accordingly. | |
| 454 class Observer { | |
| 455 public: | |
| 456 Observer() { } | |
| 457 virtual ~Observer() { } | |
| 458 // Notify the observer that changes have been applied to the sync model. | |
| 459 // This will be invoked on the same thread as on which ApplyChanges was | |
| 460 // called. |changes| is an array of size |change_count|, and contains the ID | |
| 461 // of each individual item that was changed. |changes| exists only | |
| 462 // for the duration of the call. Because the observer is passed a |trans|, | |
| 463 // the observer can assume a read lock on the database that will be released | |
| 464 // after the function returns. | |
| 465 // | |
| 466 // The SyncManager constructs |changes| in the following guaranteed order: | |
| 467 // | |
| 468 // 1. Deletions, from leaves up to parents. | |
| 469 // 2. Updates to existing items with synced parents & predecessors. | |
| 470 // 3. New items with synced parents & predecessors. | |
| 471 // 4. Items with parents & predecessors in |changes|. | |
| 472 // 5. Repeat #4 until all items are in |changes|. | |
| 473 // | |
| 474 // Thus, an implementation of OnChangesApplied should be able to | |
| 475 // process the change records in the order without having to worry about | |
| 476 // forward dependencies. But since deletions come before reparent | |
| 477 // operations, a delete may temporarily orphan a node that is | |
| 478 // updated later in the list. | |
| 479 virtual void OnChangesApplied(const BaseTransaction* trans, | |
| 480 const ChangeRecord* changes, | |
| 481 int change_count) = 0; | |
| 482 | |
| 483 // A round-trip sync-cycle took place and the syncer has resolved any | |
| 484 // conflicts that may have arisen. This is kept separate from | |
| 485 // OnStatusChanged as there isn't really any state update; it is plainly | |
| 486 // a notification of a state transition. | |
| 487 virtual void OnSyncCycleCompleted() = 0; | |
| 488 | |
| 489 // Called when user interaction may be required due to an auth problem. | |
| 490 virtual void OnAuthProblem(AuthProblem auth_problem) = 0; | |
| 491 | |
| 492 // Called when initialization is complete to the point that SyncManager can | |
| 493 // process changes. This does not necessarily mean authentication succeeded | |
| 494 // or that the SyncManager is online. | |
| 495 // IMPORTANT: Creating any type of transaction before receiving this | |
| 496 // notification is illegal! | |
| 497 // WARNING: Calling methods on the SyncManager before receiving this | |
| 498 // message, unless otherwise specified, produces undefined behavior. | |
| 499 virtual void OnInitializationComplete() = 0; | |
| 500 | |
| 501 private: | |
| 502 DISALLOW_COPY_AND_ASSIGN(Observer); | |
| 503 }; | |
| 504 | |
| 505 // Create an uninitialized SyncManager. Callers must Init() before using. | |
| 506 SyncManager(); | |
| 507 virtual ~SyncManager(); | |
| 508 | |
| 509 // Initialize the sync manager. |database_location| specifies the path of | |
| 510 // the directory in which to locate a sqlite repository storing the syncer | |
| 511 // backend state. Initialization will open the database, or create it if it | |
| 512 // does not already exist. Returns false on failure. | |
| 513 // |sync_server_and_path| and |sync_server_port| represent the Chrome sync | |
| 514 // server to use, and |use_ssl| specifies whether to communicate securely; | |
| 515 // the default is false. | |
| 516 // |gaia_service_id| is the service id used for GAIA authentication. If it's | |
| 517 // null then default will be used. | |
| 518 // |post_factory| will be owned internally and used to create | |
| 519 // instances of an HttpPostProvider. | |
| 520 // |auth_post_factory| will be owned internally and used to create | |
| 521 // instances of an HttpPostProvider for communicating with GAIA. | |
| 522 // TODO(timsteele): It seems like one factory should suffice, but for now to | |
| 523 // avoid having to deal with threading issues since the auth code and syncer | |
| 524 // code live on separate threads that run simultaneously, we just dedicate | |
| 525 // one to each component. Long term we may want to reconsider the HttpBridge | |
| 526 // API to take all the params in one chunk in a threadsafe manner.. which is | |
| 527 // still suboptimal as there will be high contention between the two threads | |
| 528 // on startup; so maybe what we have now is the best solution- it does mirror | |
| 529 // the CURL implementation as each thread creates their own internet handle. | |
| 530 // Investigate. | |
| 531 // |model_safe_worker| ownership is given to the SyncManager. | |
| 532 // |user_agent| is a 7-bit ASCII string suitable for use as the User-Agent | |
| 533 // HTTP header. Used internally when collecting stats to classify clients. | |
| 534 bool Init(const sync_char16* database_location, | |
| 535 const char* sync_server_and_path, | |
| 536 int sync_server_port, | |
| 537 const char* gaia_service_id, | |
| 538 const char* gaia_source, | |
| 539 bool use_ssl, | |
| 540 HttpPostProviderFactory* post_factory, | |
| 541 HttpPostProviderFactory* auth_post_factory, | |
| 542 ModelSafeWorkerInterface* model_safe_worker, | |
| 543 bool attempt_last_user_authentication, | |
| 544 const char* user_agent); | |
| 545 | |
| 546 // Returns the username last used for a successful authentication as a | |
| 547 // null-terminated string. Returns empty if there is no such username. | |
| 548 // The memory is not owned by the caller and should be copied. | |
| 549 const char* GetAuthenticatedUsername(); | |
| 550 | |
| 551 // Submit credentials to GAIA for verification and start the | |
| 552 // syncing process on success. On success, both |username| and the obtained | |
| 553 // auth token are persisted on disk for future re-use. | |
| 554 // If authentication fails, OnAuthProblem is called on our Observer. | |
| 555 // The Observer may, in turn, decide to try again with new | |
| 556 // credentials. Calling this method again is the appropriate course of action | |
| 557 // to "retry". | |
| 558 // |username| and |password| are expected to be owned by the caller. | |
| 559 void Authenticate(const char* username, const char* password); | |
| 560 | |
| 561 // Adds a listener to be notified of sync events. | |
| 562 // NOTE: It is OK (in fact, it's probably a good idea) to call this before | |
| 563 // having received OnInitializationCompleted. | |
| 564 void SetObserver(Observer* observer); | |
| 565 | |
| 566 // Remove the observer set by SetObserver (no op if none was set). | |
| 567 // Make sure to call this if the Observer set in SetObserver is being | |
| 568 // destroyed so the SyncManager doesn't potentially dereference garbage. | |
| 569 void RemoveObserver(); | |
| 570 | |
| 571 // Status-related getters. Typically GetStatusSummary will suffice, but | |
| 572 // GetDetailedSyncStatus can be useful for gathering debug-level details of | |
| 573 // the internals of the sync engine. | |
| 574 Status::Summary GetStatusSummary() const; | |
| 575 Status GetDetailedStatus() const; | |
| 576 | |
| 577 // Get the internal implementation for use by BaseTransaction, etc. | |
| 578 SyncInternal* GetImpl() const; | |
| 579 | |
| 580 // Call periodically from a database-safe thread to persist recent changes | |
| 581 // to the syncapi model. | |
| 582 void SaveChanges(); | |
| 583 | |
| 584 // Invoking this method will result in the syncapi bypassing authentication | |
| 585 // and opening a local store suitable for testing client code. When in this | |
| 586 // mode, nothing will ever get synced to a server (in fact no HTTP | |
| 587 // communication will take place). | |
| 588 // Note: The SyncManager precondition that you must first call Init holds; | |
| 589 // this will fail unless we're initialized. | |
| 590 void SetupForTestMode(const sync_char16* test_username); | |
| 591 | |
| 592 // Issue a final SaveChanges, close sqlite handles, and stop running threads. | |
| 593 // Must be called from the same thread that called Init(). | |
| 594 void Shutdown(); | |
| 595 | |
| 596 UserShare* GetUserShare() const; | |
| 597 | |
| 598 private: | |
| 599 // An opaque pointer to the nested private class. | |
| 600 SyncInternal* data_; | |
| 601 | |
| 602 DISALLOW_COPY_AND_ASSIGN(SyncManager); | |
| 603 }; | |
| 604 | |
| 605 // An interface the embedding application (e.g. Chromium) implements to | |
| 606 // provide required HTTP POST functionality to the syncer backend. | |
| 607 // This interface is designed for one-time use. You create one, use it, and | |
| 608 // create another if you want to make a subsequent POST. | |
| 609 // TODO(timsteele): Bug 1482576. Consider splitting syncapi.h into two files: | |
| 610 // one for the API defining the exports, which doesn't need to be included from | |
| 611 // anywhere internally, and another file for the interfaces like this one. | |
| 612 class HttpPostProviderInterface { | |
| 613 public: | |
| 614 HttpPostProviderInterface() { } | |
| 615 virtual ~HttpPostProviderInterface() { } | |
| 616 | |
| 617 // Use specified user agent string when POSTing. If not called a default UA | |
| 618 // may be used. | |
| 619 virtual void SetUserAgent(const char* user_agent) = 0; | |
| 620 | |
| 621 // Set the URL to POST to. | |
| 622 virtual void SetURL(const char* url, int port) = 0; | |
| 623 | |
| 624 // Set the type, length and content of the POST payload. | |
| 625 // |content_type| is a null-terminated MIME type specifier. | |
| 626 // |content| is a data buffer; Do not interpret as a null-terminated string. | |
| 627 // |content_length| is the total number of chars in |content|. It is used to | |
| 628 // assign/copy |content| data. | |
| 629 virtual void SetPostPayload(const char* content_type, int content_length, | |
| 630 const char* content) = 0; | |
| 631 | |
| 632 // Add the specified cookie to the request context using the url set by | |
| 633 // SetURL as the key. |cookie| should be a standard cookie line | |
| 634 // [e.g "name=val; name2=val2"]. |cookie| should be copied. | |
| 635 virtual void AddCookieForRequest(const char* cookie) = 0; | |
| 636 | |
| 637 // Returns true if the URL request succeeded. If the request failed, | |
| 638 // os_error() may be non-zero and hence contain more information. | |
| 639 virtual bool MakeSynchronousPost(int* os_error_code, int* response_code) = 0; | |
| 640 | |
| 641 // Get the length of the content returned in the HTTP response. | |
| 642 // This does not count the trailing null-terminating character returned | |
| 643 // by GetResponseContent, so it is analogous to calling string.length. | |
| 644 virtual int GetResponseContentLength() const = 0; | |
| 645 | |
| 646 // Get the content returned in the HTTP response. | |
| 647 // This is a null terminated string of characters. | |
| 648 // Value should be copied. | |
| 649 virtual const char* GetResponseContent() const = 0; | |
| 650 | |
| 651 // To simplify passing a vector<string> across this API, we provide the | |
| 652 // following two methods. Use GetResponseCookieCount to bound a loop calling | |
| 653 // GetResponseCookieAt once for each integer in the range | |
| 654 // [0, GetNumCookiesInResponse). The char* returned should be copied. | |
| 655 virtual int GetResponseCookieCount() const = 0; | |
| 656 virtual const char* GetResponseCookieAt(int cookie_number) const = 0; | |
| 657 | |
| 658 private: | |
| 659 DISALLOW_COPY_AND_ASSIGN(HttpPostProviderInterface); | |
| 660 }; | |
| 661 | |
| 662 // A factory to create HttpPostProviders to hide details about the | |
| 663 // implementations and dependencies. | |
| 664 // A factory instance itself should be owned by whomever uses it to create | |
| 665 // HttpPostProviders. | |
| 666 class HttpPostProviderFactory { | |
| 667 public: | |
| 668 // Obtain a new HttpPostProviderInterface instance, owned by caller. | |
| 669 virtual HttpPostProviderInterface* Create() = 0; | |
| 670 | |
| 671 // When the interface is no longer needed (ready to be cleaned up), clients | |
| 672 // must call Destroy(). | |
| 673 // This allows actual HttpPostProvider subclass implementations to be | |
| 674 // reference counted, which is useful if a particular implementation uses | |
| 675 // multiple threads to serve network requests. | |
| 676 virtual void Destroy(HttpPostProviderInterface* http) = 0; | |
| 677 virtual ~HttpPostProviderFactory() { } | |
| 678 }; | |
| 679 | |
| 680 // A class syncapi clients should use whenever the underlying model is bound to | |
| 681 // a particular thread in the embedding application. This exposes an interface | |
| 682 // by which any model-modifying invocations will be forwarded to the | |
| 683 // appropriate thread in the embedding application. | |
| 684 // "model safe" refers to not allowing an embedding application model to fall | |
| 685 // out of sync with the syncable::Directory due to race conditions. | |
| 686 class ModelSafeWorkerInterface { | |
| 687 public: | |
| 688 virtual ~ModelSafeWorkerInterface() { } | |
| 689 // A Visitor is passed to CallDoWorkFromModelSafeThreadAndWait invocations, | |
| 690 // and it's sole purpose is to provide a way for the ModelSafeWorkerInterface | |
| 691 // implementation to actually _do_ the work required, by calling the only | |
| 692 // method on this class, DoWork(). | |
| 693 class Visitor { | |
| 694 public: | |
| 695 virtual ~Visitor() { } | |
| 696 // When on a model safe thread, this should be called to have the syncapi | |
| 697 // actually perform the work needing to be done. | |
| 698 virtual void DoWork() = 0; | |
| 699 }; | |
| 700 // Subclasses should implement to invoke DoWork on |visitor| once on a thread | |
| 701 // appropriate for data model modifications. | |
| 702 // While it doesn't hurt, the impl does not need to be re-entrant (for now). | |
| 703 // Note: |visitor| is owned by caller. | |
| 704 virtual void CallDoWorkFromModelSafeThreadAndWait(Visitor* visitor) = 0; | |
| 705 }; | |
| 706 | |
| 707 } // namespace sync_api | |
| 708 | |
| 709 #endif // CHROME_BROWSER_SYNC_ENGINE_SYNCAPI_H_ | |
| OLD | NEW |