OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2008, 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(APPLICATION_CACHE) |
| 35 |
| 36 #include "V8Binding.h" |
| 37 #include "V8Document.h" |
| 38 #include "V8CustomBinding.h" |
| 39 #include "V8ObjectEventListener.h" |
| 40 #include "V8Proxy.h" |
| 41 #include "V8Utilities.h" |
| 42 #include "WorkerContextExecutionProxy.h" |
| 43 |
| 44 namespace WebCore { |
| 45 |
| 46 static const bool kFindOnly = true; |
| 47 static const bool kFindOrCreate = false; |
| 48 |
| 49 static PassRefPtr<EventListener> argumentToEventListener(DOMApplicationCache* ap
pcache, v8::Local<v8::Value> value, bool findOnly) |
| 50 { |
| 51 #if 0 && ENABLE(WORKERS) |
| 52 // FIXME: this is not tested yet |
| 53 WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProx
y::retrieve(); |
| 54 if (workerContextProxy) |
| 55 return workerContextProxy->findOrCreateObjectEventListener(value, false,
findOnly); |
| 56 #endif |
| 57 |
| 58 V8Proxy* proxy = V8Proxy::retrieve(appcache->scriptExecutionContext()); |
| 59 if (proxy) |
| 60 return findOnly ? proxy->FindObjectEventListener(value, false) |
| 61 : proxy->FindOrCreateObjectEventListener(value, false); |
| 62 return 0; |
| 63 } |
| 64 |
| 65 static v8::Local<v8::Object> eventListenerToV8Object(EventListener* listener) |
| 66 { |
| 67 return (static_cast<V8ObjectEventListener*>(listener))->getListenerObject(); |
| 68 } |
| 69 |
| 70 static inline ApplicationCacheEventType toEventType(v8::Local<v8::String> value) |
| 71 { |
| 72 String key = toWebCoreString(value); |
| 73 ASSERT(key.startsWith("on")); |
| 74 return DOMApplicationCache::toEventType(key.substring(2)); |
| 75 } |
| 76 |
| 77 // Handles appcache.onfooevent attribute getting |
| 78 ACCESSOR_GETTER(DOMApplicationCacheEventHandler) |
| 79 { |
| 80 INC_STATS("DOMApplicationCache.onevent_getter"); |
| 81 DOMApplicationCache* appcache = V8Proxy::ToNativeObject<DOMApplicationCache>
(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder()); |
| 82 EventListener* listener = appcache->getAttributeEventListener(toEventType(na
me)); |
| 83 return eventListenerToV8Object(listener); |
| 84 } |
| 85 |
| 86 // Handles appcache.onfooevent attribute setting |
| 87 ACCESSOR_SETTER(DOMApplicationCacheEventHandler) |
| 88 { |
| 89 INC_STATS("DOMApplicationCache.onevent_setter"); |
| 90 DOMApplicationCache* appcache = V8Proxy::ToNativeObject<DOMApplicationCache>
(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder()); |
| 91 ApplicationCacheEventType eventType = toEventType(name); |
| 92 |
| 93 if (EventListener* oldListener = appcache->getAttributeEventListener(eventTy
pe)) { |
| 94 v8::Local<v8::Object> object = eventListenerToV8Object(oldListener); |
| 95 removeHiddenDependency(info.Holder(), object, V8Custom::kDOMApplicationC
acheCacheIndex); |
| 96 appcache->clearAttributeEventListener(eventType); |
| 97 } |
| 98 |
| 99 if (value->IsFunction()) { |
| 100 RefPtr<EventListener> newListener = argumentToEventListener(appcache, va
lue, kFindOrCreate); |
| 101 if (newListener) { |
| 102 createHiddenDependency(info.Holder(), value, V8Custom::kDOMApplicati
onCacheCacheIndex); |
| 103 appcache->setAttributeEventListener(eventType, newListener); |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 // Handles appcache.addEventListner(name, func, capture) method calls |
| 109 CALLBACK_FUNC_DECL(DOMApplicationCacheAddEventListener) |
| 110 { |
| 111 INC_STATS("DOMApplicationCache.addEventListener()"); |
| 112 DOMApplicationCache* appcache = V8Proxy::ToNativeObject<DOMApplicationCache>
(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder()); |
| 113 |
| 114 RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1],
kFindOrCreate); |
| 115 if (listener) { |
| 116 createHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplication
CacheCacheIndex); |
| 117 String eventType = toWebCoreString(args[0]); |
| 118 bool useCapture = args[2]->BooleanValue(); |
| 119 appcache->addEventListener(eventType, listener, useCapture); |
| 120 } |
| 121 return v8::Undefined(); |
| 122 } |
| 123 |
| 124 // Handles appcache.removeEventListner(name, func, capture) method calls |
| 125 CALLBACK_FUNC_DECL(DOMApplicationCacheRemoveEventListener) |
| 126 { |
| 127 INC_STATS("DOMApplicationCache.removeEventListener()"); |
| 128 DOMApplicationCache* appcache = V8Proxy::ToNativeObject<DOMApplicationCache>
(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder()); |
| 129 |
| 130 RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1],
kFindOnly); |
| 131 if (listener) { |
| 132 removeHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplication
CacheCacheIndex); |
| 133 String eventType = toWebCoreString(args[0]); |
| 134 bool useCapture = args[2]->BooleanValue(); |
| 135 appcache->removeEventListener(eventType, listener.get(), useCapture); |
| 136 } |
| 137 return v8::Undefined(); |
| 138 } |
| 139 |
| 140 } // namespace WebCore |
| 141 |
| 142 #endif // ENABLE(APPLICATION_CACHE) |
OLD | NEW |