Chromium Code Reviews| 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 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; | |
|
Bernhard Bauer
2015/03/13 16:51:18
DCHECK you're on the right thread? (Also below)
Peter Beverloo
2015/03/13 16:57:07
All the public methods in database_ have the appro
Bernhard Bauer
2015/03/13 17:24:01
Those do, but there are also methods like LazyInit
Peter Beverloo
2015/03/13 20:43:30
Done.
| |
| 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())); | |
| 124 | |
| 125 task_runner_->PostTask( | |
| 126 FROM_HERE, | |
| 127 base::Bind(&PlatformNotificationContext::OpenDatabase, | |
| 128 this, success_closure, failure_closure)); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 task_runner_->PostTask(FROM_HERE, success_closure); | |
| 133 } | |
| 134 | |
| 135 void PlatformNotificationContext::OpenDatabase( | |
| 136 const base::Closure& success_closure, | |
| 137 const base::Closure& failure_closure) { | |
| 138 DCHECK(database_); | |
| 139 | |
| 140 // TODO(peter): Record UMA on |status| for opening the database. | |
| 141 NotificationDatabase::Status status = | |
| 142 database_->Open(true /* create_if_missing */); | |
| 143 | |
| 144 if (status == NotificationDatabase::STATUS_OK) { | |
| 145 status = database_->GetNextNotificationId(&next_notification_id_); | |
| 146 if (status == NotificationDatabase::STATUS_OK) { | |
| 147 success_closure.Run(); | |
| 148 return; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, failure_closure); | |
| 153 } | |
| 154 | |
| 155 base::FilePath PlatformNotificationContext::GetDatabasePath() const { | |
| 156 if (path_.empty()) | |
| 157 return path_; | |
| 158 | |
| 159 return path_.Append(kPlatformNotificationsDirectory); | |
| 160 } | |
| 161 | |
| 162 void PlatformNotificationContext::SetTaskRunnerForTesting( | |
| 163 scoped_refptr<base::SequencedTaskRunner> task_runner) { | |
| 164 task_runner_ = task_runner; | |
| 165 } | |
| 166 | |
| 167 } // namespace content | |
| OLD | NEW |