Implement PlatformNotificationService

This commit is contained in:
Cheng Zhao 2015-03-08 19:37:13 -07:00
parent 99e2dbd6e8
commit a5026907e4
8 changed files with 140 additions and 36 deletions

View file

@ -71,6 +71,8 @@
'browser/notification_presenter.h', 'browser/notification_presenter.h',
'browser/notification_presenter_mac.h', 'browser/notification_presenter_mac.h',
'browser/notification_presenter_mac.mm', 'browser/notification_presenter_mac.mm',
'browser/platform_notification_service_impl.cc',
'browser/platform_notification_service_impl.h',
'browser/linux/notification_presenter_linux.h', 'browser/linux/notification_presenter_linux.h',
'browser/linux/notification_presenter_linux.cc', 'browser/linux/notification_presenter_linux.cc',
'browser/remote_debugging_server.cc', 'browser/remote_debugging_server.cc',

View file

@ -8,7 +8,7 @@
#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/notification_presenter.h" #include "browser/platform_notification_service_impl.h"
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/path_service.h" #include "base/path_service.h"
@ -39,14 +39,6 @@ BrowserContext* BrowserClient::browser_context() {
return browser_main_parts_->browser_context(); return browser_main_parts_->browser_context();
} }
NotificationPresenter* BrowserClient::notification_presenter() {
#if defined(OS_MACOSX) || defined(OS_LINUX)
if (!notification_presenter_)
notification_presenter_.reset(NotificationPresenter::Create());
#endif
return notification_presenter_.get();
}
BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts( BrowserMainParts* BrowserClient::OverrideCreateBrowserMainParts(
const content::MainFunctionParams&) { const content::MainFunctionParams&) {
return new BrowserMainParts; return new BrowserMainParts;
@ -67,21 +59,14 @@ net::URLRequestContextGetter* BrowserClient::CreateRequestContext(
return context->CreateRequestContext(protocol_handlers, protocol_interceptors.Pass()); return context->CreateRequestContext(protocol_handlers, protocol_interceptors.Pass());
} }
void BrowserClient::ShowDesktopNotification(
const content::ShowDesktopNotificationHostMsgParams& params,
content::BrowserContext* browser_context,
int render_process_id,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) {
auto presenter = notification_presenter();
if (presenter)
presenter->ShowNotification(params, delegate.Pass(), cancel_callback);
}
content::MediaObserver* BrowserClient::GetMediaObserver() { content::MediaObserver* BrowserClient::GetMediaObserver() {
return MediaCaptureDevicesDispatcher::GetInstance(); return MediaCaptureDevicesDispatcher::GetInstance();
} }
content::PlatformNotificationService* BrowserClient::GetPlatformNotificationService() {
return PlatformNotificationServiceImpl::GetInstance();
}
void BrowserClient::GetAdditionalAllowedSchemesForFileSystem( void BrowserClient::GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_schemes) { std::vector<std::string>* additional_schemes) {
additional_schemes->push_back(content::kChromeDevToolsScheme); additional_schemes->push_back(content::kChromeDevToolsScheme);

View file

@ -11,7 +11,6 @@ namespace brightray {
class BrowserContext; class BrowserContext;
class BrowserMainParts; class BrowserMainParts;
class NotificationPresenter;
class BrowserClient : public content::ContentBrowserClient { class BrowserClient : public content::ContentBrowserClient {
public: public:
@ -22,7 +21,6 @@ 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* notification_presenter();
protected: protected:
// Subclasses should override this to provide their own BrowserMainParts // Subclasses should override this to provide their own BrowserMainParts
@ -41,20 +39,14 @@ class BrowserClient : public content::ContentBrowserClient {
private: private:
content::BrowserMainParts* CreateBrowserMainParts( content::BrowserMainParts* CreateBrowserMainParts(
const content::MainFunctionParams&) override; const content::MainFunctionParams&) override;
void ShowDesktopNotification(
const content::ShowDesktopNotificationHostMsgParams& params,
content::BrowserContext* browser_context,
int render_process_id,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) override;
content::MediaObserver* GetMediaObserver() override; content::MediaObserver* GetMediaObserver() override;
content::PlatformNotificationService* GetPlatformNotificationService() override;
void GetAdditionalAllowedSchemesForFileSystem( void GetAdditionalAllowedSchemesForFileSystem(
std::vector<std::string>* additional_schemes) override; std::vector<std::string>* additional_schemes) override;
base::FilePath GetDefaultDownloadDirectory() override; base::FilePath GetDefaultDownloadDirectory() override;
content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override; content::DevToolsManagerDelegate* GetDevToolsManagerDelegate() override;
BrowserMainParts* browser_main_parts_; BrowserMainParts* browser_main_parts_;
scoped_ptr<NotificationPresenter> notification_presenter_;
DISALLOW_COPY_AND_ASSIGN(BrowserClient); DISALLOW_COPY_AND_ASSIGN(BrowserClient);
}; };

View file

@ -6,7 +6,7 @@
namespace content { namespace content {
class DesktopNotificationDelegate; class DesktopNotificationDelegate;
struct ShowDesktopNotificationHostMsgParams; struct PlatformNotificationData;
} }
namespace brightray { namespace brightray {
@ -18,7 +18,7 @@ class NotificationPresenter {
static NotificationPresenter* Create(); static NotificationPresenter* Create();
virtual void ShowNotification( virtual void ShowNotification(
const content::ShowDesktopNotificationHostMsgParams&, const content::PlatformNotificationData&,
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) = 0; base::Closure* cancel_callback) = 0;
}; };

View file

@ -21,7 +21,7 @@ class NotificationPresenterMac : public NotificationPresenter {
~NotificationPresenterMac(); ~NotificationPresenterMac();
virtual void ShowNotification( virtual void ShowNotification(
const content::ShowDesktopNotificationHostMsgParams&, const content::PlatformNotificationData&,
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) override; base::Closure* cancel_callback) override;

View file

@ -8,8 +8,8 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "content/public/common/platform_notification_data.h"
#include "content/public/browser/desktop_notification_delegate.h" #include "content/public/browser/desktop_notification_delegate.h"
#include "content/public/common/show_desktop_notification_params.h"
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@ -40,12 +40,12 @@ NotificationPresenterMac::~NotificationPresenterMac() {
} }
void NotificationPresenterMac::ShowNotification( void NotificationPresenterMac::ShowNotification(
const content::ShowDesktopNotificationHostMsgParams& params, const content::PlatformNotificationData& data,
scoped_ptr<content::DesktopNotificationDelegate> delegate, scoped_ptr<content::DesktopNotificationDelegate> delegate,
base::Closure* cancel_callback) { base::Closure* cancel_callback) {
auto notification = [[NSUserNotification alloc] init]; auto notification = [[NSUserNotification alloc] init];
notification.title = base::SysUTF16ToNSString(params.title); notification.title = base::SysUTF16ToNSString(data.title);
notification.informativeText = base::SysUTF16ToNSString(params.body); notification.informativeText = base::SysUTF16ToNSString(data.body);
notifications_map_[delegate.get()].reset(notification); notifications_map_[delegate.get()].reset(notification);
[NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification]; [NSUserNotificationCenter.defaultUserNotificationCenter deliverNotification:notification];

View file

@ -0,0 +1,64 @@
// Copyright (c) 2015 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-CHROMIUM file.
#include "browser/platform_notification_service_impl.h"
#include "browser/notification_presenter.h"
#include "content/public/browser/desktop_notification_delegate.h"
namespace brightray {
// static
PlatformNotificationServiceImpl*
PlatformNotificationServiceImpl::GetInstance() {
return Singleton<PlatformNotificationServiceImpl>::get();
}
PlatformNotificationServiceImpl::PlatformNotificationServiceImpl() {}
PlatformNotificationServiceImpl::~PlatformNotificationServiceImpl() {}
NotificationPresenter* PlatformNotificationServiceImpl::notification_presenter() {
#if defined(OS_MACOSX) || defined(OS_LINUX)
if (!notification_presenter_)
notification_presenter_.reset(NotificationPresenter::Create());
#endif
return notification_presenter_.get();
}
blink::WebNotificationPermission PlatformNotificationServiceImpl::CheckPermission(
content::ResourceContext* resource_context,
const GURL& origin,
int render_process_id) {
return blink::WebNotificationPermissionAllowed;
}
void PlatformNotificationServiceImpl::DisplayNotification(
content::BrowserContext* browser_context,
const GURL& origin,
const SkBitmap& icon,
const content::PlatformNotificationData& notification_data,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
int render_process_id,
base::Closure* cancel_callback) {
auto presenter = notification_presenter();
if (presenter)
presenter->ShowNotification(notification_data, delegate.Pass(), cancel_callback);
}
void PlatformNotificationServiceImpl::DisplayPersistentNotification(
content::BrowserContext* browser_context,
int64 service_worker_registration_id,
const GURL& origin,
const SkBitmap& icon,
const content::PlatformNotificationData& notification_data,
int render_process_id) {
}
void PlatformNotificationServiceImpl::ClosePersistentNotification(
content::BrowserContext* browser_context,
const std::string& persistent_notification_id) {
}
} // namespace brightray

View file

@ -0,0 +1,61 @@
// Copyright (c) 2015 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-CHROMIUM file.
#ifndef BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_
#define BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_
#include "base/memory/singleton.h"
#include "content/public/browser/platform_notification_service.h"
namespace brightray {
class NotificationPresenter;
class PlatformNotificationServiceImpl
: public content::PlatformNotificationService {
public:
// Returns the active instance of the service in the browser process. Safe to
// be called from any thread.
static PlatformNotificationServiceImpl* GetInstance();
NotificationPresenter* notification_presenter();
private:
friend struct DefaultSingletonTraits<PlatformNotificationServiceImpl>;
PlatformNotificationServiceImpl();
~PlatformNotificationServiceImpl() override;
// content::PlatformNotificationService:
virtual blink::WebNotificationPermission CheckPermission(
content::ResourceContext* resource_context,
const GURL& origin,
int render_process_id) override;
virtual void DisplayNotification(
content::BrowserContext* browser_context,
const GURL& origin,
const SkBitmap& icon,
const content::PlatformNotificationData& notification_data,
scoped_ptr<content::DesktopNotificationDelegate> delegate,
int render_process_id,
base::Closure* cancel_callback) override;
virtual void DisplayPersistentNotification(
content::BrowserContext* browser_context,
int64 service_worker_registration_id,
const GURL& origin,
const SkBitmap& icon,
const content::PlatformNotificationData& notification_data,
int render_process_id) override;
virtual void ClosePersistentNotification(
content::BrowserContext* browser_context,
const std::string& persistent_notification_id) override;
scoped_ptr<NotificationPresenter> notification_presenter_;
DISALLOW_COPY_AND_ASSIGN(PlatformNotificationServiceImpl);
};
} // namespace brightray
#endif // BROWSER_PLATFORM_NOTIFICATION_SERVICE_IMPL_H_