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