V8NotificationCenterCustom.cpp revision 2fc2651226baac27029e38c9d6ef883fa32084db
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
33#if ENABLE(NOTIFICATIONS)
34#include "V8NotificationCenter.h"
35
36#include "NotImplemented.h"
37#include "Notification.h"
38#include "NotificationCenter.h"
39#include "V8Binding.h"
40#include "V8CustomVoidCallback.h"
41#include "V8EventListener.h"
42#include "V8Notification.h"
43#include "V8Proxy.h"
44#include "V8Utilities.h"
45#include "WorkerContext.h"
46
47namespace WebCore {
48
49v8::Handle<v8::Value> V8NotificationCenter::createHTMLNotificationCallback(const v8::Arguments& args)
50{
51    INC_STATS(L"DOM.NotificationCenter.CreateHTMLNotification()");
52    NotificationCenter* notificationCenter = V8NotificationCenter::toNative(args.Holder());
53
54    ExceptionCode ec = 0;
55    String url = toWebCoreString(args[0]);
56    RefPtr<Notification> notification = notificationCenter->createHTMLNotification(url, ec);
57
58    if (ec)
59        return throwError(ec);
60
61    notification->ref();
62    return toV8(notification.get());
63}
64
65v8::Handle<v8::Value> V8NotificationCenter::createNotificationCallback(const v8::Arguments& args)
66{
67    INC_STATS(L"DOM.NotificationCenter.CreateNotification()");
68    NotificationCenter* notificationCenter = V8NotificationCenter::toNative(args.Holder());
69
70    ExceptionCode ec = 0;
71    RefPtr<Notification> notification = notificationCenter->createNotification(toWebCoreString(args[0]), toWebCoreString(args[1]), toWebCoreString(args[2]), ec);
72
73    if (ec)
74        return throwError(ec);
75
76    notification->ref();
77    return toV8(notification.get());
78}
79
80v8::Handle<v8::Value> V8NotificationCenter::requestPermissionCallback(const v8::Arguments& args)
81{
82    INC_STATS(L"DOM.NotificationCenter.RequestPermission()");
83    NotificationCenter* notificationCenter = V8NotificationCenter::toNative(args.Holder());
84    ScriptExecutionContext* context = notificationCenter->scriptExecutionContext();
85
86    // Make sure that script execution context is valid.
87    if (!context)
88        return throwError(INVALID_STATE_ERR);
89
90    // Requesting permission is only valid from a page context.
91    if (context->isWorkerContext())
92        return throwError(NOT_SUPPORTED_ERR);
93
94    RefPtr<V8CustomVoidCallback> callback;
95    if (args.Length() > 0) {
96        if (!args[0]->IsObject())
97            return throwError("Callback must be of valid type.", V8Proxy::TypeError);
98
99        callback = V8CustomVoidCallback::create(args[0], context);
100    }
101
102    notificationCenter->requestPermission(callback.release());
103    return v8::Undefined();
104}
105
106
107}  // namespace WebCore
108
109#endif  // ENABLE(NOTIFICATIONS)
110