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
),
38 public base::RefCountedThreadSafe
<PlatformNotificationContextImpl
,
39 BrowserThread::DeleteOnUIThread
> {
41 // Constructs a new platform notification context. If |path| is non-empty, the
42 // database will be initialized in the "Platform Notifications" subdirectory
43 // of |path|. Otherwise, the database will be initialized in memory. The
44 // constructor must only be called on the IO thread.
45 PlatformNotificationContextImpl(
46 const base::FilePath
& path
,
47 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
49 // To be called on the UI thread to initialize the instance.
52 // To be called on the UI thread when the context is being shut down.
55 // PlatformNotificationContext implementation.
56 void ReadNotificationData(int64_t notification_id
,
58 const ReadResultCallback
& callback
) override
;
59 void WriteNotificationData(const GURL
& origin
,
60 const NotificationDatabaseData
& database_data
,
61 const WriteResultCallback
& callback
) override
;
62 void DeleteNotificationData(int64_t notification_id
,
64 const DeleteResultCallback
& callback
) override
;
65 void ReadAllNotificationDataForServiceWorkerRegistration(
67 int64_t service_worker_registration_id
,
68 const ReadAllResultCallback
& callback
) override
;
70 // ServiceWorkerContextObserver implementation.
71 void OnRegistrationDeleted(int64_t registration_id
,
72 const GURL
& pattern
) override
;
73 void OnStorageWiped() override
;
76 friend class base::DeleteHelper
<PlatformNotificationContextImpl
>;
77 friend class base::RefCountedThreadSafe
<PlatformNotificationContextImpl
,
78 BrowserThread::DeleteOnUIThread
>;
79 friend struct BrowserThread::DeleteOnThread
<BrowserThread::UI
>;
80 friend class PlatformNotificationContextTest
;
82 ~PlatformNotificationContextImpl() override
;
84 void InitializeOnIO();
87 // Initializes the database if neccesary. Must be called on the IO thread.
88 // |success_closure| will be invoked on a the |task_runner_| thread when
89 // everything is available, or |failure_closure_| will be invoked on the
90 // IO thread when initialization fails.
91 void LazyInitialize(const base::Closure
& success_closure
,
92 const base::Closure
& failure_closure
);
94 // Opens the database. Must be called on the |task_runner_| thread. When the
95 // database has been opened, |success_closure| will be invoked on the task
96 // thread, otherwise |failure_closure_| will be invoked on the IO thread.
97 void OpenDatabase(const base::Closure
& success_closure
,
98 const base::Closure
& failure_closure
);
100 // Actually reads the notification data from the database. Must only be
101 // called on the |task_runner_| thread. |callback| will be invoked on the
102 // IO thread when the operation has completed.
103 void DoReadNotificationData(int64_t notification_id
,
105 const ReadResultCallback
& callback
);
107 // Actually reads all notification data from the database. Must only be
108 // called on the |task_runner_| thread. |callback| will be invoked on the
109 // IO thread when the operation has completed.
110 void DoReadAllNotificationDataForServiceWorkerRegistration(
112 int64_t service_worker_registration_id
,
113 const ReadAllResultCallback
& callback
);
115 // Actually writes the notification database to the database. Must only be
116 // called on the |task_runner_| thread. |callback| will be invoked on the
117 // IO thread when the operation has completed.
118 void DoWriteNotificationData(const GURL
& origin
,
119 const NotificationDatabaseData
& database_data
,
120 const WriteResultCallback
& callback
);
122 // Actually deletes the notification information from the database. Must only
123 // be called on the |task_runner_| thread. |callback| will be invoked on the
124 // IO thread when the operation has completed.
125 void DoDeleteNotificationData(int64_t notification_id
,
127 const DeleteResultCallback
& callback
);
129 // Deletes all notifications associated with |service_worker_registration_id|
130 // belonging to |origin|. Must be called on the |task_runner_| thread.
131 void DoDeleteNotificationsForServiceWorkerRegistration(
133 int64_t service_worker_registration_id
);
135 // Destroys the database regardless of its initialization status. This method
136 // must only be called on the |task_runner_| thread. Returns if the directory
137 // the database was stored in could be emptied.
138 bool DestroyDatabase();
140 // Returns the path in which the database should be initialized. May be empty.
141 base::FilePath
GetDatabasePath() const;
143 // Sets the task runner to use for testing purposes.
144 void SetTaskRunnerForTesting(
145 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
147 base::FilePath path_
;
149 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
151 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
152 scoped_ptr
<NotificationDatabase
> database_
;
154 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContextImpl
);
157 } // namespace content
159 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_