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

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: 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 else if (status.IsCorruption())
24 return NotificationDatabase::STATUS_ERROR_CORRUPTED;
25
26 return NotificationDatabase::STATUS_ERROR_FAILED;
27 }
28
29 } // namespace
30
31 NotificationDatabase::NotificationDatabase(const base::FilePath& path)
32 : path_(path),
33 state_(STATE_UNINITIALIZED) {
34 sequence_checker_.DetachFromSequence();
35 }
36
37 NotificationDatabase::~NotificationDatabase() {
38 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
39 }
40
41 NotificationDatabase::Status NotificationDatabase::Open(
42 bool create_if_missing) {
43 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
44 DCHECK_EQ(STATE_UNINITIALIZED, state_);
45
46 if (!create_if_missing) {
47 if (IsInMemoryDatabase() ||
48 !base::PathExists(path_) ||
49 base::IsDirectoryEmpty(path_)) {
50 return NotificationDatabase::STATUS_ERROR_NOT_FOUND;
51 }
52 }
53
54 leveldb::Options options;
55 options.create_if_missing = create_if_missing;
56 options.paranoid_checks = true;
57 if (IsInMemoryDatabase()) {
58 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
59 options.env = env_.get();
60 }
61
62 leveldb::DB* db = nullptr;
63 Status status = LevelDBStatusToStatus(
64 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db));
65 if (status != STATUS_OK)
66 return status;
67
68 state_ = STATE_INITIALIZED;
69 db_.reset(db);
70
71 return STATUS_OK;
72 }
73
74 NotificationDatabase::Status NotificationDatabase::Destroy() {
75 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
76
77 leveldb::Options options;
78 if (IsInMemoryDatabase()) {
79 if (!env_)
80 return STATUS_OK; // The database has not been initialized.
81
82 options.env = env_.get();
83 }
84
85 state_ = STATE_DISABLED;
86 db_.reset();
87
88 return LevelDBStatusToStatus(
89 leveldb::DestroyDB(path_.AsUTF8Unsafe(), options));
90 }
91
92 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698