OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/notifications/platform_notification_context_impl.h" | 5 #include "content/browser/notifications/platform_notification_context_impl.h" |
6 | 6 |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "content/browser/notifications/notification_database.h" | 11 #include "content/browser/notifications/notification_database.h" |
12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/content_browser_client.h" | |
14 #include "content/public/browser/notification_database_data.h" | 15 #include "content/public/browser/notification_database_data.h" |
16 #include "content/public/browser/platform_notification_service.h" | |
15 | 17 |
16 using base::DoNothing; | 18 using base::DoNothing; |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 | 21 |
20 // Name of the directory in the user's profile directory where the notification | 22 // Name of the directory in the user's profile directory where the notification |
21 // database files should be stored. | 23 // database files should be stored. |
22 const base::FilePath::CharType kPlatformNotificationsDirectory[] = | 24 const base::FilePath::CharType kPlatformNotificationsDirectory[] = |
23 FILE_PATH_LITERAL("Platform Notifications"); | 25 FILE_PATH_LITERAL("Platform Notifications"); |
24 | 26 |
25 PlatformNotificationContextImpl::PlatformNotificationContextImpl( | 27 PlatformNotificationContextImpl::PlatformNotificationContextImpl( |
26 const base::FilePath& path, | 28 const base::FilePath& path, |
29 BrowserContext* browser_context, | |
27 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 30 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
28 : path_(path), | 31 : path_(path), |
32 browser_context_(browser_context), | |
29 service_worker_context_(service_worker_context) { | 33 service_worker_context_(service_worker_context) { |
30 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
31 } | 35 } |
32 | 36 |
33 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { | 37 PlatformNotificationContextImpl::~PlatformNotificationContextImpl() { |
34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 38 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
35 | 39 |
36 // If the database has been initialized, it must be deleted on the task runner | 40 // If the database has been initialized, it must be deleted on the task runner |
37 // thread as closing it may cause file I/O. | 41 // thread as closing it may cause file I/O. |
38 if (database_) { | 42 if (database_) { |
39 DCHECK(task_runner_); | 43 DCHECK(task_runner_); |
40 task_runner_->DeleteSoon(FROM_HERE, database_.release()); | 44 task_runner_->DeleteSoon(FROM_HERE, database_.release()); |
41 } | 45 } |
42 } | 46 } |
43 | 47 |
44 void PlatformNotificationContextImpl::Initialize() { | 48 void PlatformNotificationContextImpl::Initialize() { |
45 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
50 PlatformNotificationService* service = | |
51 GetContentClient()->browser()->GetPlatformNotificationService(); | |
52 if (service) { | |
53 std::set<std::string> displayed_notifications; | |
54 | |
55 bool notification_synchronization_supported = | |
56 service->GetDisplayedPersistentNotifications(browser_context_, | |
57 &displayed_notifications); | |
58 | |
59 // Synchronize the notifications stored in the database with the set of | |
60 // displaying notifications in |displayed_notifications|. This is necessary | |
61 // because flakiness may cause a platform to inform Chrome of a notification | |
62 // that has since been closed, or because the platform does not support | |
63 // notifications that exceed the lifetime of the browser process. | |
64 | |
65 // TODO(peter): Synchronizing the actual notifications will be done when the | |
66 // persistent notification ids are stable. For M44 we need to support the | |
67 // case where there may be no notifications after a Chrome restart. | |
68 if (notification_synchronization_supported && | |
69 !displayed_notifications.size()) { | |
johnme
2015/05/14 15:22:32
Until you implement GetDisplayedPersistentNotifica
Peter Beverloo
2015/05/14 16:03:35
Supporting the Android case is covered by |notific
| |
70 prune_database_on_open_ = true; | |
71 } | |
72 } | |
73 | |
46 BrowserThread::PostTask( | 74 BrowserThread::PostTask( |
47 BrowserThread::IO, | 75 BrowserThread::IO, |
48 FROM_HERE, | 76 FROM_HERE, |
49 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); | 77 base::Bind(&PlatformNotificationContextImpl::InitializeOnIO, this)); |
50 } | 78 } |
51 | 79 |
52 void PlatformNotificationContextImpl::InitializeOnIO() { | 80 void PlatformNotificationContextImpl::InitializeOnIO() { |
53 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
54 | 82 |
55 // |service_worker_context_| may be NULL in tests. | 83 // |service_worker_context_| may be NULL in tests. |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 void PlatformNotificationContextImpl::OpenDatabase( | 344 void PlatformNotificationContextImpl::OpenDatabase( |
317 const base::Closure& success_closure, | 345 const base::Closure& success_closure, |
318 const base::Closure& failure_closure) { | 346 const base::Closure& failure_closure) { |
319 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 347 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
320 | 348 |
321 if (database_) { | 349 if (database_) { |
322 success_closure.Run(); | 350 success_closure.Run(); |
323 return; | 351 return; |
324 } | 352 } |
325 | 353 |
354 if (prune_database_on_open_) { | |
355 prune_database_on_open_ = false; | |
356 DestroyDatabase(); | |
johnme
2015/05/14 15:22:32
- Shouldn't this go after `database_.reset(new Not
Peter Beverloo
2015/05/14 16:03:35
Done
johnme
2015/05/14 16:40:51
I'm talking about when DestroyDatabase returns fal
| |
357 } | |
358 | |
326 database_.reset(new NotificationDatabase(GetDatabasePath())); | 359 database_.reset(new NotificationDatabase(GetDatabasePath())); |
327 NotificationDatabase::Status status = | 360 NotificationDatabase::Status status = |
328 database_->Open(true /* create_if_missing */); | 361 database_->Open(true /* create_if_missing */); |
329 | 362 |
330 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.OpenResult", | 363 UMA_HISTOGRAM_ENUMERATION("Notifications.Database.OpenResult", |
331 status, NotificationDatabase::STATUS_COUNT); | 364 status, NotificationDatabase::STATUS_COUNT); |
332 | 365 |
333 // When the database could not be opened due to corruption, destroy it, blow | 366 // When the database could not be opened due to corruption, destroy it, blow |
334 // away the contents of the directory and try re-opening the database. | 367 // away the contents of the directory and try re-opening the database. |
335 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) { | 368 if (status == NotificationDatabase::STATUS_ERROR_CORRUPTED) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 | 412 |
380 return path_.Append(kPlatformNotificationsDirectory); | 413 return path_.Append(kPlatformNotificationsDirectory); |
381 } | 414 } |
382 | 415 |
383 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( | 416 void PlatformNotificationContextImpl::SetTaskRunnerForTesting( |
384 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { | 417 const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
385 task_runner_ = task_runner; | 418 task_runner_ = task_runner; |
386 } | 419 } |
387 | 420 |
388 } // namespace content | 421 } // namespace content |
OLD | NEW |