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(WEB_SOCKETS) 34 35 #include "V8WebSocket.h" 36 37 #include "Frame.h" 38 #include "Settings.h" 39 #include "V8Binding.h" 40 #include "V8Proxy.h" 41 #include "V8Utilities.h" 42 #include "WebSocket.h" 43 #include "WorkerContext.h" 44 #include "WorkerContextExecutionProxy.h" 45 46 namespace WebCore { 47 48 v8::Handle<v8::Value> V8WebSocket::constructorCallback(const v8::Arguments& args) 49 { 50 INC_STATS("DOM.WebSocket.Constructor"); 51 52 if (!args.IsConstructCall()) 53 return throwError("DOM object custructor cannot be called as a function."); 54 if (args.Length() == 0) 55 return throwError("Not enough arguments", V8Proxy::SyntaxError); 56 57 v8::TryCatch tryCatch; 58 v8::Handle<v8::String> urlstring = args[0]->ToString(); 59 if (tryCatch.HasCaught()) 60 return throwError(tryCatch.Exception()); 61 if (urlstring.IsEmpty()) 62 return throwError("Empty URL", V8Proxy::SyntaxError); 63 64 // Get the script execution context. 65 ScriptExecutionContext* context = getScriptExecutionContext(); 66 if (!context) 67 return throwError("WebSocket constructor's associated frame is not available", V8Proxy::ReferenceError); 68 69 const KURL& url = context->completeURL(toWebCoreString(urlstring)); 70 71 RefPtr<WebSocket> webSocket = WebSocket::create(context); 72 ExceptionCode ec = 0; 73 74 if (args.Length() < 2) 75 webSocket->connect(url, ec); 76 else { 77 v8::TryCatch tryCatchProtocol; 78 v8::Handle<v8::String> protocol = args[1]->ToString(); 79 if (tryCatchProtocol.HasCaught()) 80 return throwError(tryCatchProtocol.Exception()); 81 webSocket->connect(url, toWebCoreString(protocol), ec); 82 } 83 if (ec) 84 return throwError(ec); 85 86 // Setup the standard wrapper object internal fields. 87 V8DOMWrapper::setDOMWrapper(args.Holder(), &info, webSocket.get()); 88 89 // Add object to the wrapper map. 90 webSocket->ref(); 91 V8DOMWrapper::setJSWrapperForActiveDOMObject(webSocket.get(), v8::Persistent<v8::Object>::New(args.Holder())); 92 93 return args.Holder(); 94 } 95 96 } // namespace WebCore 97 98 #endif // ENABLE(WEB_SOCKETS) 99