Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / sync / syncable / directory.h
1 // Copyright 2013 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 SYNC_SYNCABLE_DIRECTORY_H_
6 #define SYNC_SYNCABLE_DIRECTORY_H_
7
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/file_util.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/values.h"
18 #include "sync/base/sync_export.h"
19 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
20 #include "sync/internal_api/public/util/weak_handle.h"
21 #include "sync/syncable/dir_open_result.h"
22 #include "sync/syncable/entry.h"
23 #include "sync/syncable/entry_kernel.h"
24 #include "sync/syncable/metahandle_set.h"
25 #include "sync/syncable/parent_child_index.h"
26 #include "sync/syncable/syncable_delete_journal.h"
27
28 namespace syncer {
29
30 class Cryptographer;
31 class TestUserShare;
32 class UnrecoverableErrorHandler;
33
34 namespace syncable {
35
36 class BaseTransaction;
37 class BaseWriteTransaction;
38 class DirectoryChangeDelegate;
39 class DirectoryBackingStore;
40 class NigoriHandler;
41 class ScopedKernelLock;
42 class TransactionObserver;
43 class WriteTransaction;
44
45 enum InvariantCheckLevel {
46   OFF = 0,            // No checking.
47   VERIFY_CHANGES = 1, // Checks only mutated entries.  Does not check hierarchy.
48   FULL_DB_VERIFICATION = 2 // Check every entry.  This can be expensive.
49 };
50
51 // Directory stores and manages EntryKernels.
52 //
53 // This class is tightly coupled to several other classes (see friends).
54 class SYNC_EXPORT Directory {
55   friend class BaseTransaction;
56   friend class Entry;
57   friend class ModelNeutralMutableEntry;
58   friend class MutableEntry;
59   friend class ReadTransaction;
60   friend class ScopedKernelLock;
61   friend class WriteTransaction;
62   friend class SyncableDirectoryTest;
63   friend class syncer::TestUserShare;
64   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, ManageDeleteJournals);
65   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
66                            TakeSnapshotGetsAllDirtyHandlesTest);
67   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
68                            TakeSnapshotGetsOnlyDirtyHandlesTest);
69   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
70                            TakeSnapshotGetsMetahandlesToPurge);
71
72  public:
73   typedef std::vector<int64> Metahandles;
74
75   // Be careful when using these hash_map containers.  According to the spec,
76   // inserting into them may invalidate all iterators.
77   //
78   // It gets worse, though.  The Anroid STL library has a bug that means it may
79   // invalidate all iterators when you erase from the map, too.  That means that
80   // you can't iterate while erasing.  STLDeleteElements(), std::remove_if(),
81   // and other similar functions are off-limits too, until this bug is fixed.
82   //
83   // See http://sourceforge.net/p/stlport/bugs/239/.
84   typedef base::hash_map<int64, EntryKernel*> MetahandlesMap;
85   typedef base::hash_map<std::string, EntryKernel*> IdsMap;
86   typedef base::hash_map<std::string, EntryKernel*> TagsMap;
87   typedef std::string AttachmentIdUniqueId;
88   typedef base::hash_map<AttachmentIdUniqueId, MetahandleSet>
89       IndexByAttachmentId;
90
91   static const base::FilePath::CharType kSyncDatabaseFilename[];
92
93   // The dirty/clean state of kernel fields backed by the share_info table.
94   // This is public so it can be used in SaveChangesSnapshot for persistence.
95   enum KernelShareInfoStatus {
96     KERNEL_SHARE_INFO_INVALID,
97     KERNEL_SHARE_INFO_VALID,
98     KERNEL_SHARE_INFO_DIRTY
99   };
100
101   // Various data that the Directory::Kernel we are backing (persisting data
102   // for) needs saved across runs of the application.
103   struct SYNC_EXPORT_PRIVATE PersistedKernelInfo {
104     PersistedKernelInfo();
105     ~PersistedKernelInfo();
106
107     // Set the |download_progress| entry for the given model to a
108     // "first sync" start point.  When such a value is sent to the server,
109     // a full download of all objects of the model will be initiated.
110     void ResetDownloadProgress(ModelType model_type);
111
112     // Whether a valid progress marker exists for |model_type|.
113     bool HasEmptyDownloadProgress(ModelType model_type);
114
115     // Last sync timestamp fetched from the server.
116     sync_pb::DataTypeProgressMarker download_progress[MODEL_TYPE_COUNT];
117     // Sync-side transaction version per data type. Monotonically incremented
118     // when updating native model. A copy is also saved in native model.
119     // Later out-of-sync models can be detected and fixed by comparing
120     // transaction versions of sync model and native model.
121     // TODO(hatiaol): implement detection and fixing of out-of-sync models.
122     //                Bug 154858.
123     int64 transaction_version[MODEL_TYPE_COUNT];
124     // The store birthday we were given by the server. Contents are opaque to
125     // the client.
126     std::string store_birthday;
127     // The next local ID that has not been used with this cache-GUID.
128     int64 next_id;
129     // The serialized bag of chips we were given by the server. Contents are
130     // opaque to the client. This is the serialization of a message of type
131     // ChipBag defined in sync.proto. It can contains NULL characters.
132     std::string bag_of_chips;
133     // The per-datatype context.
134     sync_pb::DataTypeContext datatype_context[MODEL_TYPE_COUNT];
135   };
136
137   // What the Directory needs on initialization to create itself and its Kernel.
138   // Filled by DirectoryBackingStore::Load.
139   struct KernelLoadInfo {
140     PersistedKernelInfo kernel_info;
141     std::string cache_guid;  // Created on first initialization, never changes.
142     int64 max_metahandle;    // Computed (using sql MAX aggregate) on init.
143     KernelLoadInfo() : max_metahandle(0) {
144     }
145   };
146
147   // When the Directory is told to SaveChanges, a SaveChangesSnapshot is
148   // constructed and forms a consistent snapshot of what needs to be sent to
149   // the backing store.
150   struct SYNC_EXPORT_PRIVATE SaveChangesSnapshot {
151     SaveChangesSnapshot();
152     ~SaveChangesSnapshot();
153
154     KernelShareInfoStatus kernel_info_status;
155     PersistedKernelInfo kernel_info;
156     EntryKernelSet dirty_metas;
157     MetahandleSet metahandles_to_purge;
158     EntryKernelSet delete_journals;
159     MetahandleSet delete_journals_to_purge;
160   };
161
162   // Does not take ownership of |encryptor|.
163   // |report_unrecoverable_error_function| may be NULL.
164   // Takes ownership of |store|.
165   Directory(
166       DirectoryBackingStore* store,
167       UnrecoverableErrorHandler* unrecoverable_error_handler,
168       ReportUnrecoverableErrorFunction
169           report_unrecoverable_error_function,
170       NigoriHandler* nigori_handler,
171       Cryptographer* cryptographer);
172   virtual ~Directory();
173
174   // Does not take ownership of |delegate|, which must not be NULL.
175   // Starts sending events to |delegate| if the returned result is
176   // OPENED.  Note that events to |delegate| may be sent from *any*
177   // thread.  |transaction_observer| must be initialized.
178   DirOpenResult Open(const std::string& name,
179                      DirectoryChangeDelegate* delegate,
180                      const WeakHandle<TransactionObserver>&
181                          transaction_observer);
182
183   // Stops sending events to the delegate and the transaction
184   // observer.
185   void Close();
186
187   int64 NextMetahandle();
188   // Returns a negative integer unique to this client.
189   syncable::Id NextId();
190
191   bool good() const { return NULL != kernel_; }
192
193   // The download progress is an opaque token provided by the sync server
194   // to indicate the continuation state of the next GetUpdates operation.
195   void GetDownloadProgress(
196       ModelType type,
197       sync_pb::DataTypeProgressMarker* value_out) const;
198   void GetDownloadProgressAsString(
199       ModelType type,
200       std::string* value_out) const;
201   size_t GetEntriesCount() const;
202   void SetDownloadProgress(
203       ModelType type,
204       const sync_pb::DataTypeProgressMarker& value);
205
206   // Gets/Increments transaction version of a model type. Must be called when
207   // holding kernel mutex.
208   int64 GetTransactionVersion(ModelType type) const;
209   void IncrementTransactionVersion(ModelType type);
210
211   // Getter/setters for the per datatype context.
212   void GetDataTypeContext(BaseTransaction* trans,
213                           ModelType type,
214                           sync_pb::DataTypeContext* context) const;
215   void SetDataTypeContext(BaseWriteTransaction* trans,
216                           ModelType type,
217                           const sync_pb::DataTypeContext& context);
218
219   ModelTypeSet InitialSyncEndedTypes();
220   bool InitialSyncEndedForType(ModelType type);
221   bool InitialSyncEndedForType(BaseTransaction* trans, ModelType type);
222
223   const std::string& name() const { return kernel_->name; }
224
225   // (Account) Store birthday is opaque to the client, so we keep it in the
226   // format it is in the proto buffer in case we switch to a binary birthday
227   // later.
228   std::string store_birthday() const;
229   void set_store_birthday(const std::string& store_birthday);
230
231   // (Account) Bag of chip is an opaque state used by the server to track the
232   // client.
233   std::string bag_of_chips() const;
234   void set_bag_of_chips(const std::string& bag_of_chips);
235
236   // Unique to each account / client pair.
237   std::string cache_guid() const;
238
239   // Returns a pointer to our Nigori node handler.
240   NigoriHandler* GetNigoriHandler();
241
242   // Returns a pointer to our cryptographer. Does not transfer ownership.
243   // Not thread safe, so should only be accessed while holding a transaction.
244   Cryptographer* GetCryptographer(const BaseTransaction* trans);
245
246   // Returns true if the directory had encountered an unrecoverable error.
247   // Note: Any function in |Directory| that can be called without holding a
248   // transaction need to check if the Directory already has an unrecoverable
249   // error on it.
250   bool unrecoverable_error_set(const BaseTransaction* trans) const;
251
252   // Called to immediately report an unrecoverable error (but don't
253   // propagate it up).
254   void ReportUnrecoverableError() {
255     if (report_unrecoverable_error_function_) {
256       report_unrecoverable_error_function_();
257     }
258   }
259
260   // Called to set the unrecoverable error on the directory and to propagate
261   // the error to upper layers.
262   void OnUnrecoverableError(const BaseTransaction* trans,
263                             const tracked_objects::Location& location,
264                             const std::string & message);
265
266   DeleteJournal* delete_journal();
267
268   // Returns the child meta handles (even those for deleted/unlinked
269   // nodes) for given parent id.  Clears |result| if there are no
270   // children.
271   bool GetChildHandlesById(BaseTransaction*, const Id& parent_id,
272       Metahandles* result);
273
274   // Counts all items under the given node, including the node itself.
275   int GetTotalNodeCount(BaseTransaction*, EntryKernel* kernel_) const;
276
277   // Returns this item's position within its parent folder.
278   // The left-most item is 0, second left-most is 1, etc.
279   int GetPositionIndex(BaseTransaction*, EntryKernel* kernel_) const;
280
281   // Returns true iff |id| has children.
282   bool HasChildren(BaseTransaction* trans, const Id& id);
283
284   // Find the first child in the positional ordering under a parent,
285   // and fill in |*first_child_id| with its id.  Fills in a root Id if
286   // parent has no children.  Returns true if the first child was
287   // successfully found, or false if an error was encountered.
288   Id GetFirstChildId(BaseTransaction* trans, const EntryKernel* parent);
289
290   // These functions allow one to fetch the next or previous item under
291   // the same folder.  Returns the "root" ID if there is no predecessor
292   // or successor.
293   //
294   // TODO(rlarocque): These functions are used mainly for tree traversal.  We
295   // should replace these with an iterator API.  See crbug.com/178275.
296   syncable::Id GetPredecessorId(EntryKernel*);
297   syncable::Id GetSuccessorId(EntryKernel*);
298
299   // Places |e| as a successor to |predecessor|.  If |predecessor| is NULL,
300   // |e| will be placed as the left-most item in its folder.
301   //
302   // Both |e| and |predecessor| must be valid entries under the same parent.
303   //
304   // TODO(rlarocque): This function includes limited support for placing items
305   // with valid positions (ie. Bookmarks) as siblings of items that have no set
306   // ordering (ie. Autofill items).  This support is required only for tests,
307   // and should be removed.  See crbug.com/178282.
308   void PutPredecessor(EntryKernel* e, EntryKernel* predecessor);
309
310   // SaveChanges works by taking a consistent snapshot of the current Directory
311   // state and indices (by deep copy) under a ReadTransaction, passing this
312   // snapshot to the backing store under no transaction, and finally cleaning
313   // up by either purging entries no longer needed (this part done under a
314   // WriteTransaction) or rolling back the dirty bits.  It also uses
315   // internal locking to enforce SaveChanges operations are mutually exclusive.
316   //
317   // WARNING: THIS METHOD PERFORMS SYNCHRONOUS I/O VIA SQLITE.
318   bool SaveChanges();
319
320   // Returns the number of entities with the unsynced bit set.
321   int64 unsynced_entity_count() const;
322
323   // Get GetUnsyncedMetaHandles should only be called after SaveChanges and
324   // before any new entries have been created. The intention is that the
325   // syncer should call it from its PerformSyncQueries member.
326   void GetUnsyncedMetaHandles(BaseTransaction* trans,
327                               Metahandles* result);
328
329   // Returns whether or not this |type| has unapplied updates.
330   bool TypeHasUnappliedUpdates(ModelType type);
331
332   // Get all the metahandles for unapplied updates for a given set of
333   // server types.
334   void GetUnappliedUpdateMetaHandles(BaseTransaction* trans,
335                                      FullModelTypeSet server_types,
336                                      std::vector<int64>* result);
337
338   // Get all the metahandles of entries of |type|.
339   void GetMetaHandlesOfType(BaseTransaction* trans,
340                             ModelType type,
341                             Metahandles* result);
342
343   // Get metahandle counts for various criteria to show on the
344   // about:sync page. The information is computed on the fly
345   // each time. If this results in a significant performance hit,
346   // additional data structures can be added to cache results.
347   void CollectMetaHandleCounts(std::vector<int>* num_entries_by_type,
348                                std::vector<int>* num_to_delete_entries_by_type);
349
350   // Returns a ListValue serialization of all nodes for the given type.
351   scoped_ptr<base::ListValue> GetNodeDetailsForType(
352       BaseTransaction* trans,
353       ModelType type);
354
355   // Sets the level of invariant checking performed after transactions.
356   void SetInvariantCheckLevel(InvariantCheckLevel check_level);
357
358   // Checks tree metadata consistency following a transaction.  It is intended
359   // to provide a reasonable tradeoff between performance and comprehensiveness
360   // and may be used in release code.
361   bool CheckInvariantsOnTransactionClose(
362       syncable::BaseTransaction* trans,
363       const MetahandleSet& modified_handles);
364
365   // Forces a full check of the directory.  This operation may be slow and
366   // should not be invoked outside of tests.
367   bool FullyCheckTreeInvariants(BaseTransaction *trans);
368
369   // Purges data associated with any entries whose ModelType or ServerModelType
370   // is found in |disabled_types|, from sync directory _both_ in memory and on
371   // disk. Only valid, "real" model types are allowed in |disabled_types| (see
372   // model_type.h for definitions).
373   // 1. Data associated with |types_to_journal| is saved in the delete journal
374   // to help prevent back-from-dead problem due to offline delete in the next
375   // sync session. |types_to_journal| must be a subset of |disabled_types|.
376   // 2. Data associated with |types_to_unapply| is reset to an "unapplied"
377   // state, wherein all local data is deleted and IS_UNAPPLIED is set to true.
378   // This is useful when there's no benefit in discarding the currently
379   // downloaded state, such as when there are cryptographer errors.
380   // |types_to_unapply| must be a subset of |disabled_types|.
381   // 3. All other data is purged entirely.
382   // Note: "Purge" is just meant to distinguish from "deleting" entries, which
383   // means something different in the syncable namespace.
384   // WARNING! This can be real slow, as it iterates over all entries.
385   // WARNING! Performs synchronous I/O.
386   // Returns: true on success, false if an error was encountered.
387   virtual bool PurgeEntriesWithTypeIn(ModelTypeSet disabled_types,
388                                       ModelTypeSet types_to_journal,
389                                       ModelTypeSet types_to_unapply);
390
391   // Resets the base_versions and server_versions of all synced entities
392   // associated with |type| to 1.
393   // WARNING! This can be slow, as it iterates over all entries for a type.
394   bool ResetVersionsForType(BaseWriteTransaction* trans, ModelType type);
395
396   // Returns true iff the attachment identified by |attachment_id_proto| is
397   // linked to an entry.
398   //
399   // An attachment linked to a deleted entry is still considered linked if the
400   // entry hasn't yet been purged.
401   bool IsAttachmentLinked(
402       const sync_pb::AttachmentIdProto& attachment_id_proto) const;
403
404   // Given attachment id return metahandles to all entries that reference this
405   // attachment.
406   void GetMetahandlesByAttachmentId(
407       BaseTransaction* trans,
408       const sync_pb::AttachmentIdProto& attachment_id_proto,
409       Metahandles* result);
410
411   // Change entry to not dirty. Used in special case when we don't want to
412   // persist modified entry on disk. e.g. SyncBackupManager uses this to
413   // preserve sync preferences in DB on disk.
414   void UnmarkDirtyEntry(WriteTransaction* trans, Entry* entry);
415
416  protected:  // for friends, mainly used by Entry constructors
417   virtual EntryKernel* GetEntryByHandle(int64 handle);
418   virtual EntryKernel* GetEntryByHandle(int64 metahandle,
419       ScopedKernelLock* lock);
420   virtual EntryKernel* GetEntryById(const Id& id);
421   EntryKernel* GetEntryByServerTag(const std::string& tag);
422   virtual EntryKernel* GetEntryByClientTag(const std::string& tag);
423   bool ReindexId(BaseWriteTransaction* trans, EntryKernel* const entry,
424                  const Id& new_id);
425   bool ReindexParentId(BaseWriteTransaction* trans, EntryKernel* const entry,
426                        const Id& new_parent_id);
427   // Update the attachment index for |metahandle| removing it from the index
428   // under |old_metadata| entries and add it under |new_metadata| entries.
429   void UpdateAttachmentIndex(const int64 metahandle,
430                              const sync_pb::AttachmentMetadata& old_metadata,
431                              const sync_pb::AttachmentMetadata& new_metadata);
432   void ClearDirtyMetahandles();
433
434   DirOpenResult OpenImpl(
435       const std::string& name,
436       DirectoryChangeDelegate* delegate,
437       const WeakHandle<TransactionObserver>& transaction_observer);
438
439  private:
440   struct Kernel {
441     // |delegate| must not be NULL.  |transaction_observer| must be
442     // initialized.
443     Kernel(const std::string& name, const KernelLoadInfo& info,
444            DirectoryChangeDelegate* delegate,
445            const WeakHandle<TransactionObserver>& transaction_observer);
446
447     ~Kernel();
448
449     // Implements ReadTransaction / WriteTransaction using a simple lock.
450     base::Lock transaction_mutex;
451
452     // Protected by transaction_mutex.  Used by WriteTransactions.
453     int64 next_write_transaction_id;
454
455     // The name of this directory.
456     std::string const name;
457
458     // Protects all members below.
459     // The mutex effectively protects all the indices, but not the
460     // entries themselves.  So once a pointer to an entry is pulled
461     // from the index, the mutex can be unlocked and entry read or written.
462     //
463     // Never hold the mutex and do anything with the database or any
464     // other buffered IO.  Violating this rule will result in deadlock.
465     base::Lock mutex;
466
467     // Entries indexed by metahandle.  This container is considered to be the
468     // owner of all EntryKernels, which may be referened by the other
469     // containers.  If you remove an EntryKernel from this map, you probably
470     // want to remove it from all other containers and delete it, too.
471     MetahandlesMap metahandles_map;
472
473     // Entries indexed by id
474     IdsMap ids_map;
475
476     // Entries indexed by server tag.
477     // This map does not include any entries with non-existent server tags.
478     TagsMap server_tags_map;
479
480     // Entries indexed by client tag.
481     // This map does not include any entries with non-existent client tags.
482     // IS_DEL items are included.
483     TagsMap client_tags_map;
484
485     // Contains non-deleted items, indexed according to parent and position
486     // within parent.  Protected by the ScopedKernelLock.
487     ParentChildIndex parent_child_index;
488
489     // This index keeps track of which metahandles refer to a given attachment.
490     // Think of it as the inverse of EntryKernel's AttachmentMetadata Records.
491     //
492     // Because entries can be undeleted (e.g. PutIsDel(false)), entries should
493     // not removed from the index until they are actually deleted from memory.
494     //
495     // All access should go through IsAttachmentLinked,
496     // RemoveFromAttachmentIndex, AddToAttachmentIndex, and
497     // UpdateAttachmentIndex methods to avoid iterator invalidation errors.
498     IndexByAttachmentId index_by_attachment_id;
499
500     // 3 in-memory indices on bits used extremely frequently by the syncer.
501     // |unapplied_update_metahandles| is keyed by the server model type.
502     MetahandleSet unapplied_update_metahandles[MODEL_TYPE_COUNT];
503     MetahandleSet unsynced_metahandles;
504     // Contains metahandles that are most likely dirty (though not
505     // necessarily).  Dirtyness is confirmed in TakeSnapshotForSaveChanges().
506     MetahandleSet dirty_metahandles;
507
508     // When a purge takes place, we remove items from all our indices and stash
509     // them in here so that SaveChanges can persist their permanent deletion.
510     MetahandleSet metahandles_to_purge;
511
512     KernelShareInfoStatus info_status;
513
514     // These 3 members are backed in the share_info table, and
515     // their state is marked by the flag above.
516
517     // A structure containing the Directory state that is written back into the
518     // database on SaveChanges.
519     PersistedKernelInfo persisted_info;
520
521     // A unique identifier for this account's cache db, used to generate
522     // unique server IDs. No need to lock, only written at init time.
523     const std::string cache_guid;
524
525     // It doesn't make sense for two threads to run SaveChanges at the same
526     // time; this mutex protects that activity.
527     base::Lock save_changes_mutex;
528
529     // The next metahandle is protected by kernel mutex.
530     int64 next_metahandle;
531
532     // The delegate for directory change events.  Must not be NULL.
533     DirectoryChangeDelegate* const delegate;
534
535     // The transaction observer.
536     const WeakHandle<TransactionObserver> transaction_observer;
537   };
538
539   // These private versions expect the kernel lock to already be held
540   // before calling.
541   EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock);
542
543   // A helper that implements the logic of checking tree invariants.
544   bool CheckTreeInvariants(syncable::BaseTransaction* trans,
545                            const MetahandleSet& handles);
546
547   // Helper to prime metahandles_map, ids_map, parent_child_index,
548   // unsynced_metahandles, unapplied_update_metahandles, server_tags_map and
549   // client_tags_map from metahandles_index.  The input |handles_map| will be
550   // cleared during the initialization process.
551   void InitializeIndices(MetahandlesMap* handles_map);
552
553   // Constructs a consistent snapshot of the current Directory state and
554   // indices (by deep copy) under a ReadTransaction for use in |snapshot|.
555   // See SaveChanges() for more information.
556   void TakeSnapshotForSaveChanges(SaveChangesSnapshot* snapshot);
557
558   // Purges from memory any unused, safe to remove entries that were
559   // successfully deleted on disk as a result of the SaveChanges that processed
560   // |snapshot|.  See SaveChanges() for more information.
561   bool VacuumAfterSaveChanges(const SaveChangesSnapshot& snapshot);
562
563   // Rolls back dirty bits in the event that the SaveChanges that
564   // processed |snapshot| failed, for example, due to no disk space.
565   void HandleSaveChangesFailure(const SaveChangesSnapshot& snapshot);
566
567   // For new entry creation only
568   bool InsertEntry(BaseWriteTransaction* trans,
569                    EntryKernel* entry, ScopedKernelLock* lock);
570   bool InsertEntry(BaseWriteTransaction* trans, EntryKernel* entry);
571
572   // Used by CheckTreeInvariants
573   void GetAllMetaHandles(BaseTransaction* trans, MetahandleSet* result);
574   bool SafeToPurgeFromMemory(WriteTransaction* trans,
575                              const EntryKernel* const entry) const;
576
577   // A helper used by GetTotalNodeCount.
578   void GetChildSetForKernel(
579       BaseTransaction*,
580       EntryKernel* kernel_,
581       std::deque<const OrderedChildSet*>* child_sets) const;
582
583   // Append the handles of the children of |parent_id| to |result|.
584   void AppendChildHandles(
585       const ScopedKernelLock& lock,
586       const Id& parent_id, Directory::Metahandles* result);
587
588   // Helper methods used by PurgeDisabledTypes.
589   void UnapplyEntry(EntryKernel* entry);
590   void DeleteEntry(bool save_to_journal,
591                    EntryKernel* entry,
592                    EntryKernelSet* entries_to_journal,
593                    const ScopedKernelLock& lock);
594
595   // Remove each of |metahandle|'s attachment ids from index_by_attachment_id.
596   void RemoveFromAttachmentIndex(
597       const int64 metahandle,
598       const sync_pb::AttachmentMetadata& attachment_metadata,
599       const ScopedKernelLock& lock);
600   // Add each of |metahandle|'s attachment ids to the index_by_attachment_id.
601   void AddToAttachmentIndex(
602       const int64 metahandle,
603       const sync_pb::AttachmentMetadata& attachment_metadata,
604       const ScopedKernelLock& lock);
605
606   Kernel* kernel_;
607
608   scoped_ptr<DirectoryBackingStore> store_;
609
610   UnrecoverableErrorHandler* const unrecoverable_error_handler_;
611   const ReportUnrecoverableErrorFunction report_unrecoverable_error_function_;
612   bool unrecoverable_error_set_;
613
614   // Not owned.
615   NigoriHandler* const nigori_handler_;
616   Cryptographer* const cryptographer_;
617
618   InvariantCheckLevel invariant_check_level_;
619
620   // Maintain deleted entries not in |kernel_| until it's verified that they
621   // are deleted in native models as well.
622   scoped_ptr<DeleteJournal> delete_journal_;
623
624   DISALLOW_COPY_AND_ASSIGN(Directory);
625 };
626
627 }  // namespace syncable
628 }  // namespace syncer
629
630 #endif // SYNC_SYNCABLE_DIRECTORY_H_