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

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

Issue 1006493005: Introduce the PlatformNotificationContext class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@n-db-ReadWriteDelete
Patch Set: fix a typo in the test 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/platform_notification_context.h"
6
7 #include "base/threading/sequenced_worker_pool.h"
8 #include "content/browser/notifications/notification_database.h"
9 #include "content/browser/notifications/notification_database_data.h"
10 #include "content/public/browser/browser_thread.h"
11
12 namespace content {
13
14 // Name of the directory in the user's profile directory where the notification
15 // database files should be stored.
16 const base::FilePath::CharType kPlatformNotificationsDirectory[] =
17 FILE_PATH_LITERAL("Platform Notifications");
18
19 PlatformNotificationContext::PlatformNotificationContext(
20 const base::FilePath& path)
21 : path_(path),
22 next_notification_id_(0) {
23 }
24
25 PlatformNotificationContext::~PlatformNotificationContext() {
26 // If the database has been initialized, it must be deleted on the task runner
27 // thread as closing it may cause file I/O.
28 if (database_) {
29 DCHECK(task_runner_);
30 task_runner_->DeleteSoon(FROM_HERE, database_.release());
31 }
32 }
33
34 void PlatformNotificationContext::ReadNotificationData(
35 int64_t notification_id,
36 const GURL& origin,
37 const ReadResultCallback& callback) {
38 DCHECK_CURRENTLY_ON(BrowserThread::IO);
39 LazyInitialize(
40 base::Bind(&PlatformNotificationContext::DoReadNotificationData,
41 this, notification_id, origin, callback),
42 base::Bind(callback, false /* success */, NotificationDatabaseData()));
43 }
44
45 void PlatformNotificationContext::DoReadNotificationData(
46 int64_t notification_id,
47 const GURL& origin,
48 const ReadResultCallback& callback) {
49 NotificationDatabaseData database_data;
50 NotificationDatabase::Status status =
51 database_->ReadNotificationData(notification_id,
52 origin,
53 &database_data);
54
55 if (status == NotificationDatabase::STATUS_OK) {
56 BrowserThread::PostTask(BrowserThread::IO,
57 FROM_HERE,
58 base::Bind(callback,
59 true /* success */,
60 database_data));
61 return;
62 }
63
64 // TODO(peter): Record UMA on |status| for reading from the database.
65 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED.
66
67 BrowserThread::PostTask(
68 BrowserThread::IO,
69 FROM_HERE,
70 base::Bind(callback, false /* success */, NotificationDatabaseData()));
71 }
72
73 void PlatformNotificationContext::WriteNotificationData(
74 const GURL& origin,
75 const NotificationDatabaseData& database_data,
76 const WriteResultCallback& callback) {
77 DCHECK_CURRENTLY_ON(BrowserThread::IO);
78 LazyInitialize(
79 base::Bind(&PlatformNotificationContext::DoWriteNotificationData,
80 this, origin, database_data, callback),
81 base::Bind(callback, false /* success */, 0 /* notification_id */));
82 }
83
84 void PlatformNotificationContext::DoWriteNotificationData(
85 const GURL& origin,
86 const NotificationDatabaseData& database_data,
87 const WriteResultCallback& callback) {
88 int64_t notification_id = next_notification_id_++;
89 NotificationDatabase::Status status =
90 database_->WriteNotificationData(notification_id,
91 origin,
92 database_data);
93
94 if (status == NotificationDatabase::STATUS_OK) {
95 BrowserThread::PostTask(BrowserThread::IO,
96 FROM_HERE,
97 base::Bind(callback,
98 true /* success */,
99 notification_id));
100 return;
101 }
102
103 // TODO(peter): Record UMA on |status| for reading from the database.
104 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED.
105
106 BrowserThread::PostTask(
107 BrowserThread::IO,
108 FROM_HERE,
109 base::Bind(callback, false /* success */, 0 /* notification_id */));
110 }
111
112 void PlatformNotificationContext::LazyInitialize(
113 const base::Closure& success_closure,
114 const base::Closure& failure_closure) {
115 if (!task_runner_) {
116 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool();
117 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken();
118
119 task_runner_ = pool->GetSequencedTaskRunner(token);
120 }
121
122 if (!database_) {
123 database_.reset(new NotificationDatabase(GetDatabasePath()));
Bernhard Bauer 2015/03/13 17:24:01 Hm, I think you might be able to simplify both thi
Peter Beverloo 2015/03/13 20:43:30 Done.
124 task_runner_->PostTask(
125 FROM_HERE,
126 base::Bind(&PlatformNotificationContext::OpenDatabase,
127 this, success_closure, failure_closure));
128 return;
129 }
130
131 task_runner_->PostTask(FROM_HERE, success_closure);
132 }
133
134 void PlatformNotificationContext::OpenDatabase(
135 const base::Closure& success_closure,
136 const base::Closure& failure_closure) {
137 DCHECK(database_);
138
139 // TODO(peter): Record UMA on |status| for opening the database.
140 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED.
141
142 NotificationDatabase::Status status =
143 database_->Open(true /* create_if_missing */);
144
145 if (status == NotificationDatabase::STATUS_OK) {
146 status = database_->GetNextNotificationId(&next_notification_id_);
147 if (status == NotificationDatabase::STATUS_OK) {
148 success_closure.Run();
149 return;
150 }
151 }
152
153 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure);
154 }
155
156 base::FilePath PlatformNotificationContext::GetDatabasePath() const {
157 if (path_.empty())
158 return path_;
159
160 return path_.Append(kPlatformNotificationsDirectory);
161 }
162
163 void PlatformNotificationContext::SetTaskRunnerForTesting(
164 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
165 task_runner_ = task_runner;
166 }
167
168 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698