desktop_notification_service_win.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/notifications/desktop_notification_service.h"
6
7#include "base/logging.h"
8#include "base/win/metro.h"
9#include "chrome/browser/notifications/notification.h"
10#include "chrome/browser/notifications/notification_object_proxy.h"
11#include "chrome/browser/notifications/notification_ui_manager.h"
12#include "chrome/browser/notifications/notification_ui_manager.h"
13
14typedef void (*MetroDisplayNotification)(
15    const char* origin_url, const char* icon_url, const wchar_t* title,
16    const wchar_t* body, const wchar_t* display_source,
17    const char* notification_id);
18
19typedef bool (*MetroCancelNotification)(const char* notification_id);
20
21bool DesktopNotificationService::CancelDesktopNotification(
22    int process_id, int route_id, int notification_id) {
23  scoped_refptr<NotificationObjectProxy> proxy(
24      new NotificationObjectProxy(process_id, route_id, notification_id,
25                                  false));
26  if (base::win::IsMetroProcess()) {
27    MetroCancelNotification cancel_metro_notification =
28        reinterpret_cast<MetroCancelNotification>(GetProcAddress(
29            base::win::GetMetroModule(), "CancelNotification"));
30    DCHECK(cancel_metro_notification);
31    if (cancel_metro_notification(proxy->id().c_str()))
32      return true;
33  }
34  return GetUIManager()->CancelById(proxy->id());
35}
36
37void DesktopNotificationService::ShowNotification(
38    const Notification& notification) {
39  if (base::win::IsMetroProcess()) {
40    MetroDisplayNotification display_metro_notification =
41        reinterpret_cast<MetroDisplayNotification>(GetProcAddress(
42            base::win::GetMetroModule(), "DisplayNotification"));
43    DCHECK(display_metro_notification);
44    if (!notification.is_html()) {
45      display_metro_notification(notification.origin_url().spec().c_str(),
46                                 notification.content_url().spec().c_str(),
47                                 notification.title().c_str(),
48                                 notification.body().c_str(),
49                                 notification.display_source().c_str(),
50                                 notification.notification_id().c_str());
51      return;
52    } else {
53      NOTREACHED() << "We don't support HTML notifications in Windows 8 metro";
54    }
55  }
56  GetUIManager()->Add(notification, profile_);
57}
58