Expose NotificationPresenter to public

This commit is contained in:
Cheng Zhao 2015-12-24 22:20:47 +08:00
parent 9897f3aab0
commit 6f81d1e29f
5 changed files with 57 additions and 58 deletions

View file

@ -4,15 +4,19 @@
#include "browser/browser_client.h" #include "browser/browser_client.h"
#include "base/path_service.h"
#include "browser/browser_context.h" #include "browser/browser_context.h"
#include "browser/browser_main_parts.h" #include "browser/browser_main_parts.h"
#include "browser/devtools_manager_delegate.h" #include "browser/devtools_manager_delegate.h"
#include "browser/media/media_capture_devices_dispatcher.h" #include "browser/media/media_capture_devices_dispatcher.h"
#include "browser/platform_notification_service_impl.h" #include "browser/notification_presenter.h"
#include "browser/platform_notification_service.h"
#include "base/path_service.h"
#include "content/public/common/url_constants.h" #include "content/public/common/url_constants.h"
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#endif
namespace brightray { namespace brightray {
namespace { namespace {
@ -38,6 +42,20 @@ BrowserContext* BrowserClient::browser_context() {
return browser_main_parts_->browser_context(); return browser_main_parts_->browser_context();
} }
NotificationPresenter* BrowserClient::GetNotificationPresenter() {
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return nullptr;
#endif
if (!notification_presenter_) {
// Create a new presenter if on OS X, Linux, or Windows 8+
notification_presenter_.reset(NotificationPresenter::Create());
}
return notification_presenter_.get();
}
BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts( BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) { const content::MainFunctionParams&) {
return new BrowserMainParts; return new BrowserMainParts;
@ -65,7 +83,9 @@ content::MediaObserver* BrowserClient::GetMediaObserver() {
} }
content::PlatformNotificationService* BrowserClient::GetPlatformNotificationService() { content::PlatformNotificationService* BrowserClient::GetPlatformNotificationService() {
return PlatformNotificationServiceImpl::GetInstance(); if (!notification_service_)
notification_service_.reset(new PlatformNotificationService(this));
return notification_service_.get();
} }
void BrowserClient::GetAdditionalAllowedSchemesForFileSystem( void BrowserClient::GetAdditionalAllowedSchemesForFileSystem(

View file

@ -13,6 +13,8 @@ namespace brightray {
class BrowserContext; class BrowserContext;
class BrowserMainParts; class BrowserMainParts;
class NetLog; class NetLog;
class NotificationPresenter;
class PlatformNotificationService;
class BrowserClient : public content::ContentBrowserClient { class BrowserClient : public content::ContentBrowserClient {
public: public:
@ -24,6 +26,8 @@ class BrowserClient : public content::ContentBrowserClient {
BrowserContext* browser_context(); BrowserContext* browser_context();
BrowserMainParts* browser_main_parts() { return browser_main_parts_; } BrowserMainParts* browser_main_parts() { return browser_main_parts_; }
NotificationPresenter* GetNotificationPresenter();
protected: protected:
// Subclasses should override this to provide their own BrowserMainParts // Subclasses should override this to provide their own BrowserMainParts
// implementation. The lifetime of the returned instance is managed by the // implementation. The lifetime of the returned instance is managed by the
@ -51,6 +55,9 @@ class BrowserClient : public content::ContentBrowserClient {
NetLog net_log_; NetLog net_log_;
private: private:
scoped_ptr<PlatformNotificationService> notification_service_;
scoped_ptr<NotificationPresenter> notification_presenter_;
DISALLOW_COPY_AND_ASSIGN(BrowserClient); DISALLOW_COPY_AND_ASSIGN(BrowserClient);
}; };

View file

@ -2,68 +2,48 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file. // found in the LICENSE-CHROMIUM file.
#include "browser/platform_notification_service_impl.h" #include "browser/platform_notification_service.h"
#include "browser/browser_client.h"
#include "browser/notification_presenter.h" #include "browser/notification_presenter.h"
#include "content/public/browser/desktop_notification_delegate.h" #include "content/public/browser/desktop_notification_delegate.h"
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#endif
namespace brightray { namespace brightray {
// static PlatformNotificationService::PlatformNotificationService(
PlatformNotificationServiceImpl* BrowserClient* browser_client)
PlatformNotificationServiceImpl::GetInstance() { : browser_client_(browser_client) {
return base::Singleton<PlatformNotificationServiceImpl>::get();
} }
PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() {} PlatformNotificationService::~PlatformNotificationService() {}
PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {}
NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() { blink::WebNotificationPermission PlatformNotificationService::CheckPermissionOnUIThread(
#if defined(OS_WIN)
// Bail out if on Windows 7 or even lower, no operating will follow
if (base::win::GetVersion() < base::win::VERSION_WIN8)
return nullptr;
#endif
if (!notification_presenter_) {
// Create a new presenter if on OS X, Linux, or Windows 8+
notification_presenter_.reset(NotificationPresenter::Create());
}
return notification_presenter_.get();
}
blink::WebNotificationPermission PlatformNotificationServiceImpl::CheckPermissionOnUIThread(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
const GURL& origin, const GURL& origin,
int render_process_id) { int render_process_id) {
return blink::WebNotificationPermissionAllowed; return blink::WebNotificationPermissionAllowed;
} }
blink::WebNotificationPermission PlatformNotificationServiceImpl::CheckPermissionOnIOThread( blink::WebNotificationPermission PlatformNotificationService::CheckPermissionOnIOThread(
content::ResourceContext* resource_context, content::ResourceContext* resource_context,
const GURL& origin, const GURL& origin,
int render_process_id) { int render_process_id) {
return blink::WebNotificationPermissionAllowed; return blink::WebNotificationPermissionAllowed;
} }
void PlatformNotificationServiceImpl::DisplayNotification( void PlatformNotificationService::DisplayNotification(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
const GURL& origin, const GURL& origin,
const SkBitmap& icon, const SkBitmap& icon,
const content::PlatformNotificationData& notification_data, const content::PlatformNotificationData& notification_data,
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) { base::Closure* cancel_callback) {
auto presenter = notification_presenter(); auto presenter = browser_client_->GetNotificationPresenter();
if (presenter) if (presenter)
presenter->ShowNotification(notification_data, icon, delegate.Pass(), cancel_callback); presenter->ShowNotification(notification_data, icon, delegate.Pass(), cancel_callback);
} }
void PlatformNotificationServiceImpl::DisplayPersistentNotification( void PlatformNotificationService::DisplayPersistentNotification(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
int64_t service_worker_registration_id, int64_t service_worker_registration_id,
const GURL& origin, const GURL& origin,
@ -71,12 +51,12 @@ void PlatformNotificationServiceImpl::DisplayPersistentNotification(
const content::PlatformNotificationData& notification_data) { const content::PlatformNotificationData& notification_data) {
} }
void PlatformNotificationServiceImpl::ClosePersistentNotification( void PlatformNotificationService::ClosePersistentNotification(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
int64_t persistent_notification_id) { int64_t persistent_notification_id) {
} }
bool PlatformNotificationServiceImpl::GetDisplayedPersistentNotifications( bool PlatformNotificationService::GetDisplayedPersistentNotifications(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
std::set<std::string>* displayed_notifications) { std::set<std::string>* displayed_notifications) {
return false; return false;

View file

@ -2,31 +2,22 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file. // found in the LICENSE-CHROMIUM file.
#ifndef BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_ #ifndef BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_
#define BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_ #define BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_
#include "base/memory/singleton.h"
#include "content/public/browser/platform_notification_service.h" #include "content/public/browser/platform_notification_service.h"
namespace brightray { namespace brightray {
class NotificationPresenter; class BrowserClient;
class PlatformNotificationServiceImpl class PlatformNotificationService
: public content::PlatformNotificationService { : public content::PlatformNotificationService {
public: public:
// Returns the active instance of the service in the browser process. Safe to explicit PlatformNotificationService(BrowserClient* browser_client);
// be called from any thread. ~PlatformNotificationService() override;
static PlatformNotificationServiceImpl* GetInstance();
NotificationPresenter* notification_presenter();
private:
friend struct base::DefaultSingletonTraits<PlatformNotificationServiceImpl>;
PlatformNotificationServiceImpl();
~PlatformNotificationServiceImpl() override;
protected:
// content::PlatformNotificationService: // content::PlatformNotificationService:
blink::WebNotificationPermission CheckPermissionOnUIThread( blink::WebNotificationPermission CheckPermissionOnUIThread(
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
@ -56,11 +47,12 @@ class PlatformNotificationServiceImpl
content::BrowserContext* browser_context, content::BrowserContext* browser_context,
std::set<std::string>* displayed_notifications) override; std::set<std::string>* displayed_notifications) override;
scoped_ptr<NotificationPresenter> notification_presenter_; private:
BrowserClient* browser_client_;
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationServiceImpl); DISALLOW_COPY_AND_ASSIGN(PlatformNotificationService);
}; };
} // namespace brightray } // namespace brightray
#endif // BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_ #endif // BROWSER_PLATFORM_NOTIFICATION_SERVICE_H_

View file

@ -64,8 +64,8 @@
'browser/notification.h', 'browser/notification.h',
'browser/permission_manager.cc', 'browser/permission_manager.cc',
'browser/permission_manager.h', 'browser/permission_manager.h',
'browser/platform_notification_service_impl.cc', 'browser/platform_notification_service.cc',
'browser/platform_notification_service_impl.h', 'browser/platform_notification_service.h',
'browser/linux/libnotify_loader.h', 'browser/linux/libnotify_loader.h',
'browser/linux/libnotify_loader.cc', 'browser/linux/libnotify_loader.cc',
'browser/linux/notification_presenter_linux.h', 'browser/linux/notification_presenter_linux.h',