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::LazyInitialize( | |
118 const base::Closure& success_closure, | |
119 const base::Closure& failure_closure) { | |
120 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
121 | |
122 if (!task_runner_) { | |
123 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
124 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | |
125 | |
126 task_runner_ = pool->GetSequencedTaskRunner(token); | |
127 } | |
128 | |
129 task_runner_->PostTask( | |
130 FROM_HERE, | |
131 base::Bind(&PlatformNotificationContext::OpenDatabase, | |
132 this, success_closure, failure_closure)); | |
133 } | |
134 | |
135 void PlatformNotificationContext::OpenDatabase( | |
136 const base::Closure& success_closure, | |
137 const base::Closure& failure_closure) { | |
138 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
139 | |
140 if (database_) { | |
141 success_closure.Run(); | |
142 return; | |
143 } | |
144 | |
145 database_.reset(new NotificationDatabase(GetDatabasePath())); | |
146 | |
147 // TODO(peter): Record UMA on |status| for opening the database. | |
148 // TODO(peter): Do the DeleteAndStartOver dance for STATUS_ERROR_CORRUPTED. | |
149 | |
150 NotificationDatabase::Status status = | |
151 database_->Open(true /* create_if_missing */); | |
152 | |
153 if (status == NotificationDatabase::STATUS_OK) { | |
154 success_closure.Run(); | |
155 return; | |
156 } | |
157 | |
158 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); | |
johnme
2015/03/16 16:54:43
Surely you should also reset database_ to nullptr,
Peter Beverloo
2015/03/16 17:55:01
Done.
| |
159 } | |
160 | |
161 base::FilePath PlatformNotificationContext::GetDatabasePath() const { | |
162 if (path_.empty()) | |
163 return path_; | |
164 | |
165 return path_.Append(kPlatformNotificationsDirectory); | |
166 } | |
167 | |
168 void PlatformNotificationContext::SetTaskRunnerForTesting( | |
169 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | |
170 task_runner_ = task_runner; | |
171 } | |
172 | |
173 } // namespace content | |
OLD | NEW |