Skip to content

Commit be36dfe

Browse files
committed
[Notification] rebase for m41 beta
[Linux] (+2 squashed commits) Squashed commits: [fee6d9d] [Notification][WIN] rebase, fix toastStatics created by different threads, optimize the notification image, if the url is file scheme [b104a2d] [Notification] OSX rebase
1 parent 26e9b71 commit be36dfe

13 files changed

+157
-134
lines changed

nw.gypi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,16 @@
380380
'src/renderer/shell_render_process_observer.h',
381381
'src/nw_shell.cc',
382382
'src/nw_shell.h',
383-
# 'src/nw_notification_manager.h',
384-
# 'src/nw_notification_manager.cc',
385-
# 'src/nw_notification_manager_win.h',
386-
# 'src/nw_notification_manager_win.cc',
387-
# 'src/nw_notification_manager_toast_win.h',
388-
# 'src/nw_notification_manager_toast_win.cc',
389-
# 'src/nw_notification_manager_mac.h',
390-
# 'src/nw_notification_manager_mac.mm',
391-
# 'src/nw_notification_manager_linux.h',
392-
# 'src/nw_notification_manager_linux.cc',
383+
'src/nw_notification_manager.h',
384+
'src/nw_notification_manager.cc',
385+
'src/nw_notification_manager_win.h',
386+
'src/nw_notification_manager_win.cc',
387+
'src/nw_notification_manager_toast_win.h',
388+
'src/nw_notification_manager_toast_win.cc',
389+
'src/nw_notification_manager_mac.h',
390+
'src/nw_notification_manager_mac.mm',
391+
'src/nw_notification_manager_linux.h',
392+
'src/nw_notification_manager_linux.cc',
393393
'src/shell_browser_context.cc',
394394
'src/shell_browser_context.h',
395395
'src/shell_browser_main.cc',

src/nw_notification_manager.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1919

2020
#include "ui/gfx/image/image.h"
21+
#include "content/public/browser/desktop_notification_delegate.h"
2122
#include "content/public/browser/render_process_host.h"
2223
#include "content/common/platform_notification_messages.h"
2324

@@ -97,4 +98,45 @@ bool NotificationManager::DesktopNotificationPostError(int render_process_id, in
9798
#endif
9899
return true;
99100
}
101+
102+
blink::WebNotificationPermission NotificationManager::CheckPermission(ResourceContext* resource_context,
103+
const GURL& origin,
104+
int render_process_id) {
105+
return blink::WebNotificationPermissionAllowed;
106+
}
107+
108+
void CancelDesktopNotification(int notification_id) {
109+
nw::NotificationManager *notificationManager = nw::NotificationManager::getSingleton();
110+
if (notificationManager == NULL) {
111+
NOTIMPLEMENTED();
112+
return;
113+
}
114+
notificationManager->CancelDesktopNotification(notification_id);
115+
}
116+
117+
void NotificationManager::DisplayNotification(BrowserContext* browser_context,
118+
const GURL& origin,
119+
const SkBitmap& icon,
120+
const PlatformNotificationData& notification_data,
121+
scoped_ptr<DesktopNotificationDelegate> delegate,
122+
int render_process_id,
123+
base::Closure* cancel_callback) {
124+
if (AddDesktopNotification(notification_data, render_process_id, delegate->notification_id(), icon))
125+
*cancel_callback = base::Bind(&nw::CancelDesktopNotification, delegate->notification_id());
126+
}
127+
128+
void NotificationManager::DisplayPersistentNotification(BrowserContext* browser_context,
129+
int64 service_worker_registration_id,
130+
const GURL& origin,
131+
const SkBitmap& icon,
132+
const PlatformNotificationData& notification_data,
133+
int render_process_id) {
134+
NOTIMPLEMENTED();
135+
}
136+
137+
void NotificationManager::ClosePersistentNotification(BrowserContext* browser_context,
138+
const std::string& persistent_notification_id) {
139+
NOTIMPLEMENTED();
140+
}
141+
100142
} // namespace nw

src/nw_notification_manager.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,58 @@
2121
#define CONTENT_NW_NOTIFICATION_MANAGER_H_
2222

2323
#include "base/basictypes.h"
24-
#include "content/public/common/platform_notification_data.h"
24+
#include "content/public/browser/platform_notification_service.h"
2525

