| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | // Copyright (c) 2012 The Chromium Authors. All rights reserved.
 | 
					
						
							| 
									
										
										
										
											2017-03-23 12:48:22 -07:00
										 |  |  | // Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and
 | 
					
						
							|  |  |  | // Jason Poon <jason.poon@microsoft.com>. All rights reserved.
 | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | // Use of this source code is governed by a BSD-style license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE-CHROMIUM file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | #include "atom/browser/notifications/win/notification_presenter_win.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-10 20:07:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 19:25:56 -05:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2017-05-18 15:06:57 -07:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | #include "atom/browser/notifications/win/notification_presenter_win7.h"
 | 
					
						
							|  |  |  | #include "atom/browser/notifications/win/windows_toast_notification.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-13 16:31:02 -08:00
										 |  |  | #include "base/environment.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  | #include "base/files/file_util.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-20 13:20:37 -04:00
										 |  |  | #include "base/hash/md5.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  | #include "base/strings/utf_string_conversions.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-03 17:25:19 +09:00
										 |  |  | #include "base/threading/thread_restrictions.h"
 | 
					
						
							| 
									
										
										
										
											2018-01-03 17:32:18 +09:00
										 |  |  | #include "base/time/time.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-07 19:41:29 -08:00
										 |  |  | #include "base/win/windows_version.h"
 | 
					
						
							|  |  |  | #include "third_party/skia/include/core/SkBitmap.h"
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  | #include "ui/gfx/codec/png_codec.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-07 19:41:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | #pragma comment(lib, "runtimeobject.lib")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | namespace atom { | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 20:23:08 +08:00
										 |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-13 16:25:49 -08:00
										 |  |  | bool IsDebuggingNotifications() { | 
					
						
							|  |  |  |   return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  | bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) { | 
					
						
							|  |  |  |   std::vector<unsigned char> png_data; | 
					
						
							|  |  |  |   if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data)) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   char* data = reinterpret_cast<char*>(&png_data[0]); | 
					
						
							|  |  |  |   int size = static_cast<int>(png_data.size()); | 
					
						
							|  |  |  |   return base::WriteFile(path, data, size) == size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-10 20:23:08 +08:00
										 |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | NotificationPresenter* NotificationPresenter::Create() { | 
					
						
							| 
									
										
										
										
											2017-03-15 13:58:53 +01:00
										 |  |  |   auto version = base::win::GetVersion(); | 
					
						
							| 
									
										
										
										
											2019-04-30 20:18:22 -04:00
										 |  |  |   if (version < base::win::Version::WIN8) | 
					
						
							| 
									
										
										
										
											2017-03-15 13:58:53 +01:00
										 |  |  |     return new NotificationPresenterWin7; | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  |   if (!WindowsToastNotification::Initialize()) | 
					
						
							|  |  |  |     return nullptr; | 
					
						
							| 
									
										
										
										
											2017-03-23 15:47:30 -07:00
										 |  |  |   std::unique_ptr<NotificationPresenterWin> presenter( | 
					
						
							|  |  |  |       new NotificationPresenterWin); | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  |   if (!presenter->Init()) | 
					
						
							| 
									
										
										
										
											2015-11-24 14:40:58 +08:00
										 |  |  |     return nullptr; | 
					
						
							| 
									
										
										
										
											2017-12-13 16:31:02 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (IsDebuggingNotifications()) | 
					
						
							| 
									
										
										
										
											2017-12-13 16:25:49 -08:00
										 |  |  |     LOG(INFO) << "Successfully created Windows notifications presenter"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  |   return presenter.release(); | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:56:12 -04:00
										 |  |  | NotificationPresenterWin::NotificationPresenterWin() {} | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:56:12 -04:00
										 |  |  | NotificationPresenterWin::~NotificationPresenterWin() {} | 
					
						
							| 
									
										
										
										
											2015-11-04 10:13:52 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  | bool NotificationPresenterWin::Init() { | 
					
						
							| 
									
										
										
										
											2018-01-03 17:25:19 +09:00
										 |  |  |   base::ThreadRestrictions::ScopedAllowIO allow_io; | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  |   return temp_dir_.CreateUniqueTempDir(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-25 11:05:48 +08:00
										 |  |  | base::string16 NotificationPresenterWin::SaveIconToFilesystem( | 
					
						
							| 
									
										
										
										
											2018-04-17 21:56:12 -04:00
										 |  |  |     const SkBitmap& icon, | 
					
						
							|  |  |  |     const GURL& origin) { | 
					
						
							| 
									
										
										
										
											2017-09-25 13:51:16 -07:00
										 |  |  |   std::string filename; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (origin.is_valid()) { | 
					
						
							|  |  |  |     filename = base::MD5String(origin.spec()) + ".png"; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     base::TimeTicks now = base::TimeTicks::Now(); | 
					
						
							|  |  |  |     filename = std::to_string(now.ToInternalValue()) + ".png"; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 17:25:19 +09:00
										 |  |  |   base::ThreadRestrictions::ScopedAllowIO allow_io; | 
					
						
							| 
									
										
										
										
											2017-01-26 16:09:45 +09:00
										 |  |  |   base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename)); | 
					
						
							| 
									
										
										
										
											2015-12-24 20:03:54 +08:00
										 |  |  |   if (base::PathExists(path)) | 
					
						
							|  |  |  |     return path.value(); | 
					
						
							|  |  |  |   if (SaveIconToPath(icon, path)) | 
					
						
							|  |  |  |     return path.value(); | 
					
						
							|  |  |  |   return base::UTF8ToUTF16(origin.spec()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 10:38:21 +02:00
										 |  |  | Notification* NotificationPresenterWin::CreateNotificationObject( | 
					
						
							|  |  |  |     NotificationDelegate* delegate) { | 
					
						
							| 
									
										
										
										
											2017-01-10 16:34:41 +01:00
										 |  |  |   return new WindowsToastNotification(delegate, this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-17 20:01:11 +02:00
										 |  |  | }  // namespace atom
 |