• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "DOMApplicationCache.h"
33 
34 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
35 
36 #include "ApplicationCacheHost.h"
37 #include "V8Binding.h"
38 #include "V8CustomBinding.h"
39 #include "V8Document.h"
40 #include "V8ObjectEventListener.h"
41 #include "V8Proxy.h"
42 #include "V8Utilities.h"
43 #include "WorkerContextExecutionProxy.h"
44 
45 namespace WebCore {
46 
47 static const bool kFindOnly = true;
48 static const bool kFindOrCreate = false;
49 
argumentToEventListener(DOMApplicationCache * appcache,v8::Local<v8::Value> value,bool findOnly)50 static PassRefPtr<EventListener> argumentToEventListener(DOMApplicationCache* appcache, v8::Local<v8::Value> value, bool findOnly)
51 {
52     V8Proxy* proxy = V8Proxy::retrieve(appcache->scriptExecutionContext());
53     if (proxy)
54         return findOnly ? proxy->objectListeners()->findWrapper(value, false)
55                         : proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false);
56     return 0;
57 }
58 
eventListenerToV8Object(EventListener * listener)59 static v8::Local<v8::Object> eventListenerToV8Object(EventListener* listener)
60 {
61     return (static_cast<V8ObjectEventListener*>(listener))->getListenerObject();
62 }
63 
toEventID(v8::Local<v8::String> value)64 static inline ApplicationCacheHost::EventID toEventID(v8::Local<v8::String> value)
65 {
66     String key = toWebCoreString(value);
67     ASSERT(key.startsWith("on"));
68     return DOMApplicationCache::toEventID(key.substring(2));
69 }
70 
71 // Handles appcache.onfooevent attribute getting
ACCESSOR_GETTER(DOMApplicationCacheEventHandler)72 ACCESSOR_GETTER(DOMApplicationCacheEventHandler)
73 {
74     INC_STATS("DOMApplicationCache.onevent_getter");
75     DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder());
76     EventListener* listener = appcache->getAttributeEventListener(toEventID(name));
77     if (!listener)
78         return v8::Null();
79     return eventListenerToV8Object(listener);
80 }
81 
82 // Handles appcache.onfooevent attribute setting
ACCESSOR_SETTER(DOMApplicationCacheEventHandler)83 ACCESSOR_SETTER(DOMApplicationCacheEventHandler)
84 {
85     INC_STATS("DOMApplicationCache.onevent_setter");
86     DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder());
87     ApplicationCacheHost::EventID eventType = toEventID(name);
88 
89     if (EventListener* oldListener = appcache->getAttributeEventListener(eventType)) {
90         v8::Local<v8::Object> object = eventListenerToV8Object(oldListener);
91         removeHiddenDependency(info.Holder(), object, V8Custom::kDOMApplicationCacheCacheIndex);
92         appcache->clearAttributeEventListener(eventType);
93     }
94 
95     if (value->IsFunction()) {
96         RefPtr<EventListener> newListener = argumentToEventListener(appcache, value, kFindOrCreate);
97         if (newListener) {
98             createHiddenDependency(info.Holder(), value, V8Custom::kDOMApplicationCacheCacheIndex);
99             appcache->setAttributeEventListener(eventType, newListener);
100         }
101     }
102 }
103 
104 // Handles appcache.addEventListner(name, func, capture) method calls
CALLBACK_FUNC_DECL(DOMApplicationCacheAddEventListener)105 CALLBACK_FUNC_DECL(DOMApplicationCacheAddEventListener)
106 {
107     INC_STATS("DOMApplicationCache.addEventListener()");
108     DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder());
109 
110     RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1], kFindOrCreate);
111     if (listener) {
112         createHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplicationCacheCacheIndex);
113         String eventType = toWebCoreString(args[0]);
114         bool useCapture = args[2]->BooleanValue();
115         appcache->addEventListener(eventType, listener, useCapture);
116     }
117     return v8::Undefined();
118 }
119 
120 // Handles appcache.removeEventListner(name, func, capture) method calls
CALLBACK_FUNC_DECL(DOMApplicationCacheRemoveEventListener)121 CALLBACK_FUNC_DECL(DOMApplicationCacheRemoveEventListener)
122 {
123     INC_STATS("DOMApplicationCache.removeEventListener()");
124     DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder());
125 
126     RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1], kFindOnly);
127     if (listener) {
128         removeHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplicationCacheCacheIndex);
129         String eventType = toWebCoreString(args[0]);
130         bool useCapture = args[2]->BooleanValue();
131         appcache->removeEventListener(eventType, listener.get(), useCapture);
132     }
133     return v8::Undefined();
134 }
135 
136 } // namespace WebCore
137 
138 #endif  // ENABLE(OFFLINE_WEB_APPLICATIONS)
139