Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: content/browser/notifications/notification_database.cc

Issue 988163003: Introduce the NotificationDatabase class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "content/browser/notifications/notification_database.h"
6
7 #include "base/files/file_util.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
10 #include "third_party/leveldatabase/src/include/leveldb/db.h"
11 #include "third_party/leveldatabase/src/include/leveldb/env.h"
12
13 namespace content {
14 namespace {
15
16 // Converts the LevelDB |status| to one of the notification database's values.
17 NotificationDatabase::Status LevelDBStatusToStatus(
18 const leveldb::Status& status) {
19 if (status.ok())
20 return NotificationDatabase::STATUS_OK;
21 else if (status.IsNotFound())
22 return NotificationDatabase::STATUS_ERROR_NOT_FOUND;
23
24 return NotificationDatabase::STATUS_ERROR_FAILED;
25 }
26
27 } // namespace
28
29 NotificationDatabase::NotificationDatabase(const base::FilePath& path)
30 : path_(path),
31 state_(STATE_UNINITIALIZED) {
32 sequence_checker_.DetachFromSequence();
33 }
34
35 NotificationDatabase::~NotificationDatabase() {
36 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
37 db_.reset();
38 }
39
40 NotificationDatabase::Status NotificationDatabase::Open(
41 bool create_if_missing) {
42 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
43 DCHECK_EQ(STATE_UNINITIALIZED, state_);
44
45 if (!create_if_missing) {
46 if (IsInMemoryDatabase() ||
47 !base::PathExists(path_) ||
48 base::IsDirectoryEmpty(path_)) {
49 return NotificationDatabase::STATUS_ERROR_NOT_FOUND;
50 }
51 }
52
53 leveldb::Options options;
54 options.create_if_missing = create_if_missing;
55 options.paranoid_checks = true;
56 if (IsInMemoryDatabase()) {
57 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
58 options.env = env_.get();
59 }
60
61 leveldb::DB* db = nullptr;
62 Status status = LevelDBStatusToStatus(
63 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db));
64 if (status != STATUS_OK)
65 return status;
66
67 state_ = STATE_INITIALIZED;
68 db_.reset(db);
69
70 return STATUS_OK;
71 }
72
73 NotificationDatabase::Status NotificationDatabase::Destroy() {
74 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
75
76 leveldb::Options options;
77 if (IsInMemoryDatabase()) {
78 if (!env_)
79 return STATUS_OK; // The database has not been initialized.
80
81 options.env = env_.get();
82 }
83
84 state_ = STATE_DISABLED;
85 db_.reset();
86
87 return LevelDBStatusToStatus(
88 leveldb::DestroyDB(path_.AsUTF8Unsafe(), options));
89 }
90
91 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698