Home | History | Annotate | Download | only in runner
      1 /*
      2  * Copyright (C) 2010 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 "NotificationPresenter.h"
     32 
     33 #include "public/platform/Platform.h"
     34 #include "public/platform/WebString.h"
     35 #include "public/platform/WebURL.h"
     36 #include "public/testing/WebTestDelegate.h"
     37 #include "public/web/WebKit.h"
     38 #include "public/web/WebNotificationPermissionCallback.h"
     39 #include "public/web/WebSecurityOrigin.h"
     40 #include <url/gurl.h>
     41 
     42 using namespace blink;
     43 using namespace std;
     44 
     45 namespace WebTestRunner {
     46 
     47 namespace {
     48 
     49 WebString identifierForNotification(const WebNotification& notification)
     50 {
     51     if (notification.isHTML())
     52         return notification.url().spec().utf16();
     53     return notification.title();
     54 }
     55 
     56 void deferredDisplayDispatch(void* context)
     57 {
     58     WebNotification* notification = static_cast<WebNotification*>(context);
     59     notification->dispatchDisplayEvent();
     60     delete notification;
     61 }
     62 
     63 }
     64 
     65 NotificationPresenter::NotificationPresenter()
     66     : m_delegate(0)
     67 {
     68 }
     69 
     70 NotificationPresenter::~NotificationPresenter()
     71 {
     72 }
     73 
     74 void NotificationPresenter::grantPermission(const WebString& origin)
     75 {
     76     m_allowedOrigins.insert(origin.utf8());
     77 }
     78 
     79 bool NotificationPresenter::simulateClick(const WebString& title)
     80 {
     81     string id(title.utf8());
     82     if (m_activeNotifications.find(id) == m_activeNotifications.end())
     83         return false;
     84 
     85     const WebNotification& notification = m_activeNotifications.find(id)->second;
     86     WebNotification eventTarget(notification);
     87     eventTarget.dispatchClickEvent();
     88     return true;
     89 }
     90 
     91 void NotificationPresenter::cancelAllActiveNotifications()
     92 {
     93     while (!m_activeNotifications.empty()) {
     94         const WebNotification& notification = m_activeNotifications.begin()->second;
     95         cancel(notification);
     96     }
     97 }
     98 
     99 // The output from all these methods matches what DumpRenderTree produces.
    100 bool NotificationPresenter::show(const WebNotification& notification)
    101 {
    102     WebString identifier = identifierForNotification(notification);
    103     if (!notification.replaceId().isEmpty()) {
    104         string replaceId(notification.replaceId().utf8());
    105         if (m_replacements.find(replaceId) != m_replacements.end())
    106             m_delegate->printMessage(string("REPLACING NOTIFICATION ") + m_replacements.find(replaceId)->second + "\n");
    107 
    108         m_replacements[replaceId] = identifier.utf8();
    109     }
    110 
    111     if (notification.isHTML())
    112         m_delegate->printMessage(string("DESKTOP NOTIFICATION: contents at ") + string(notification.url().spec()) + "\n");
    113     else {
    114         m_delegate->printMessage("DESKTOP NOTIFICATION:");
    115         m_delegate->printMessage(notification.direction() == WebTextDirectionRightToLeft ? "(RTL)" : "");
    116         m_delegate->printMessage(" icon ");
    117         m_delegate->printMessage(notification.iconURL().isEmpty() ? "" : notification.iconURL().spec().data());
    118         m_delegate->printMessage(", title ");
    119         m_delegate->printMessage(notification.title().isEmpty() ? "" : notification.title().utf8().data());
    120         m_delegate->printMessage(", text ");
    121         m_delegate->printMessage(notification.body().isEmpty() ? "" : notification.body().utf8().data());
    122         m_delegate->printMessage("\n");
    123     }
    124 
    125     string id(identifier.utf8());
    126     m_activeNotifications[id] = notification;
    127 
    128     Platform::current()->callOnMainThread(deferredDisplayDispatch, new WebNotification(notification));
    129     return true;
    130 }
    131 
    132 void NotificationPresenter::cancel(const WebNotification& notification)
    133 {
    134     WebString identifier = identifierForNotification(notification);
    135     m_delegate->printMessage(string("DESKTOP NOTIFICATION CLOSED: ") + string(identifier.utf8()) + "\n");
    136     WebNotification eventTarget(notification);
    137     eventTarget.dispatchCloseEvent(false);
    138 
    139     string id(identifier.utf8());
    140     m_activeNotifications.erase(id);
    141 }
    142 
    143 void NotificationPresenter::objectDestroyed(const blink::WebNotification& notification)
    144 {
    145     WebString identifier = identifierForNotification(notification);
    146     string id(identifier.utf8());
    147     m_activeNotifications.erase(id);
    148 }
    149 
    150 WebNotificationPresenter::Permission NotificationPresenter::checkPermission(const WebSecurityOrigin& origin)
    151 {
    152     // Check with the layout test controller
    153     WebString originString = origin.toString();
    154     bool allowed = m_allowedOrigins.find(string(originString.utf8())) != m_allowedOrigins.end();
    155     return allowed ? WebNotificationPresenter::PermissionAllowed
    156         : WebNotificationPresenter::PermissionDenied;
    157 }
    158 
    159 void NotificationPresenter::requestPermission(
    160     const WebSecurityOrigin& origin,
    161     WebNotificationPermissionCallback* callback)
    162 {
    163     m_delegate->printMessage("DESKTOP NOTIFICATION PERMISSION REQUESTED: " + string(origin.toString().utf8()) + "\n");
    164     callback->permissionRequestComplete();
    165 }
    166 
    167 }
    168