OLD | NEW |
| (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 } | |
23 | |
24 PlatformNotificationContext::~PlatformNotificationContext() { | |
25 // If the database has been initialized, it must be deleted on the task runner | |
26 // thread as closing it may cause file I/O. | |
27 if (database_) { | |
28 DCHECK(task_runner_); | |
29 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | |
30 } | |
31 } | |
32 | |
33 void PlatformNotificationContext::ReadNotificationData( | |
34 int64_t notification_id, | |
35 const GURL& origin, | |
36 const ReadResultCallback& callback) { | |
37 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
38 LazyInitialize( | |
39 base::Bind(&PlatformNotificationContext::DoReadNotificationData, | |
40 this, notification_id, origin, callback), | |
41 base::Bind(callback, false /* success */, NotificationDatabaseData())); | |
42 } | |
43 | |
44 void PlatformNotificationContext::DoReadNotificationData( | |
45 int64_t notification_id, | |
46 const GURL& origin, | |
47 const ReadResultCallback& callback) { | |
48 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
49 | |
50 NotificationDatabaseData database_data; | |
51 NotificationDatabase::Status status = | |
52 database_->ReadNotificationData(notification_id, | |
53 origin, | |
54 &database_data); | |
55 | |
56 if (status == NotificationDatabase::STATUS_OK) { | |
57 BrowserThread::PostTask(BrowserThread::IO, | |
58 FROM_HERE, | |
59 base::Bind(callback, | |
60 true /* success */, | |
61 database_data)); | |
62 return; | |
63 } | |
64 | |
65 // TODO(peter): Record UMA on |status| for reading from the database. | |
66 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | |
67 | |
68 BrowserThread::PostTask( | |
69 BrowserThread::IO, | |
70 FROM_HERE, | |
71 base::Bind(callback, false /* success */, NotificationDatabaseData())); | |
72 } | |
73 | |
74 void PlatformNotificationContext::WriteNotificationData( | |
75 const GURL& origin, | |
76 const NotificationDatabaseData& database_data, | |
77 const WriteResultCallback& callback) { | |
78 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
79 LazyInitialize( | |
80 base::Bind(&PlatformNotificationContext::DoWriteNotificationData, | |
81 this, origin, database_data, callback), | |
82 base::Bind(callback, false /* success */, 0 /* notification_id */)); | |
83 } | |
84 | |
85 void PlatformNotificationContext::DoWriteNotificationData( | |
86 const GURL& origin, | |
87 const NotificationDatabaseData& database_data, | |
88 const WriteResultCallback& callback) { | |
89 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
90 | |
91 int64_t notification_id = 0; | |
92 NotificationDatabase::Status status = | |
93 database_->WriteNotificationData(origin, | |
94 database_data, | |
95 ¬ification_id); | |
96 | |
97 DCHECK_GT(notification_id, 0); | |
98 | |
99 if (status == NotificationDatabase::STATUS_OK) { | |
100 BrowserThread::PostTask(BrowserThread::IO, | |
101 FROM_HERE, | |
102 base::Bind(callback, | |
103 true /* success */, | |
104 notification_id)); | |
105 return; | |
106 } | |
107 | |
108 // TODO(peter): Record UMA on |status| for reading from the database. | |
109 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | |
110 | |
111 BrowserThread::PostTask( | |
112 BrowserThread::IO, | |
113 FROM_HERE, | |
114 base::Bind(callback, false /* success */, 0 /* notification_id */)); | |
115 } | |
116 | |
117 void PlatformNotificationContext::DeleteNotificationData( | |
118 int64_t notification_id, | |
119 const GURL& origin, | |
120 const DeleteResultCallback& callback) { | |
121 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
122 LazyInitialize( | |
123 base::Bind(&PlatformNotificationContext::DoDeleteNotificationData, | |
124 this, notification_id, origin, callback), | |
125 base::Bind(callback, false /* success */)); | |
126 } | |
127 | |
128 void PlatformNotificationContext::DoDeleteNotificationData( | |
129 int64_t notification_id, | |
130 const GURL& origin, | |
131 const DeleteResultCallback& callback) { | |
132 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
133 | |
134 NotificationDatabase::Status status = | |
135 database_->DeleteNotificationData(notification_id, origin); | |
136 | |
137 const bool success = status == NotificationDatabase::STATUS_OK; | |
138 | |
139 // TODO(peter): Record UMA on |status| for reading from the database. | |
140 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | |
141 | |
142 BrowserThread::PostTask(BrowserThread::IO, | |
143 FROM_HERE, | |
144 base::Bind(callback, success)); | |
145 } | |
146 | |
147 void PlatformNotificationContext::LazyInitialize( | |
148 const base::Closure& success_closure, | |
149 const base::Closure& failure_closure) { | |
150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
151 | |
152 if (!task_runner_) { | |
153 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
154 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
155 | |
156 task_runner_ = pool->GetSequencedTaskRunner(token); | |
157 } | |
158 | |
159 task_runner_->PostTask( | |
160 FROM_HERE, | |
161 base::Bind(&PlatformNotificationContext::OpenDatabase, | |
162 this, success_closure, failure_closure)); | |
163 } | |
164 | |
165 void PlatformNotificationContext::OpenDatabase( | |
166 const base::Closure& success_closure, | |
167 const base::Closure& failure_closure) { | |
168 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
169 | |
170 if (database_) { | |
171 success_closure.Run(); | |
172 return; | |
173 } | |
174 | |
175 database_.reset(new NotificationDatabase(GetDatabasePath())); | |
176 | |
177 // TODO(peter): Record UMA on |status| for opening the database. | |
178 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | |
179 | |
180 NotificationDatabase::Status status = | |
181 database_->Open(true /* create_if_missing */); | |
182 | |
183 if (status == NotificationDatabase::STATUS_OK) { | |
184 success_closure.Run(); | |
185 return; | |
186 } | |
187 | |
188 // TODO(peter): Properly handle failures when opening the database. | |
189 database_.reset(); | |
190 | |
191 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); | |
192 } | |
193 | |
194 base::FilePath PlatformNotificationContext::GetDatabasePath() const { | |
195 if (path_.empty()) | |
196 return path_; | |
197 | |
198 return path_.Append(kPlatformNotificationsDirectory); | |
199 } | |
200 | |
201 void PlatformNotificationContext::SetTaskRunnerForTesting( | |
202 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | |
203 task_runner_ = task_runner; | |
204 } | |
205 | |
206 } // namespace content | |
OLD | NEW |