Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "DRTDesktopNotificationPresenter.h"
     33 
     34 #include "DumpRenderTree.h"
     35 #include "LayoutTestController.h"
     36 #include <JavaScriptCore/JSStringRef.h>
     37 #include <JavaScriptCore/JSStringRefBSTR.h>
     38 #include <WebCore/NotificationPresenter.h>
     39 
     40 DRTDesktopNotificationPresenter::DRTDesktopNotificationPresenter()
     41     : m_refCount(1) {}
     42 
     43 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::QueryInterface(REFIID riid, void** ppvObject)
     44 {
     45     *ppvObject = 0;
     46     if (IsEqualGUID(riid, IID_IUnknown))
     47         *ppvObject = static_cast<DRTDesktopNotificationPresenter*>(this);
     48     else if (IsEqualGUID(riid, IID_IWebDesktopNotificationsDelegate))
     49         *ppvObject = static_cast<DRTDesktopNotificationPresenter*>(this);
     50     else
     51         return E_NOINTERFACE;
     52 
     53     AddRef();
     54     return S_OK;
     55 }
     56 
     57 ULONG STDMETHODCALLTYPE DRTDesktopNotificationPresenter::AddRef()
     58 {
     59     return ++m_refCount;
     60 }
     61 
     62 ULONG STDMETHODCALLTYPE DRTDesktopNotificationPresenter::Release()
     63 {
     64     ULONG newRef = --m_refCount;
     65     if (!newRef)
     66         delete(this);
     67 
     68     return newRef;
     69 }
     70 
     71 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::showDesktopNotification(
     72     /* [in] */ IWebDesktopNotification* notification)
     73 {
     74     BSTR title, text, url;
     75     BOOL html;
     76 
     77     if (!notification->isHTML(&html) && html) {
     78         notification->contentsURL(&url);
     79         printf("DESKTOP NOTIFICATION: contents at %S\n", url ? url : L"");
     80     } else {
     81         notification->iconURL(&url);
     82         notification->title(&title);
     83         notification->text(&text);
     84         printf("DESKTOP NOTIFICATION: icon %S, title %S, text %S\n",
     85             url ? url : L"",
     86             title ? title : L"",
     87             text ? text : L"");
     88     }
     89 
     90     // In this stub implementation, the notification is displayed immediately;
     91     // we dispatch the display event to mimic that.
     92     notification->notifyDisplay();
     93 
     94     return S_OK;
     95 }
     96 
     97 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::cancelDesktopNotification(
     98     /* [in] */ IWebDesktopNotification* notification)
     99 {
    100     BSTR identifier;
    101     BOOL html;
    102     notification->isHTML(&html);
    103     if (html)
    104         notification->contentsURL(&identifier);
    105     else
    106         notification->title(&identifier);
    107 
    108     printf("DESKTOP NOTIFICATION CLOSED: %S\n", identifier ? identifier : L"");
    109     notification->notifyClose(false);
    110 
    111     return S_OK;
    112 }
    113 
    114 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::notificationDestroyed(
    115     /* [in] */ IWebDesktopNotification* notification)
    116 {
    117     // Since in these tests events happen immediately, we don't hold on to
    118     // Notification pointers.  So there's no cleanup to do.
    119     return S_OK;
    120 }
    121 
    122 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::checkNotificationPermission(
    123         /* [in] */ BSTR origin,
    124         /* [out, retval] */ int* result)
    125 {
    126 #if ENABLE(NOTIFICATIONS)
    127     JSStringRef jsOrigin = JSStringCreateWithBSTR(origin);
    128     bool allowed = ::gLayoutTestController->checkDesktopNotificationPermission(jsOrigin);
    129 
    130     if (allowed)
    131         *result = WebCore::NotificationPresenter::PermissionAllowed;
    132     else
    133         *result = WebCore::NotificationPresenter::PermissionDenied;
    134 
    135     JSStringRelease(jsOrigin);
    136 #endif
    137     return S_OK;
    138 }
    139 
    140 HRESULT STDMETHODCALLTYPE DRTDesktopNotificationPresenter::requestNotificationPermission(
    141         /* [in] */ BSTR origin)
    142 {
    143     printf("DESKTOP NOTIFICATION PERMISSION REQUESTED: %S\n", origin ? origin : L"");
    144     return S_OK;
    145 }
    146