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 per-BrowserContext data for persistent Web Notifications. |
| 29 // The methods defined in this interface must only be called on the IO thread. |
| 30 class CONTENT_EXPORT PlatformNotificationContext |
| 31 : public base::RefCountedThreadSafe<PlatformNotificationContext> { |
| 32 public: |
| 33 // Constructs a new platform notification context. If |path| is non-empty, the |
| 34 // database will be initialized in the "Platform Notifications" subdirectory |
| 35 // of |path|. Otherwise, the database will be initialized in memory. |
| 36 explicit PlatformNotificationContext(const base::FilePath& path); |
| 37 |
| 38 using ReadResultCallback = |
| 39 base::Callback<void(bool /* success */, |
| 40 const NotificationDatabaseData&)>; |
| 41 |
| 42 using WriteResultCallback = |
| 43 base::Callback<void(bool /* success */, |
| 44 int64_t /* notification_id */)>; |
| 45 |
| 46 // Reads the data associated with |notification_id| belonging to |origin| |
| 47 // from the database. |callback| will be invoked with the success status |
| 48 // and a reference to the notification database data when completed. |
| 49 void ReadNotificationData(int64_t notification_id, |
| 50 const GURL& origin, |
| 51 const ReadResultCallback& callback); |
| 52 |
| 53 // Writes the data associated with a notification to a database. When this |
| 54 // action completed, |callback| will be invoked with the success status and |
| 55 // the persistent notification id when written successfully. |
| 56 void WriteNotificationData(const GURL& origin, |
| 57 const NotificationDatabaseData& database_data, |
| 58 const WriteResultCallback& callback); |
| 59 |
| 60 private: |
| 61 friend class base::RefCountedThreadSafe<PlatformNotificationContext>; |
| 62 friend class PlatformNotificationContextTest; |
| 63 |
| 64 virtual ~PlatformNotificationContext(); |
| 65 |
| 66 // Initializes the database if neccesary. Must be called on the IO thread. |
| 67 // |success_closure| will be invoked on a the |task_runner_| thread when |
| 68 // everything is available, or |failure_closure_| will be invoked on the |
| 69 // IO thread when initialization fails. |
| 70 void LazyInitialize(const base::Closure& success_closure, |
| 71 const base::Closure& failure_closure); |
| 72 |
| 73 // Opens the database. Must be called on the |task_runner_| thread. When the |
| 74 // database has been opened, |success_closure| will be invoked on the task |
| 75 // thread, otherwise |failure_closure_ will be invoked on the IO thread. |
| 76 void OpenDatabase(const base::Closure& success_closure, |
| 77 const base::Closure& failure_closure); |
| 78 |
| 79 // Actually reads the notification data from the database. Must only be |
| 80 // called on the |task_runner_| thread. Will post a task to |callback| on |
| 81 // the IO thread when the operation has completed. |
| 82 void DoReadNotificationData(int64_t notification_id, |
| 83 const GURL& origin, |
| 84 const ReadResultCallback& callback); |
| 85 |
| 86 // Actually writes the notification database to the database. Must only be |
| 87 // called on the |task_runner_| thread. Will post a task to |callback| on |
| 88 // the IO thread when the operation has completed. |
| 89 void DoWriteNotificationData(const GURL& origin, |
| 90 const NotificationDatabaseData& database_data, |
| 91 const WriteResultCallback& callback); |
| 92 |
| 93 // Returns the path in which the database should be initialized. May be empty. |
| 94 base::FilePath GetDatabasePath() const; |
| 95 |
| 96 // Sets the task runner to use for testing purposes. |
| 97 void SetTaskRunnerForTesting( |
| 98 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 99 |
| 100 base::FilePath path_; |
| 101 |
| 102 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 103 scoped_ptr<NotificationDatabase> database_; |
| 104 |
| 105 int64_t next_notification_id_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContext); |
| 108 }; |
| 109 |
| 110 } // namespace content |
| 111 |
| 112 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_H_ |
OLD | NEW |