Commit 7015281c authored by peter's avatar peter Committed by Commit bot

Factor Google Now welcome notifications out of DesktopNotificationService.

This changes the ExtensionWelcomeNotification to be its own keyed service,
which seperates it from the DesktopNotificationService.

BUG=

Review URL: https://codereview.chromium.org/580093003

Cr-Commit-Position: refs/heads/master@{#296494}
parent b50c2c7d
......@@ -55,8 +55,6 @@ using message_center::NotifierId;
namespace {
const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
void CancelNotification(const std::string& id) {
g_browser_process->notification_ui_manager()->CancelById(id);
}
......@@ -74,7 +72,6 @@ void DesktopNotificationService::RegisterProfilePrefs(
registry->RegisterListPref(
prefs::kMessageCenterDisabledSystemComponentIds,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
ExtensionWelcomeNotification::RegisterProfilePrefs(registry);
}
// static
......@@ -271,19 +268,6 @@ void DesktopNotificationService::SetNotifierEnabled(
}
}
void DesktopNotificationService::ShowWelcomeNotificationIfNecessary(
const Notification& notification) {
if (!chrome_now_welcome_notification_) {
chrome_now_welcome_notification_ =
ExtensionWelcomeNotification::Create(kChromeNowExtensionID, profile_);
}
if (chrome_now_welcome_notification_) {
chrome_now_welcome_notification_->ShowWelcomeNotificationIfNecessary(
notification);
}
}
void DesktopNotificationService::OnStringListPrefChanged(
const char* pref_name, std::set<std::string>* ids_field) {
ids_field->clear();
......
......@@ -18,7 +18,6 @@
#include "base/scoped_observer.h"
#include "base/strings/string16.h"
#include "chrome/browser/content_settings/permission_context_base.h"
#include "chrome/browser/notifications/extension_welcome_notification.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/keyed_service/core/keyed_service.h"
#include "third_party/WebKit/public/platform/WebNotificationPermission.h"
......@@ -108,10 +107,6 @@ class DesktopNotificationService : public PermissionContextBase
void SetNotifierEnabled(const message_center::NotifierId& notifier_id,
bool enabled);
// Adds in a the welcome notification if required for components built
// into Chrome that show notifications like Chrome Now.
void ShowWelcomeNotificationIfNecessary(const Notification& notification);
private:
// Returns a display name for an origin in the process id, to be used in
// permission infobar or on the frame of the notification toast. Different
......@@ -171,9 +166,6 @@ class DesktopNotificationService : public PermissionContextBase
extension_registry_observer_;
#endif
// Welcome Notification
scoped_ptr<ExtensionWelcomeNotification> chrome_now_welcome_notification_;
base::WeakPtrFactory<DesktopNotificationService> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DesktopNotificationService);
......
......@@ -27,6 +27,8 @@
#include "ui/message_center/notification_types.h"
const int ExtensionWelcomeNotification::kRequestedShowTimeDays = 14;
const char ExtensionWelcomeNotification::kChromeNowExtensionID[] =
"pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
namespace {
......@@ -135,10 +137,10 @@ class DefaultDelegate : public ExtensionWelcomeNotification::Delegate {
} // namespace
ExtensionWelcomeNotification::ExtensionWelcomeNotification(
const std::string& extension_id,
Profile* const profile,
ExtensionWelcomeNotification::Delegate* const delegate)
: notifier_id_(message_center::NotifierId::APPLICATION, extension_id),
: notifier_id_(message_center::NotifierId::APPLICATION,
kChromeNowExtensionID),
profile_(profile),
delegate_(delegate) {
welcome_notification_dismissed_pref_.Init(
......@@ -153,19 +155,15 @@ ExtensionWelcomeNotification::ExtensionWelcomeNotification(
}
// static
scoped_ptr<ExtensionWelcomeNotification> ExtensionWelcomeNotification::Create(
const std::string& extension_id,
ExtensionWelcomeNotification* ExtensionWelcomeNotification::Create(
Profile* const profile) {
return Create(extension_id, profile, new DefaultDelegate()).Pass();
return Create(profile, new DefaultDelegate());
}
// static
scoped_ptr<ExtensionWelcomeNotification> ExtensionWelcomeNotification::Create(
const std::string& extension_id,
Profile* const profile,
Delegate* const delegate) {
return scoped_ptr<ExtensionWelcomeNotification>(
new ExtensionWelcomeNotification(extension_id, profile, delegate)).Pass();
ExtensionWelcomeNotification* ExtensionWelcomeNotification::Create(
Profile* const profile, Delegate* const delegate) {
return new ExtensionWelcomeNotification(profile, delegate);
}
ExtensionWelcomeNotification::~ExtensionWelcomeNotification() {
......
......@@ -11,6 +11,7 @@
#include "base/prefs/pref_member.h"
#include "base/timer/timer.h"
#include "chrome/browser/prefs/pref_service_syncable_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "ui/message_center/notifier_settings.h"
namespace base {
......@@ -32,14 +33,16 @@ class PrefRegistrySyncable;
class Notification;
class Profile;
// ExtensionWelcomeNotification is a part of DesktopNotificationService and
// manages showing and hiding a welcome notification for built-in components
// that show notifications. The Welcome Notification presumes network
// connectivity since it relies on synced preferences to work. This is generally
// fine since the current consumers on the welcome notification also presume
// network connectivity.
// ExtensionWelcomeNotification is a keyed service which manages showing and
// hiding a welcome notification for built-in components that show
// notifications. The Welcome Notification presumes network connectivity since
// it relies on synced preferences to work. This is generally fine since the
// current consumers on the welcome notification also presume network
// connectivity.
//
// This class expects to be created and called from the UI thread.
class ExtensionWelcomeNotification : public PrefServiceSyncableObserver {
class ExtensionWelcomeNotification : public KeyedService,
public PrefServiceSyncableObserver {
public:
// Allows for overriding global calls.
class Delegate {
......@@ -58,22 +61,19 @@ class ExtensionWelcomeNotification : public PrefServiceSyncableObserver {
// Requested time from showing the welcome notification to expiration.
static const int kRequestedShowTimeDays;
// The extension Id associated with the Google Now extension.
static const char kChromeNowExtensionID[];
virtual ~ExtensionWelcomeNotification();
// To workaround the lack of delegating constructors prior to C++11, we use
// static Create methods.
// Creates an ExtensionWelcomeNotification owned by the specified
// extension id with the default delegate.
static scoped_ptr<ExtensionWelcomeNotification> Create(
const std::string& extension_id,
Profile* const profile);
// Creates an ExtensionWelcomeNotification owned by the specified
// extension id with the specified delegate.
static scoped_ptr<ExtensionWelcomeNotification> Create(
const std::string& extension_id,
Profile* const profile,
Delegate* const delegate);
// Creates an ExtensionWelcomeNotification with the default delegate.
static ExtensionWelcomeNotification* Create(Profile* const profile);
// Creates an ExtensionWelcomeNotification with the specified delegate.
static ExtensionWelcomeNotification* Create(Profile* const profile,
Delegate* const delegate);
// PrefServiceSyncableObserver
virtual void OnIsSyncingChanged() OVERRIDE;
......@@ -89,7 +89,6 @@ class ExtensionWelcomeNotification : public PrefServiceSyncableObserver {
enum PopUpRequest { POP_UP_HIDDEN = 0, POP_UP_SHOWN = 1, };
ExtensionWelcomeNotification(
const std::string& extension_id,
Profile* const profile,
ExtensionWelcomeNotification::Delegate* const delegate);
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/notifications/extension_welcome_notification_factory.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/extension_welcome_notification.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_thread.h"
// static
ExtensionWelcomeNotification*
ExtensionWelcomeNotificationFactory::GetForBrowserContext(
content::BrowserContext* context) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
return static_cast<ExtensionWelcomeNotification*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
ExtensionWelcomeNotificationFactory*
ExtensionWelcomeNotificationFactory::GetInstance() {
return Singleton<ExtensionWelcomeNotificationFactory>::get();
}
ExtensionWelcomeNotificationFactory::ExtensionWelcomeNotificationFactory()
: BrowserContextKeyedServiceFactory(
"ExtensionWelcomeNotification",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(DesktopNotificationServiceFactory::GetInstance());
}
ExtensionWelcomeNotificationFactory::~ExtensionWelcomeNotificationFactory() {}
KeyedService* ExtensionWelcomeNotificationFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
return ExtensionWelcomeNotification::Create(static_cast<Profile*>(context));
}
content::BrowserContext*
ExtensionWelcomeNotificationFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextOwnInstanceInIncognito(context);
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_FACTORY_H_
#define CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
class ExtensionWelcomeNotification;
// Singleton owning the extension welcome notification objects and associates
// them with the browser context for which they may have to be shown.
class ExtensionWelcomeNotificationFactory
: public BrowserContextKeyedServiceFactory {
public:
// Returns the ExtensionWelcomeNotification instance to be used for |context|.
static ExtensionWelcomeNotification* GetForBrowserContext(
content::BrowserContext* context);
static ExtensionWelcomeNotificationFactory* GetInstance();
private:
friend struct DefaultSingletonTraits<ExtensionWelcomeNotificationFactory>;
ExtensionWelcomeNotificationFactory();
virtual ~ExtensionWelcomeNotificationFactory();
// BrowserContextKeyedServiceFactory:
virtual KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const OVERRIDE;
virtual content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const OVERRIDE;
};
#endif // CHROME_BROWSER_NOTIFICATIONS_EXTENSION_WELCOME_NOTIFICATION_FACTORY_H_
......@@ -23,8 +23,6 @@
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/notification.h"
const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
class MockMessageCenter : public message_center::FakeMessageCenter {
public:
MockMessageCenter()
......@@ -140,8 +138,8 @@ class ExtensionWelcomeNotificationTest : public testing::Test {
new base::ThreadTaskRunnerHandle(task_runner_));
profile_.reset(new TestingProfile());
delegate_ = new WelcomeNotificationDelegate();
welcome_notification_ = ExtensionWelcomeNotification::Create(
kChromeNowExtensionID, profile_.get(), delegate_);
welcome_notification_.reset(
ExtensionWelcomeNotification::Create(profile_.get(), delegate_));
}
virtual void TearDown() {
......@@ -166,8 +164,9 @@ class ExtensionWelcomeNotificationTest : public testing::Test {
void ShowChromeNowNotification() const {
ShowNotification(
"ChromeNowNotification",
message_center::NotifierId(message_center::NotifierId::APPLICATION,
kChromeNowExtensionID));
message_center::NotifierId(
message_center::NotifierId::APPLICATION,
ExtensionWelcomeNotification::kChromeNowExtensionID));
}
void ShowRegularNotification() const {
......
......@@ -8,13 +8,13 @@
#include "base/metrics/sparse_histogram.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/notifications/extension_welcome_notification.h"
#include "chrome/browser/notifications/notification.h"
#include "chrome/browser/notifications/notification_ui_manager.h"
#include "content/public/browser/user_metrics.h"
#include "ui/message_center/notification.h"
namespace {
const char kChromeNowExtensionID[] = "pafkbggdmjlpgkdkcbjmhmfcdpncadgh";
const int kNotificationsMaxCount = 20;
}
......@@ -57,7 +57,8 @@ bool GoogleNowNotificationStatsCollector::IsNotificationIdForGoogleNow(
isGoogleNowNotification =
((notification->notifier_id().type ==
message_center::NotifierId::APPLICATION) &&
(notification->notifier_id().id == kChromeNowExtensionID));
(notification->notifier_id().id ==
ExtensionWelcomeNotification::kChromeNowExtensionID));
}
return isGoogleNowNotification;
}
......@@ -70,7 +71,8 @@ int GoogleNowNotificationStatsCollector::CountVisibleGoogleNowNotifications() {
for (Notifications::iterator iter = visible_notifications.begin();
iter != visible_notifications.end();
++iter) {
if ((*iter)->notifier_id().id == kChromeNowExtensionID)
if ((*iter)->notifier_id().id ==
ExtensionWelcomeNotification::kChromeNowExtensionID)
google_now_notification_count++;
}
return google_now_notification_count;
......
......@@ -13,6 +13,8 @@
#include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/desktop_notification_service_factory.h"
#include "chrome/browser/notifications/extension_welcome_notification.h"
#include "chrome/browser/notifications/extension_welcome_notification_factory.h"
#include "chrome/browser/notifications/fullscreen_notification_blocker.h"
#include "chrome/browser/notifications/message_center_settings_controller.h"
#include "chrome/browser/notifications/notification.h"
......@@ -122,7 +124,7 @@ void MessageCenterNotificationManager::Add(const Notification& notification,
if (Update(notification, profile))
return;
DesktopNotificationServiceFactory::GetForProfile(profile)->
ExtensionWelcomeNotificationFactory::GetForBrowserContext(profile)->
ShowWelcomeNotificationIfNecessary(notification);
// WARNING: You MUST update the message center via the notification within a
......
......@@ -43,6 +43,7 @@
#include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
#include "chrome/browser/net/ssl_config_service_manager.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/extension_welcome_notification.h"
#include "chrome/browser/notifications/message_center_notification_manager.h"
#include "chrome/browser/pepper_flash_settings_manager.h"
#include "chrome/browser/plugins/plugin_finder.h"
......@@ -428,6 +429,13 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
DesktopNotificationService::RegisterProfilePrefs(registry);
#endif
#if defined(ENABLE_NOTIFICATIONS) && defined(ENABLE_EXTENSIONS) && \
!defined(OS_ANDROID)
// The extension welcome notification requires a build that enables extensions
// and notifications, and uses the UI message center.
ExtensionWelcomeNotification::RegisterProfilePrefs(registry);
#endif
#if defined(ENABLE_SERVICE_DISCOVERY)
LocalDiscoveryUI::RegisterProfilePrefs(registry);
#endif
......
......@@ -1758,10 +1758,6 @@
'browser/notifications/desktop_notification_service.h',
'browser/notifications/desktop_notification_service_factory.cc',
'browser/notifications/desktop_notification_service_factory.h',
'browser/notifications/extension_welcome_notification.cc',
'browser/notifications/extension_welcome_notification.h',
'browser/notifications/google_now_notification_stats_collector.cc',
'browser/notifications/google_now_notification_stats_collector.h',
'browser/notifications/message_center_notification_manager_win.cc',
'browser/notifications/notification.cc',
'browser/notifications/notification.h',
......@@ -1783,6 +1779,12 @@
],
# Used on non-Android platforms when notifications are enabled.
'chrome_browser_non_android_notifications_sources': [
'browser/notifications/extension_welcome_notification.cc',
'browser/notifications/extension_welcome_notification.h',
'browser/notifications/extension_welcome_notification_factory.cc',
'browser/notifications/extension_welcome_notification_factory.h',
'browser/notifications/google_now_notification_stats_collector.cc',
'browser/notifications/google_now_notification_stats_collector.h',
'browser/notifications/message_center_notification_manager.cc',
'browser/notifications/message_center_notification_manager.h',
'browser/notifications/message_center_settings_controller.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment