electron/brightray/browser/win/notification_presenter_win.cc

102 lines
3.1 KiB
C++
Raw Normal View History

// 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.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "brightray/browser/win/notification_presenter_win.h"
2015-11-10 12:07:12 +00:00
#include <memory>
#include <string>
#include <vector>
2017-12-14 00:31:02 +00:00
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/md5.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
2018-01-03 08:32:18 +00:00
#include "base/time/time.h"
#include "base/win/windows_version.h"
#include "brightray/browser/win/notification_presenter_win7.h"
#include "brightray/browser/win/windows_toast_notification.h"
#include "content/public/common/platform_notification_data.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#pragma comment(lib, "runtimeobject.lib")
namespace brightray {
2015-11-10 12:23:08 +00:00
namespace {
bool IsDebuggingNotifications() {
return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
}
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
// static
NotificationPresenter* NotificationPresenter::Create() {
auto version = base::win::GetVersion();
if (version < base::win::VERSION_WIN8)
return new NotificationPresenterWin7;
if (!WindowsToastNotification::Initialize())
return nullptr;
std::unique_ptr<NotificationPresenterWin> presenter(
new NotificationPresenterWin);
if (!presenter->Init())
return nullptr;
2017-12-14 00:31:02 +00:00
if (IsDebuggingNotifications())
LOG(INFO) << "Successfully created Windows notifications presenter";
return presenter.release();
}
2018-04-18 01:56:12 +00:00
NotificationPresenterWin::NotificationPresenterWin() {}
2018-04-18 01:56:12 +00:00
NotificationPresenterWin::~NotificationPresenterWin() {}
bool NotificationPresenterWin::Init() {
base::ThreadRestrictions::ScopedAllowIO allow_io;
return temp_dir_.CreateUniqueTempDir();
}
2015-12-25 03:05:48 +00:00
base::string16 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";
}
base::ThreadRestrictions::ScopedAllowIO allow_io;
2017-01-26 07:09:45 +00:00
base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
if (base::PathExists(path))
return path.value();
if (SaveIconToPath(icon, path))
return path.value();
return base::UTF8ToUTF16(origin.spec());
}
Notification* NotificationPresenterWin::CreateNotificationObject(
NotificationDelegate* delegate) {
return new WindowsToastNotification(delegate, this);
}
} // namespace brightray