• 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 
33 #if ENABLE(WORKERS)
34 #include "V8Worker.h"
35 
36 #include "Worker.h"
37 
38 #include "ExceptionCode.h"
39 #include "Frame.h"
40 #include "SerializedScriptValue.h"
41 #include "V8Binding.h"
42 #include "V8MessagePortCustom.h"
43 #include "V8Proxy.h"
44 #include "V8Utilities.h"
45 #include "WorkerContext.h"
46 #include "WorkerContextExecutionProxy.h"
47 
48 namespace WebCore {
49 
constructorCallback(const v8::Arguments & args)50 v8::Handle<v8::Value> V8Worker::constructorCallback(const v8::Arguments& args)
51 {
52     INC_STATS(L"DOM.Worker.Constructor");
53 
54     if (!args.IsConstructCall())
55         return throwError("DOM object constructor cannot be called as a function.");
56 
57     if (!args.Length())
58         return throwError("Not enough arguments", V8Proxy::SyntaxError);
59 
60     v8::TryCatch tryCatch;
61     v8::Handle<v8::String> scriptUrl = args[0]->ToString();
62     if (tryCatch.HasCaught())
63         return throwError(tryCatch.Exception());
64 
65     if (scriptUrl.IsEmpty())
66         return v8::Undefined();
67 
68     // Get the script execution context.
69     ScriptExecutionContext* context = getScriptExecutionContext();
70     if (!context)
71         return v8::Undefined();
72 
73     // Create the worker object.
74     // Note: it's OK to let this RefPtr go out of scope because we also call setDOMWrapper(), which effectively holds a reference to obj.
75     ExceptionCode ec = 0;
76     RefPtr<Worker> obj = Worker::create(toWebCoreString(scriptUrl), context, ec);
77     if (ec)
78         return throwError(ec);
79 
80     // Setup the standard wrapper object internal fields.
81     v8::Handle<v8::Object> wrapperObject = args.Holder();
82     V8DOMWrapper::setDOMWrapper(wrapperObject, &info, obj.get());
83 
84     obj->ref();
85     V8DOMWrapper::setJSWrapperForActiveDOMObject(obj.get(), v8::Persistent<v8::Object>::New(wrapperObject));
86 
87     return wrapperObject;
88 }
89 
postMessageCallback(const v8::Arguments & args)90 v8::Handle<v8::Value> V8Worker::postMessageCallback(const v8::Arguments& args)
91 {
92     INC_STATS("DOM.Worker.postMessage");
93     Worker* worker = V8Worker::toNative(args.Holder());
94     bool didThrow = false;
95     RefPtr<SerializedScriptValue> message = SerializedScriptValue::create(args[0], didThrow);
96     if (didThrow)
97         return v8::Undefined();
98     MessagePortArray portArray;
99     if (args.Length() > 1) {
100         if (!getMessagePortArray(args[1], portArray))
101             return v8::Undefined();
102     }
103     ExceptionCode ec = 0;
104     worker->postMessage(message.release(), &portArray, ec);
105     return throwError(ec);
106 }
107 
108 } // namespace WebCore
109 
110 #endif // ENABLE(WORKERS)
111