Home | History | Annotate | Download | only in syncable
      1 // Copyright 2012 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_SYNCABLE_BASE_TRANSACTION_H_
      6 #define SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_
      7 
      8 #include "base/location.h"
      9 #include "sync/base/sync_export.h"
     10 #include "sync/syncable/syncable_id.h"
     11 
     12 namespace syncer {
     13 namespace syncable {
     14 
     15 class Directory;
     16 
     17 // A WriteTransaction has a writer tag describing which body of code is doing
     18 // the write. This is defined up here since WriteTransactionInfo also contains
     19 // one.
     20 enum WriterTag {
     21   INVALID,
     22   SYNCER,
     23   AUTHWATCHER,
     24   UNITTEST,
     25   VACUUM_AFTER_SAVE,
     26   HANDLE_SAVE_FAILURE,
     27   PURGE_ENTRIES,
     28   SYNCAPI,
     29 };
     30 
     31 // Make sure to update this if you update WriterTag.
     32 std::string WriterTagToString(WriterTag writer_tag);
     33 
     34 class SYNC_EXPORT BaseTransaction {
     35  public:
     36   Directory* directory() const;
     37   Id root_id() const;
     38 
     39   virtual ~BaseTransaction();
     40 
     41   // This should be called when a database corruption is detected and there is
     42   // no way for us to recover short of wiping the database clean. When this is
     43   // called we set a bool in the transaction. The caller has to unwind the
     44   // stack. When the destructor for the transaction is called it acts upon the
     45   // bool and calls the Directory to handle the unrecoverable error.
     46   void OnUnrecoverableError(const tracked_objects::Location& location,
     47                             const std::string& message);
     48 
     49   bool unrecoverable_error_set() const;
     50 
     51  protected:
     52   BaseTransaction(const tracked_objects::Location& from_here,
     53                   const char* name,
     54                   WriterTag writer,
     55                   Directory* directory);
     56 
     57   void Lock();
     58   void Unlock();
     59 
     60   // This should be called before unlocking because it calls the Direcotry's
     61   // OnUnrecoverableError method which is not protected by locks and could
     62   // be called from any thread. Holding the transaction lock ensures only one
     63   // thread could call the method at a time.
     64   void HandleUnrecoverableErrorIfSet();
     65 
     66   const tracked_objects::Location from_here_;
     67   const char* const name_;
     68   WriterTag writer_;
     69   Directory* const directory_;
     70 
     71   // Error information.
     72   bool unrecoverable_error_set_;
     73   tracked_objects::Location unrecoverable_error_location_;
     74   std::string unrecoverable_error_msg_;
     75 
     76  private:
     77   friend class Entry;
     78   DISALLOW_COPY_AND_ASSIGN(BaseTransaction);
     79 };
     80 
     81 }  // namespace syncable
     82 }  // namespace syncer
     83 
     84 #endif  // SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_
     85