summaryrefslogtreecommitdiffstats
path: root/chromium/sync/syncable/syncable_base_transaction.h
blob: 58c7bfafbf5a2733bbc7f53baa342a327b25c07b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_
#define SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_

#include "base/location.h"
#include "sync/base/sync_export.h"
#include "sync/syncable/syncable_id.h"

namespace syncer {
namespace syncable {

class Directory;

// A WriteTransaction has a writer tag describing which body of code is doing
// the write. This is defined up here since WriteTransactionInfo also contains
// one.
enum WriterTag {
  INVALID,
  SYNCER,
  AUTHWATCHER,
  UNITTEST,
  VACUUM_AFTER_SAVE,
  HANDLE_SAVE_FAILURE,
  PURGE_ENTRIES,
  SYNCAPI,
};

// Make sure to update this if you update WriterTag.
std::string WriterTagToString(WriterTag writer_tag);

class SYNC_EXPORT BaseTransaction {
 public:
  static Id root_id();

  Directory* directory() const;

  virtual ~BaseTransaction();

  // This should be called when a database corruption is detected and there is
  // no way for us to recover short of wiping the database clean. When this is
  // called we set a bool in the transaction. The caller has to unwind the
  // stack. When the destructor for the transaction is called it acts upon the
  // bool and calls the Directory to handle the unrecoverable error.
  void OnUnrecoverableError(const tracked_objects::Location& location,
                            const std::string& message);

  bool unrecoverable_error_set() const;

 protected:
  BaseTransaction(const tracked_objects::Location& from_here,
                  const char* name,
                  WriterTag writer,
                  Directory* directory);

  void Lock();
  void Unlock();

  // This should be called before unlocking because it calls the Direcotry's
  // OnUnrecoverableError method which is not protected by locks and could
  // be called from any thread. Holding the transaction lock ensures only one
  // thread could call the method at a time.
  void HandleUnrecoverableErrorIfSet();

  const tracked_objects::Location from_here_;
  const char* const name_;
  WriterTag writer_;
  Directory* const directory_;

  // Error information.
  bool unrecoverable_error_set_;
  tracked_objects::Location unrecoverable_error_location_;
  std::string unrecoverable_error_msg_;

 private:
  friend class Entry;
  DISALLOW_COPY_AND_ASSIGN(BaseTransaction);
};

}  // namespace syncable
}  // namespace syncer

#endif  // SYNC_SYNCABLE_SYNCABLE_BASE_TRANSACTION_H_