2015-11-04 18:13:52 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
2015-11-08 06:11:15 +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.
|
|
|
|
|
|
|
|
#include "browser/win/notification_presenter_win.h"
|
2015-11-10 12:07:12 +00:00
|
|
|
|
2015-12-24 12:03:54 +00:00
|
|
|
#include "base/files/file_util.h"
|
|
|
|
#include "base/md5.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-11-08 03:41:29 +00:00
|
|
|
#include "base/win/windows_version.h"
|
2015-11-10 12:07:12 +00:00
|
|
|
#include "browser/win/windows_toast_notification.h"
|
2015-11-04 18:13:52 +00:00
|
|
|
#include "content/public/browser/desktop_notification_delegate.h"
|
|
|
|
#include "content/public/common/platform_notification_data.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")
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
2015-11-10 12:23:08 +00:00
|
|
|
namespace {
|
|
|
|
|
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
|
|
|
void RemoveNotification(base::WeakPtr<WindowsToastNotification> notification) {
|
|
|
|
if (notification)
|
|
|
|
notification->DismissNotification();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // 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;
|
|
|
|
scoped_ptr<NotificationPresenterWin> presenter(new NotificationPresenterWin);
|
|
|
|
if (!presenter->Init())
|
2015-11-24 06:40:58 +00:00
|
|
|
return nullptr;
|
2015-12-24 12:03:54 +00:00
|
|
|
return presenter.release();
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPresenterWin::NotificationPresenterWin() {
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationPresenterWin::~NotificationPresenterWin() {
|
|
|
|
}
|
|
|
|
|
2015-12-24 12:03:54 +00:00
|
|
|
bool NotificationPresenterWin::Init() {
|
|
|
|
return temp_dir_.CreateUniqueTempDir();
|
|
|
|
}
|
|
|
|
|
2015-11-04 18:13:52 +00:00
|
|
|
void NotificationPresenterWin::ShowNotification(
|
2015-11-10 10:27:18 +00:00
|
|
|
const content::PlatformNotificationData& data,
|
|
|
|
const SkBitmap& icon,
|
2015-11-10 11:50:38 +00:00
|
|
|
scoped_ptr<content::DesktopNotificationDelegate> delegate,
|
2015-11-10 10:27:18 +00:00
|
|
|
base::Closure* cancel_callback) {
|
2015-11-10 12:23:08 +00:00
|
|
|
// This class manages itself.
|
2015-11-20 05:28:37 +00:00
|
|
|
auto notification = new WindowsToastNotification(delegate.Pass());
|
2015-12-24 12:03:54 +00:00
|
|
|
notification->ShowNotification(
|
|
|
|
data.title, data.body, SaveIconToFilesystem(icon, data.icon));
|
2015-11-10 10:27:18 +00:00
|
|
|
|
2015-11-08 06:11:15 +00:00
|
|
|
if (cancel_callback) {
|
|
|
|
*cancel_callback = base::Bind(
|
2015-11-10 12:23:08 +00:00
|
|
|
&RemoveNotification, notification->GetWeakPtr());
|
2015-11-08 06:11:15 +00:00
|
|
|
}
|
2015-11-04 18:13:52 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 12:03:54 +00:00
|
|
|
std::wstring NotificationPresenterWin::SaveIconToFilesystem(
|
|
|
|
const SkBitmap& icon, const GURL& origin) {
|
|
|
|
std::string filename = base::MD5String(origin.spec()) + ".png";
|
|
|
|
base::FilePath path = temp_dir_.path().Append(base::UTF8ToUTF16(filename));
|
|
|
|
if (base::PathExists(path))
|
|
|
|
return path.value();
|
|
|
|
if (SaveIconToPath(icon, path))
|
|
|
|
return path.value();
|
|
|
|
return base::UTF8ToUTF16(origin.spec());
|
|
|
|
}
|
|
|
|
|
2015-11-10 10:27:18 +00:00
|
|
|
} // namespace brightray
|