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.
5 #ifndef CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_
6 #define CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "content/browser/service_worker/service_worker_context_observer.h"
16 #include "content/common/content_export.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/platform_notification_context.h"
23 class SequencedTaskRunner
;
28 class NotificationDatabase
;
29 struct NotificationDatabaseData
;
30 class ServiceWorkerContextWrapper
;
32 // Implementation of the Web Notification storage context. The public methods
33 // defined in this interface must only be called on the IO thread unless
34 // otherwise specified.
35 class CONTENT_EXPORT PlatformNotificationContextImpl
36 : NON_EXPORTED_BASE(public PlatformNotificationContext
),
37 NON_EXPORTED_BASE(public ServiceWorkerContextObserver
) {
39 // Constructs a new platform notification context. If |path| is non-empty, the
40 // database will be initialized in the "Platform Notifications" subdirectory
41 // of |path|. Otherwise, the database will be initialized in memory. The
42 // constructor must only be called on the IO thread.
43 PlatformNotificationContextImpl(
44 const base::FilePath
& path
,
45 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
47 // To be called on the UI thread to initialize the instance.
50 // To be called on the UI thread when the context is being shut down.
53 // PlatformNotificationContext implementation.
54 void ReadNotificationData(int64_t notification_id
,
56 const ReadResultCallback
& callback
) override
;
57 void WriteNotificationData(const GURL
& origin
,
58 const NotificationDatabaseData
& database_data
,
59 const WriteResultCallback
& callback
) override
;
60 void DeleteNotificationData(int64_t notification_id
,
62 const DeleteResultCallback
& callback
) override
;
63 void ReadAllNotificationDataForServiceWorkerRegistration(
65 int64_t service_worker_registration_id
,
66 const ReadAllResultCallback
& callback
) override
;
68 // ServiceWorkerContextObserver implementation.
69 void OnRegistrationDeleted(int64_t registration_id
,
70 const GURL
& pattern
) override
;
71 void OnStorageWiped() override
;
74 friend class PlatformNotificationContextTest
;
76 ~PlatformNotificationContextImpl() override
;
78 void InitializeOnIO();
81 // Initializes the database if neccesary. Must be called on the IO thread.
82 // |success_closure| will be invoked on a the |task_runner_| thread when
83 // everything is available, or |failure_closure_| will be invoked on the
84 // IO thread when initialization fails.
85 void LazyInitialize(const base::Closure
& success_closure
,
86 const base::Closure
& failure_closure
);
88 // Opens the database. Must be called on the |task_runner_| thread. When the
89 // database has been opened, |success_closure| will be invoked on the task
90 // thread, otherwise |failure_closure_| will be invoked on the IO thread.
91 void OpenDatabase(const base::Closure
& success_closure
,
92 const base::Closure
& failure_closure
);
94 // Actually reads the notification data from the database. Must only be
95 // called on the |task_runner_| thread. |callback| will be invoked on the
96 // IO thread when the operation has completed.
97 void DoReadNotificationData(int64_t notification_id
,
99 const ReadResultCallback
& callback
);
101 // Actually reads all notification data from the database. Must only be
102 // called on the |task_runner_| thread. |callback| will be invoked on the
103 // IO thread when the operation has completed.
104 void DoReadAllNotificationDataForServiceWorkerRegistration(
106 int64_t service_worker_registration_id
,
107 const ReadAllResultCallback
& callback
);
109 // Actually writes the notification database to the database. Must only be
110 // called on the |task_runner_| thread. |callback| will be invoked on the
111 // IO thread when the operation has completed.
112 void DoWriteNotificationData(const GURL
& origin
,
113 const NotificationDatabaseData
& database_data
,
114 const WriteResultCallback
& callback
);
116 // Actually deletes the notification information from the database. Must only
117 // be called on the |task_runner_| thread. |callback| will be invoked on the
118 // IO thread when the operation has completed.
119 void DoDeleteNotificationData(int64_t notification_id
,
121 const DeleteResultCallback
& callback
);
123 // Deletes all notifications associated with |service_worker_registration_id|
124 // belonging to |origin|. Must be called on the |task_runner_| thread.
125 void DoDeleteNotificationsForServiceWorkerRegistration(
127 int64_t service_worker_registration_id
);
129 // Destroys the database regardless of its initialization status. This method
130 // must only be called on the |task_runner_| thread. Returns if the directory
131 // the database was stored in could be emptied.
132 bool DestroyDatabase();
134 // Returns the path in which the database should be initialized. May be empty.
135 base::FilePath
GetDatabasePath() const;
137 // Sets the task runner to use for testing purposes.
138 void SetTaskRunnerForTesting(
139 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
141 base::FilePath path_
;
143 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
145 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
146 scoped_ptr
<NotificationDatabase
> database_
;
148 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContextImpl
);
151 } // namespace content
153 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_