Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / notifications / platform_notification_context_impl.h
blob757d22e42b130377f72f5d10461b59cca4072186
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_
8 #include <stdint.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"
20 class GURL;
22 namespace base {
23 class SequencedTaskRunner;
26 namespace content {
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> {
40 public:
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.
50 void Initialize();
52 // To be called on the UI thread when the context is being shut down.
53 void Shutdown();
55 // PlatformNotificationContext implementation.
56 void ReadNotificationData(int64_t notification_id,
57 const GURL& origin,
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,
63 const GURL& origin,
64 const DeleteResultCallback& callback) override;
66 // ServiceWorkerContextObserver implementation.
67 void OnRegistrationDeleted(int64_t registration_id,
68 const GURL& pattern) override;
69 void OnStorageWiped() override;
71 private:
72 friend class base::DeleteHelper<PlatformNotificationContextImpl>;
73 friend class base::RefCountedThreadSafe<PlatformNotificationContextImpl,
74 BrowserThread::DeleteOnUIThread>;
75 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
76 friend class PlatformNotificationContextTest;
78 ~PlatformNotificationContextImpl() override;
80 void InitializeOnIO();
81 void ShutdownOnIO();
83 // Initializes the database if neccesary. Must be called on the IO thread.
84 // |success_closure| will be invoked on a the |task_runner_| thread when
85 // everything is available, or |failure_closure_| will be invoked on the
86 // IO thread when initialization fails.
87 void LazyInitialize(const base::Closure& success_closure,
88 const base::Closure& failure_closure);
90 // Opens the database. Must be called on the |task_runner_| thread. When the
91 // database has been opened, |success_closure| will be invoked on the task
92 // thread, otherwise |failure_closure_| will be invoked on the IO thread.
93 void OpenDatabase(const base::Closure& success_closure,
94 const base::Closure& failure_closure);
96 // Actually reads the notification data from the database. Must only be
97 // called on the |task_runner_| thread. |callback| will be invoked on the
98 // IO thread when the operation has completed.
99 void DoReadNotificationData(int64_t notification_id,
100 const GURL& origin,
101 const ReadResultCallback& callback);
103 // Actually writes the notification database to the database. Must only be
104 // called on the |task_runner_| thread. |callback| will be invoked on the
105 // IO thread when the operation has completed.
106 void DoWriteNotificationData(const GURL& origin,
107 const NotificationDatabaseData& database_data,
108 const WriteResultCallback& callback);
110 // Actually deletes the notification information from the database. Must only
111 // be called on the |task_runner_| thread. |callback| will be invoked on the
112 // IO thread when the operation has completed.
113 void DoDeleteNotificationData(int64_t notification_id,
114 const GURL& origin,
115 const DeleteResultCallback& callback);
117 // Deletes all notifications associated with |service_worker_registration_id|
118 // belonging to |origin|. Must be called on the |task_runner_| thread.
119 void DoDeleteNotificationsForServiceWorkerRegistration(
120 const GURL& origin,
121 int64_t service_worker_registration_id);
123 // Destroys the database regardless of its initialization status. This method
124 // must only be called on the |task_runner_| thread. Returns if the directory
125 // the database was stored in could be emptied.
126 bool DestroyDatabase();
128 // Returns the path in which the database should be initialized. May be empty.
129 base::FilePath GetDatabasePath() const;
131 // Sets the task runner to use for testing purposes.
132 void SetTaskRunnerForTesting(
133 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
135 base::FilePath path_;
137 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
139 scoped_refptr<base::SequencedTaskRunner> task_runner_;
140 scoped_ptr<NotificationDatabase> database_;
142 DISALLOW_COPY_AND_ASSIGN(PlatformNotificationContextImpl);
145 } // namespace content
147 #endif // CONTENT_BROWSER_NOTIFICATIONS_PLATFORM_NOTIFICATION_CONTEXT_IMPL_H_