| 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 #ifndef CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ | |
| 6 #define CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "content/common/content_export.h" | |
| 14 | |
| 15 class GURL; | |
| 16 | |
| 17 namespace base { | |
| 18 class SequencedTaskRunner; | |
| 19 } | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 class NotificationDatabase; | |
| 24 struct NotificationDatabaseData; | |
| 25 | |
| 26 // Implementation of the Web Notification storage context. | |
| 27 // | |
| 28 // Represents the storage context for persistent Web Notifications, specific to | |
| 29 // the storage partition owning the instance. The public methods defined in this | |
| 30 // interface must only be called on the IO thread. | |
| 31 class CONTENT_EXPORT PlatformNotificationContext | |
| 32 : public base::RefCountedThreadSafe<PlatformNotificationContext> { | |
| 33 public: | |
| 34 // Constructs a new platform notification context. If |path| is non-empty, the | |
| 35 // database will be initialized in the "Platform Notifications" subdirectory | |
| 36 // of |path|. Otherwise, the database will be initialized in memory. | |
| 37 explicit PlatformNotificationContext(const base::FilePath& path); | |
| 38 | |
| 39 using ReadResultCallback = | |
| 40 base::Callback<void(bool /* success */, | |
| 41 const NotificationDatabaseData&)>; | |
| 42 | |
| 43 using WriteResultCallback = | |
| 44 base::Callback<void(bool /* success */, | |
| 45 int64_t /* notification_id */)>; | |
| 46 | |
| 47 using DeleteResultCallback = base::Callback<void(bool /* success */)>; | |
| 48 | |
| 49 // Reads the data associated with |notification_id| belonging to |origin| | |
| 50 // from the database. |callback| will be invoked with the success status | |
| 51 // and a reference to the notification database data when completed. | |
| 52 void ReadNotificationData(int64_t notification_id, | |
| 53 const GURL& origin, | |
| 54 const ReadResultCallback& callback); | |
| 55 | |
| 56 // Writes the data associated with a notification to a database. When this | |
| 57 // action completed, |callback| will be invoked with the success status and | |
| 58 // the persistent notification id when written successfully. | |
| 59 void WriteNotificationData(const GURL& origin, | |
| 60 const NotificationDatabaseData& database_data, | |
| 61 const WriteResultCallback& callback); | |
| 62 | |
| 63 // Deletes all data associated with |notification_id| belonging to |origin| | |
| 64 // from the database. |callback| will be invoked with the success status | |
| 65 // when the operation has completed. | |
| 66 void DeleteNotificationData(int64_t notification_id, | |
| 67 const GURL& origin, | |
| 68 const DeleteResultCallback& callback); | |
| 69 | |
| 70 private: | |
| 71 friend class base::RefCountedThreadSafe<PlatformNotificationContext>; | |
| 72 friend class PlatformNotificationContextTest; | |
| 73 | |
| 74 virtual ~PlatformNotificationContext(); | |
| 75 | |
| 76 // Initializes the database if neccesary. Must be called on the IO thread. | |
| 77 // |success_closure| will be invoked on a the |task_runner_| thread when | |
| 78 // everything is available, or |failure_closure_| will be invoked on the | |
| 79 // IO thread when initialization fails. | |
| 80 void LazyInitialize(const base::Closure& success_closure, | |
| 81 const base::Closure& failure_closure); | |
| 82 | |
| 83 // Opens the database. Must be called on the |task_runner_| thread. When the | |
| 84 // database has been opened, |success_closure| will be invoked on the task | |
| 85 // thread, otherwise |failure_closure_| will be invoked on the IO thread. | |
| 86 void OpenDatabase(const base::Closure& success_closure, | |
| 87 const base::Closure& failure_closure); | |
| 88 | |
| 89 // Actually reads the notification data from the database. Must only be | |
| 90 // called on the |task_runner_| thread. Will post a task to the IO thread | |
| 91 // for invoking |callback| on when the operation has completed. | |
| 92 void DoReadNotificationData(int64_t notification_id, | |
| 93 const GURL& origin, | |
| 94 const ReadResultCallback& callback); | |
| 95 | |
| 96 // Actually writes the notification database to the database. Must only be | |
| 97 // called on the |task_runner_| thread. Will post a task to the IO thread | |
| 98 // for invoking |callback| on when the operation has completed. | |
| 99 void DoWriteNotificationData(const GURL& origin, | |
| 100 const NotificationDatabaseData& database_data, | |
| 101 const WriteResultCallback& callback); | |
| 102 | |
| 103 // Actually deletes the notification information from the database. Must only | |
| 104 // be called on the |task_runner_| thread. Will post a task to the IO thread | |
| 105 // for invoking |callback| on when the operation has completed. | |
| 106 void DoDeleteNotificationData(int64_t notification_id, | |
| 107 const GURL& origin, | |
| 108 const DeleteResultCallback& callback); | |
| 109 | |
| 110 // Returns the path in which the database should be initialized. May be empty. | |
| 111 base::FilePath GetDatabasePath() const; | |
| 112 | |
| 113 // Sets the task runner to use for testing purposes. | |
| 114 void SetTaskRunnerForTesting( | |
| 115 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
| 116 | |
| 117 base::FilePath path_; | |
| 118 | |
| 119 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 120 scoped_ptr<NotificationDatabase> database_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContext); | |
| 123 }; | |
| 124 | |
| 125 } // namespace content | |
| 126 | |
| 127 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ | |
| OLD | NEW |