summaryrefslogtreecommitdiffstats
path: root/content/browser/notifications/notification_database.h
blob: 85b855a0bd56dd433f9d661edaa0ec22bfb24e64 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2015 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 CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_
#define CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_

#include <stdint.h>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/sequence_checker.h"
#include "content/common/content_export.h"

namespace leveldb {
class DB;
class Env;
class WriteBatch;
}

namespace content {

// Implementation of the persistent notification database.
//
// This class can be constructed on any thread, but method calls must only be
// made on a thread or sequenced task runner that allows file I/O. The same
// thread or task runner must be used for all method calls.
class CONTENT_EXPORT NotificationDatabase {
 public:
  // Result status codes for interations with the database. Will be used for
  // UMA, so the assigned ids must remain stable.
  enum Status {
    STATUS_OK = 0,

    // The database, or the key associated with the operation, could not be
    // found.
    STATUS_ERROR_NOT_FOUND = 1,

    // The database, or data in the database, could not be parsed as valid data.
    STATUS_ERROR_CORRUPTED = 2,

    // General failure code. More specific failures should be used if available.
    STATUS_ERROR_FAILED = 3,
  };

  explicit NotificationDatabase(const base::FilePath& path);
  ~NotificationDatabase();

  // Opens the database. If |path| is non-empty, it will be created on the given
  // directory on the filesystem. If |path| is empty, the database will be
  // created in memory instead, and its lifetime will be tied to this instance.
  // |create_if_missing| determines whether to create the database if necessary.
  Status Open(bool create_if_missing);

  // Returns whether the next available notification id could be read, and
  // stores the id in |notification_id| if the read was successful.
  Status GetNextNotificationId(int64_t* notification_id) const;

  // Completely destroys the contents of this database.
  Status Destroy();

 private:
  friend class NotificationDatabaseTest;

  // TODO(peter): Convert to an enum class when DCHECK_EQ supports this.
  // See https://crbug.com/463869.
  enum State {
    STATE_UNINITIALIZED,
    STATE_INITIALIZED,
    STATE_DISABLED,
  };

  // Writes the next available notification id as a put operation to |batch|.
  void WriteNextNotificationId(leveldb::WriteBatch* batch,
                               int64_t next_notification_id) const;

  // Returns whether the database has been opened.
  bool IsOpen() const { return db_ != nullptr; }

  // Returns whether the database should only exist in memory.
  bool IsInMemoryDatabase() const { return path_.empty(); }

  // Exposes the LevelDB database used to back this notification database.
  // Should only be used for testing purposes.
  leveldb::DB* GetDBForTesting() const { return db_.get(); }

  base::FilePath path_;

  // The declaration order for these members matters, as |db_| depends on |env_|
  // and thus has to be destructed first.
  scoped_ptr<leveldb::Env> env_;
  scoped_ptr<leveldb::DB> db_;

  State state_;

  base::SequenceChecker sequence_checker_;

  DISALLOW_COPY_AND_ASSIGN(NotificationDatabase);
};

}  // namespace content

#endif  // CONTENT_BROWSER_NOTIFICATIONS_NOTIFICATION_DATABASE_H_