1/*
2 * Copyright (C) 2011 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(MUTATION_OBSERVERS)
34
35#include "V8WebKitMutationObserver.h"
36
37#include "MutationObserverOptions.h"
38#include "OptionsObject.h"
39#include "V8Binding.h"
40#include "V8BindingMacros.h"
41#include "V8DOMWrapper.h"
42#include "V8MutationCallback.h"
43#include "V8Node.h"
44#include "V8Proxy.h"
45#include "V8Utilities.h"
46
47namespace WebCore {
48
49v8::Handle<v8::Value> V8WebKitMutationObserver::constructorCallback(const v8::Arguments& args)
50{
51 INC_STATS("DOM.WebKitMutationObserver.Constructor");
52
53 if (!args.IsConstructCall())
54 return throwError("DOM object constructor cannot be called as a function.", V8Proxy::TypeError);
55
56 if (args.Length() < 1)
57 return throwError("Not enough arguments", V8Proxy::TypeError);
58
59 v8::Local<v8::Value> arg = args[0];
60 if (!arg->IsObject())
61 return throwError("Argument must be an Object", V8Proxy::TypeError);
62
63 ScriptExecutionContext* context = getScriptExecutionContext();
64 if (!context)
65 return throwError("WebKitMutationObserver constructor's associated frame unavailable", V8Proxy::ReferenceError);
66
67 RefPtr<MutationCallback> callback = V8MutationCallback::create(arg, context);
68 RefPtr<WebKitMutationObserver> observer = WebKitMutationObserver::create(callback.release());
69
70 V8DOMWrapper::setDOMWrapper(args.Holder(), &info, observer.get());
71 observer->ref();
72 V8DOMWrapper::setJSWrapperForDOMObject(observer.get(), v8::Persistent<v8::Object>::New(args.Holder()));
73 return args.Holder();
74}
75
76v8::Handle<v8::Value> V8WebKitMutationObserver::observeCallback(const v8::Arguments& args)
77{
78 INC_STATS("DOM.WebKitMutationObserver.observe");
79 if (args.Length() < 2)
80 return throwError("Not enough arguments", V8Proxy::TypeError);
81 WebKitMutationObserver* imp = V8WebKitMutationObserver::toNative(args.Holder());
82 EXCEPTION_BLOCK(Node*, target, V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0);
83
84 if (!args[1]->IsObject())
85 return throwError("Options must be an object", V8Proxy::TypeError);
86
87 OptionsObject optionsObject(args[1]);
88 RefPtr<MutationObserverOptions> options = MutationObserverOptions::create();
89
90#define SET_OPTION(option, setter) \
91 do { \
92 bool option; \
93 if (optionsObject.getKeyValue(#option, option)) \
94 options->setter(option); \
95 } while (0)
96
97 SET_OPTION(childList, setChildList);
98 SET_OPTION(attributes, setAttributes);
99 SET_OPTION(characterData, setCharacterData);
100 SET_OPTION(subtree, setSubtree);
101 SET_OPTION(attributeOldValue, setAttributeOldValue);
102 SET_OPTION(characterDataOldValue, setCharacterDataOldValue);
103
104#undef SET_OPTION
105
106 imp->observe(target, options.get());
107 return v8::Handle<v8::Value>();
108}
109
110} // namespace WebCore
111
112#endif // ENABLE(MUTATION_OBSERVERS)