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