2626
namespace nw {
2727

28-
class NotificationManager{
28+
using namespace content;
29+
30+
class NotificationManager : public content::PlatformNotificationService{
2931
private:
3032
static NotificationManager *singleton_;
3133

3234
protected:
3335
explicit NotificationManager();
3436

3537
public:
36-
virtual ~NotificationManager();
38+
~NotificationManager() override;
3739
static NotificationManager* getSingleton();
38-
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
40+
41+
virtual bool AddDesktopNotification(const content::PlatformNotificationData& params,
3942
const int render_process_id,
4043
const int notification_id,
41-
const bool worker) = 0;
44+
const SkBitmap& icon) = 0;
4245

43-
virtual bool CancelDesktopNotification(int render_process_id, int notification_id) = 0;
46+
virtual bool CancelDesktopNotification(int notification_id) = 0;
4447

4548
bool DesktopNotificationPostClick(int render_process_id, int notification_id);
4649
bool DesktopNotificationPostClose(int render_process_id, int notification_id, bool by_user);
4750
bool DesktopNotificationPostDisplay(int render_process_id, int notification_id);
4851
bool DesktopNotificationPostError(int render_process_id, int notification_id, const base::string16& message);
52+
53+
// PlatformNotificationService functions
54+
blink::WebNotificationPermission CheckPermission(ResourceContext* resource_context,
55+
const GURL& origin,
56+
int render_process_id) override;
57+
58+
void DisplayNotification(BrowserContext* browser_context,
59+
const GURL& origin,
60+
const SkBitmap& icon,
61+
const PlatformNotificationData& notification_data,
62+
scoped_ptr<DesktopNotificationDelegate> delegate,
63+
int render_process_id,
64+
base::Closure* cancel_callback) override;
65+
66+
void DisplayPersistentNotification(BrowserContext* browser_context,
67+
int64 service_worker_registration_id,
68+
const GURL& origin,
69+
const SkBitmap& icon,
70+
const PlatformNotificationData& notification_data,
71+
int render_process_id) override;
72+
73+
void ClosePersistentNotification(BrowserContext* browser_context,
74+
const std::string& persistent_notification_id) override;
75+
4976

5077
};
5178

src/nw_notification_manager_linux.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "content/public/browser/web_contents.h"
2424
#include "content/public/browser/render_frame_host.h"
2525
#include "content/public/browser/render_view_host.h"
26+
#include "content/public/common/platform_notification_data.h"
2627
#include "content/nw/src/browser/native_window.h"
2728
#include "content/nw/src/nw_package.h"
2829
#include "content/nw/src/nw_shell.h"
@@ -69,14 +70,14 @@ void NotificationManagerLinux::onClose(NotifyNotification *notif)
6970
g_object_unref(G_OBJECT(notif));
7071
};
7172

