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
12namespace syncer {
13namespace syncable {
14
15class 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.
20enum 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.
32std::string WriterTagToString(WriterTag writer_tag);
33
34class SYNC_EXPORT BaseTransaction {
35 public:
36  static Id root_id();
37
38  Directory* directory() const;
39
40  virtual ~BaseTransaction();
41
42  // This should be called when a database corruption is detected and there is
43  // no way for us to recover short of wiping the database clean. When this is
44  // called we set a bool in the transaction. The caller has to unwind the
45  // stack. When the destructor for the transaction is called it acts upon the
46  // bool and calls the Directory to handle the unrecoverable error.
47  void OnUnrecoverableError(const tracked_objects::Location& location,
48                            const std::string& message);
49
50  bool unrecoverable_error_set() const;
51
52 protected:
53  BaseTransaction(const tracked_objects::Location& from_here,
54                  const char* name,
55                  WriterTag writer,
56                  Directory* directory);
57
58  void Lock();
59  void Unlock();
60
61  // This should be called before unlocking because it calls the Direcotry's
62  // OnUnrecoverableError method which is not protected by locks and could
63  // be called from any thread. Holding the transaction lock ensures only one
64  // thread could call the method at a time.
65  void HandleUnrecoverableErrorIfSet();
66
67  const tracked_objects::Location from_here_;
68  const char* const name_;
69  WriterTag writer_;
70  Directory* const directory_;
71
72  // Error information.
73  bool unrecoverable_error_set_;
74  tracked_objects::Location unrecoverable_error_location_;
75  std::string unrecoverable_error_msg_;
76
77 private:
78  friend class Entry;
79  DISALLOW_COPY_AND_ASSIGN(BaseTransaction);
80};
81
82}  // namespace syncable
83}  // namespace syncer
84
85#endif  // SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_
86