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_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/ref_counted.h"
17 #include "content/browser/service_worker/service_worker_context_observer.h"
18 #include "content/common/content_export.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/platform_notification_context.h"
25 class SequencedTaskRunner
;
31 class NotificationDatabase
;
32 struct NotificationDatabaseData
;
33 class ServiceWorkerContextWrapper
;
35 // Implementation of the Web Notification storage context. The public methods
36 // defined in this interface must only be called on the IO thread unless
37 // otherwise specified.
38 class CONTENT_EXPORT PlatformNotificationContextImpl
39 : NON_EXPORTED_BASE(public PlatformNotificationContext
),
40 NON_EXPORTED_BASE(public ServiceWorkerContextObserver
) {
42 // Constructs a new platform notification context. If |path| is non-empty, the
43 // database will be initialized in the "Platform Notifications" subdirectory
44 // of |path|. Otherwise, the database will be initialized in memory. The
45 // constructor must only be called on the IO thread.
46 PlatformNotificationContextImpl(
47 const base::FilePath
& path
,
48 BrowserContext
* browser_context
,
49 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
51 // To be called on the UI thread to initialize the instance.
54 // To be called on the UI thread when the context is being shut down.
57 // PlatformNotificationContext implementation.
58 void ReadNotificationData(int64_t notification_id
,
60 const ReadResultCallback
& callback
) override
;
61 void WriteNotificationData(const GURL
& origin
,
62 const NotificationDatabaseData
& database_data
,
63 const WriteResultCallback
& callback
) override
;
64 void DeleteNotificationData(int64_t notification_id
,
66 const DeleteResultCallback
& callback
) override
;
67 void ReadAllNotificationDataForServiceWorkerRegistration(
69 int64_t service_worker_registration_id
,
70 const ReadAllResultCallback
& callback
) override
;
72 // ServiceWorkerContextObserver implementation.
73 void OnRegistrationDeleted(int64_t registration_id
,
74 const GURL
& pattern
) override
;
75 void OnStorageWiped() override
;
78 friend class PlatformNotificationContextTest
;
80 ~PlatformNotificationContextImpl() override
;
82 void InitializeOnIO();
85 // Initializes the database if neccesary. Must be called on the IO thread.
86 // |success_closure| will be invoked on a the |task_runner_| thread when
87 // everything is available, or |failure_closure_| will be invoked on the
88 // IO thread when initialization fails.
89 void LazyInitialize(const base::Closure
& success_closure
,
90 const base::Closure
& failure_closure
);
92 // Opens the database. Must be called on the |task_runner_| thread. When the
93 // database has been opened, |success_closure| will be invoked on the task
94 // thread, otherwise |failure_closure_| will be invoked on the IO thread.
95 void OpenDatabase(const base::Closure
& success_closure
,
96 const base::Closure
& failure_closure
);
98 // Actually reads the notification data from the database. Must only be
99 // called on the |task_runner_| thread. |callback| will be invoked on the
100 // IO thread when the operation has completed.
101 void DoReadNotificationData(int64_t notification_id
,
103 const ReadResultCallback
& callback
);
105 // Actually reads all notification data from the database. Must only be
106 // called on the |task_runner_| thread. |callback| will be invoked on the
107 // IO thread when the operation has completed.
108 void DoReadAllNotificationDataForServiceWorkerRegistration(
110 int64_t service_worker_registration_id
,
111 const ReadAllResultCallback
& callback
);
113 // Actually writes the notification database to the database. Must only be
114 // called on the |task_runner_| thread. |callback| will be invoked on the
115 // IO thread when the operation has completed.
116 void DoWriteNotificationData(const GURL
& origin
,
117 const NotificationDatabaseData
& database_data
,
118 const WriteResultCallback
& callback
);
120 // Actually deletes the notification information from the database. Must only
121 // be called on the |task_runner_| thread. |callback| will be invoked on the
122 // IO thread when the operation has completed.
123 void DoDeleteNotificationData(int64_t notification_id
,
125 const DeleteResultCallback
& callback
);
127 // Deletes all notifications associated with |service_worker_registration_id|
128 // belonging to |origin|. Must be called on the |task_runner_| thread.
129 void DoDeleteNotificationsForServiceWorkerRegistration(
131 int64_t service_worker_registration_id
);
133 // Destroys the database regardless of its initialization status. This method
134 // must only be called on the |task_runner_| thread. Returns if the directory
135 // the database was stored in could be emptied.
136 bool DestroyDatabase();
138 // Returns the path in which the database should be initialized. May be empty.
139 base::FilePath
GetDatabasePath() const;
141 // Sets the task runner to use for testing purposes.
142 void SetTaskRunnerForTesting(
143 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
145 base::FilePath path_
;
146 BrowserContext
* browser_context_
;
148 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
150 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
151 scoped_ptr
<NotificationDatabase
> database_
;
153 // Indicates whether the database should be pruned when it's opened.
154 bool prune_database_on_open_
= false;
156 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContextImpl
);
159 } // namespace content
161 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_