72-
bool NotificationManagerLinux::AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
73+
bool NotificationManagerLinux::AddDesktopNotification(const content::PlatformNotificationData& params,
7374
const int render_process_id,
7475
const int notification_id,
75-
const bool worker) {
76+
const SkBitmap& icon) {
7677
content::Shell* shell = content::Shell::windows()[0];
7778
SkBitmap bitmap;
78-
if(params.icon.getSize()) {
79-
bitmap = params.icon;
79+
if(icon.getSize()) {
80+
bitmap = icon;
8081
} else {
8182
bitmap = shell->window()->app_icon().AsBitmap();
8283
}
@@ -126,7 +127,7 @@ bool NotificationManagerLinux::AddDesktopNotification(const content::ShowDesktop
126127
return error==NULL;
127128
}
128129

129-
bool NotificationManagerLinux::CancelDesktopNotification(int render_process_id, int notification_id) {
130+
bool NotificationManagerLinux::CancelDesktopNotification(int notification_id) {
130131
NotificationMap::const_iterator i = getNotification(notification_id);
131132
if (i!=mNotificationIDmap.end()) {
132133
return notify_notification_close(i->second.mNotification, NULL);

src/nw_notification_manager_linux.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class NotificationManagerLinux : public NotificationManager {
2929
struct NotificationData {
3030
NotifyNotification* mNotification;
3131
int mRenderProcessId;
32-
int mRenderViewId;
3332
};
3433

3534
typedef std::map<int, NotificationData> NotificationMap;
@@ -42,12 +41,12 @@ class NotificationManagerLinux : public NotificationManager {
4241

4342
public:
4443
explicit NotificationManagerLinux();
45-
virtual ~NotificationManagerLinux();
46-
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
44+
~NotificationManagerLinux() override;
45+
bool AddDesktopNotification(const content::PlatformNotificationData& params,
4746
const int render_process_id,
4847
const int notification_id,
49-
const bool worker) override;
50-
virtual bool CancelDesktopNotification(int render_process_id, int notification_id) override;
48+
const SkBitmap& icon) override;
49+
bool CancelDesktopNotification(int notification_id) override;
5150
};
5251

5352
} // namespace nw

src/nw_notification_manager_mac.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
namespace nw {
2626

2727
class NotificationManagerMac : public NotificationManager {
28-
2928
public:
3029
explicit NotificationManagerMac();
3130
virtual ~NotificationManagerMac(){}
3231

33-
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
34-
const int render_process_id, const int notification_id, const bool worker) override;
32+
virtual bool AddDesktopNotification(const content::PlatformNotificationData& params,
33+
const int render_process_id, const int notification_id, const SkBitmap& icon) override;
3534

36-
virtual bool CancelDesktopNotification(int render_process_id, int notification_id) override;
35+
virtual bool CancelDesktopNotification(int notification_id) override;
3736

3837
};
3938

src/nw_notification_manager_mac.mm

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "content/public/browser/web_contents.h"
2525
#include "content/public/browser/render_frame_host.h"
2626
#include "content/public/browser/render_view_host.h"
27+
#include "content/public/common/platform_notification_data.h"
2728
#include "content/nw/src/browser/native_window.h"
2829
#include "content/nw/src/nw_package.h"
2930
#include "content/nw/src/nw_shell.h"
@@ -80,21 +81,19 @@ -(void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNoti
8081
namespace nw {
8182
NotificationManagerMac::NotificationManagerMac() {
8283
}
83-
84-
bool NotificationManagerMac::AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
85-
const int render_process_id, const int notification_id, const bool worker) {
86-
84+
85+
bool NotificationManagerMac::AddDesktopNotification(const content::PlatformNotificationData &params,
86+
const int render_process_id, const int notification_id, const SkBitmap& icon) {
87+
8788
NSUserNotification *notification = [[NSUserNotification alloc] init];
8889
[notification setTitle : base::SysUTF16ToNSString(params.title)];
8990
[notification setInformativeText : base::SysUTF16ToNSString(params.body)];
9091
notification.hasActionButton = YES;
91-
92-
if (params.icon.getSize() > 0 && base::mac::IsOSMavericksOrLater()) {
93-
// try to get the notification icon image given by image download callback
94-
gfx::Image icon = gfx::Image::CreateFrom1xBitmap(params.icon);
95-
92+
93+
if (base::mac::IsOSMavericksOrLater() && icon.getSize()) {
94+
gfx::Image image = gfx::Image::CreateFrom1xBitmap(icon);
9695
// this function only runs on Mavericks or later
97-
[notification setContentImage : icon.ToNSImage()];
96+
[notification setContentImage : image.ToNSImage()];
9897
}
9998

10099
notification.userInfo = @{ @"render_process_id" :[NSNumber numberWithInt : render_process_id],
@@ -111,7 +110,7 @@ -(void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNoti
111110
return true;
112111
}
113112

114-
bool NotificationManagerMac::CancelDesktopNotification(int render_process_id, int notification_id){
113+
bool NotificationManagerMac::CancelDesktopNotification(int notification_id){
115114
for (NSUserNotification *notification in[[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications]) {
116115
NSNumber *current_notification_id = [notification.userInfo objectForKey : @"notification_id"];
117116
if (current_notification_id.intValue == notification_id){

src/nw_notification_manager_toast_win.cc

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "content/public/browser/web_contents.h"
2323
#include "content/public/browser/render_frame_host.h"
2424
#include "content/public/browser/render_view_host.h"
25+
#include "content/public/common/platform_notification_data.h"
2526
#include "content/nw/src/browser/native_window.h"
2627
#include "content/nw/src/nw_package.h"
2728
#include "content/nw/src/nw_shell.h"
@@ -174,7 +175,7 @@ IFACEMETHODIMP ToastEventHandler::Invoke(_In_ IToastNotification* /* sender */,
174175
BOOL succeeded = nw::NotificationManager::getSingleton()->DesktopNotificationPostClose(_render_process_id, _notification_id, tdr == ToastDismissalReason_UserCanceled);
175176
hr = succeeded ? S_OK : E_FAIL;
176177
}
177-
nw::NotificationManager::getSingleton()->CancelDesktopNotification(_render_process_id, _notification_id);
178+
nw::NotificationManager::getSingleton()->CancelDesktopNotification(_notification_id);
178179
return hr;
179180
}
180181

@@ -269,17 +270,21 @@ HRESULT NotificationManagerToastWin::SetImageSrc(_In_z_ const wchar_t *imagePath
269270
}
270271

271272
HRESULT NotificationManagerToastWin::CreateToastXml(_In_ IToastNotificationManagerStatics *toastManager,
272-
const content::ShowDesktopNotificationHostMsgParams& params, _Outptr_ IXmlDocument** inputXml) {
273-
bool bImage = params.icon.getSize() > 0;
273+
const content::PlatformNotificationData& params, const SkBitmap& icon, _Outptr_ IXmlDocument** inputXml) {
274+
bool bImage = icon.getSize() > 0;
274275
char tempFileName[MAX_PATH];
275276

276-
if (bImage) {
277+
if (params.icon.SchemeIsFile()) {
278+
strcpy_s(tempFileName, params.icon.spec().data());
279+
}
280+
else if (bImage) {
281+
277282
char temp[MAX_PATH];
278283
GetTempPathA(MAX_PATH, tempFileName);
279284
GetTempFileNameA(tempFileName, "NTF", 0, temp);
280285

281286
Vector<char> encodedImage;
282-
bImage = blink::PNGImageEncoder::encode(params.icon, reinterpret_cast<Vector<unsigned char>*>(&encodedImage));
287+
bImage = blink::PNGImageEncoder::encode(icon, reinterpret_cast<Vector<unsigned char>*>(&encodedImage));
283288

284289
FILE *f = fopen(temp, "wb");
285290
fwrite(encodedImage.data(), sizeof(char), encodedImage.size(), f);
@@ -347,6 +352,15 @@ bool NotificationManagerToastWin::IsSupported() {
347352
}
348353

349354
NotificationManagerToastWin::NotificationManagerToastWin() {
355+
Init();
356+
}
357+
358+
bool NotificationManagerToastWin::Init() {
359+
if (!content::BrowserThread::CurrentlyOn(BrowserThread::UI))
360+
return false;
361+
362+
DCHECK(toastStatics_.Get() == NULL);
363+
350364
HRESULT hr = GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &toastStatics_);
351365
if (SUCCEEDED(hr)) {
352366
base::string16 appID;
@@ -355,16 +369,20 @@ NotificationManagerToastWin::NotificationManagerToastWin() {
355369

356370
HRESULT hr = toastStatics_->CreateToastNotifierWithId(StringReferenceWrapper(appID.c_str(), appID.length()).Get(), &notifier_);
357371
}
372+
return SUCCEEDED(hr);
358373
}
359374

360375
NotificationManagerToastWin::~NotificationManagerToastWin() {
361376
}
362377

363-
bool NotificationManagerToastWin::AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
364-
const int render_process_id, const int notification_id, const bool worker) {
378+
bool NotificationManagerToastWin::AddDesktopNotification(const content::PlatformNotificationData& params,
379+
const int render_process_id, const int notification_id, const SkBitmap& icon) {
380+
381+
if (toastStatics_ == NULL)
382+
if (!Init()) return false;
365383

366384
ComPtr<IXmlDocument> toastXml;
367-
HRESULT hr = CreateToastXml(toastStatics_.Get(), params, &toastXml);
385+
HRESULT hr = CreateToastXml(toastStatics_.Get(), params, icon, &toastXml);
368386
if (SUCCEEDED(hr)) {
369387
hr = CreateToast(toastStatics_.Get(), toastXml.Get(), render_process_id, notification_id);
370388
if (SUCCEEDED(hr))
@@ -374,7 +392,7 @@ bool NotificationManagerToastWin::AddDesktopNotification(const content::ShowDesk
374392
return SUCCEEDED(hr);
375393
}
376394

377-
bool NotificationManagerToastWin::CancelDesktopNotification(int render_process_id, int notification_id) {
395+
bool NotificationManagerToastWin::CancelDesktopNotification(int notification_id) {
378396
std::map<int, ComPtr<IToastNotification>>::iterator i = notification_map_.find(notification_id);
379397
if (i == notification_map_.end())
380398
return false;

src/nw_notification_manager_toast_win.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class NotificationManagerToastWin : public NotificationManager {
4343

4444
// Create the toast XML from a template
4545
HRESULT CreateToastXml(_In_ IToastNotificationManagerStatics *toastManager,
46-
const content::ShowDesktopNotificationHostMsgParams& params, _Outptr_ IXmlDocument** inputXml);
46+
const content::PlatformNotificationData& params, const SkBitmap& icon, _Outptr_ IXmlDocument** inputXml);
4747

4848
// Set the value of the "src" attribute of the "image" node
4949
HRESULT SetImageSrc(_In_z_ const wchar_t *imagePath, _In_ IXmlDocument *toastXml);
@@ -54,13 +54,15 @@ class NotificationManagerToastWin : public NotificationManager {
5454

5555
HRESULT SetNodeValueString(_In_ HSTRING inputString, _In_ IXmlNode *node, _In_ IXmlDocument *xml);
5656

57+
bool Init();
58+
5759
public:
5860
static bool IsSupported();
5961
explicit NotificationManagerToastWin();
6062
virtual ~NotificationManagerToastWin();
61-
virtual bool AddDesktopNotification(const content::ShowDesktopNotificationHostMsgParams& params,
62-
const int render_process_id, const int notification_id, const bool worker) override;
63-
virtual bool CancelDesktopNotification(int render_process_id, int notification_id) override;
63+
virtual bool AddDesktopNotification(const content::PlatformNotificationData& params,
64+
const int render_process_id, const int notification_id, const SkBitmap& icon) override;
65+
virtual bool CancelDesktopNotification(int notification_id) override;
6466
};
6567

6668
} // namespace nw

0 commit comments

Comments
 (0)