2015-11-04 18:13:52 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
2017-03-23 19:48:22 +00:00
|
|
|
// Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and
|
|
|
|
// Jason Poon <jason.poon@microsoft.com>. All rights reserved.
|
2015-11-04 18:13:52 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-CHROMIUM file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/notifications/win/notification_presenter_win.h"
|
2015-11-10 12:07:12 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
2017-05-18 22:06:57 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-12-14 00:31:02 +00:00
|
|
|
#include "base/environment.h"
|
2015-12-24 12:03:54 +00:00
|
|
|
#include "base/files/file_util.h"
|
2019-04-20 17:20:37 +00:00
|
|
|
#include "base/hash/md5.h"
|
2020-07-14 01:13:34 +00:00
|
|
|
#include "base/logging.h"
|
2015-12-24 12:03:54 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-01-03 08:32:18 +00:00
|
|
|
#include "base/time/time.h"
|
2015-11-08 03:41:29 +00:00
|
|
|
#include "base/win/windows_version.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/notifications/win/windows_toast_notification.h"
|
2022-11-17 19:59:23 +00:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2015-11-08 03:41:29 +00:00
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
2015-12-24 12:03:54 +00:00
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
2015-11-08 03:41:29 +00:00
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
#pragma comment(lib, "runtimeobject.lib")
|
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
namespace electron {
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-11-10 12:23:08 +00:00
|
|
|
namespace {
|
|
|
|
|
2017-12-14 00:25:49 +00:00
|
|
|
bool IsDebuggingNotifications() {
|
|
|
|
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
|
|
|
|
}
|
|
|
|
|
2015-12-24 12:03:54 +00: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 12:23:08 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
// static
|
|
|
|
NotificationPresenter* NotificationPresenter::Create() {
|
2015-12-24 12:03:54 +00:00
|
|
|
if (!WindowsToastNotification::Initialize())
|
|
|
|
return nullptr;
|
2021-06-08 02:00:05 +00:00
|
|
|
auto presenter = std::make_unique<NotificationPresenterWin>();
|
2015-12-24 12:03:54 +00:00
|
|
|
if (!presenter->Init())
|
2015-11-24 06:40:58 +00:00
|
|
|
return nullptr;
|
2017-12-14 00:31:02 +00:00
|
|
|
|
|
|
|
if (IsDebuggingNotifications())
|
2017-12-14 00:25:49 +00:00
|
|
|
LOG(INFO) << "Successfully created Windows notifications presenter";
|
|
|
|
|
2015-12-24 12:03:54 +00:00
|
|
|
return presenter.release();
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
NotificationPresenterWin::NotificationPresenterWin() = default;
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2021-06-04 04:16:13 +00:00
|
|
|
NotificationPresenterWin::~NotificationPresenterWin() = default;
|
2015-11-04 18:13:52 +00:00
|
|
|
|
2015-12-24 12:03:54 +00:00
|
|
|
bool NotificationPresenterWin::Init() {
|
2022-11-17 19:59:23 +00:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2015-12-24 12:03:54 +00:00
|
|
|
return temp_dir_.CreateUniqueTempDir();
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:55:51 +00:00
|
|
|
std::wstring NotificationPresenterWin::SaveIconToFilesystem(
|
2018-04-18 01:56:12 +00:00
|
|
|
const SkBitmap& icon,
|
|
|
|
const GURL& origin) {
|
2017-09-25 20:51:16 +00: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";
|
|
|
|
}
|
|
|
|
|
2022-11-17 19:59:23 +00:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2021-03-18 19:55:51 +00:00
|
|
|
base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToWide(filename));
|
2015-12-24 12:03:54 +00:00
|
|
|
if (base::PathExists(path))
|
|
|
|
return path.value();
|
|
|
|
if (SaveIconToPath(icon, path))
|
|
|
|
return path.value();
|
2021-03-18 19:55:51 +00:00
|
|
|
return base::UTF8ToWide(origin.spec());
|
2015-12-24 12:03:54 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 08:38:21 +00:00
|
|
|
Notification* NotificationPresenterWin::CreateNotificationObject(
|
|
|
|
NotificationDelegate* delegate) {
|
2017-01-10 15:34:41 +00:00
|
|
|
return new WindowsToastNotification(delegate, this);
|
|
|
|
}
|
|
|
|
|
2018-10-17 18:01:11 +00:00
|
|
|
} // namespace electron
|