JSXMLHttpRequest.cpp 8.57 KB
Newer Older
ap's avatar
ap committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2004 Apple Computer, Inc.
 *  Copyright (C) 2005, 2006 Alexey Proskuryakov <ap@nypop.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"
#include "JSXMLHttpRequest.h"

darin's avatar
darin committed
24
#include "Frame.h"
darin's avatar
darin committed
25
#include "PlatformString.h"
darin's avatar
darin committed
26
#include "html_documentimpl.h"
ap's avatar
ap committed
27
#include "kjs_events.h"
darin's avatar
darin committed
28 29
#include "kjs_window.h"
#include "xmlhttprequest.h"
ap's avatar
ap committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#include "JSXMLHttpRequest.lut.h"

using namespace WebCore;

namespace KJS {

////////////////////// JSXMLHttpRequest Object ////////////////////////

/* Source for JSXMLHttpRequestProtoTable.
@begin JSXMLHttpRequestProtoTable 7
  abort                 JSXMLHttpRequest::Abort                   DontDelete|Function 0
  getAllResponseHeaders JSXMLHttpRequest::GetAllResponseHeaders   DontDelete|Function 0
  getResponseHeader     JSXMLHttpRequest::GetResponseHeader       DontDelete|Function 1
  open                  JSXMLHttpRequest::Open                    DontDelete|Function 5
  overrideMimeType      JSXMLHttpRequest::OverrideMIMEType        DontDelete|Function 1
  send                  JSXMLHttpRequest::Send                    DontDelete|Function 1
  setRequestHeader      JSXMLHttpRequest::SetRequestHeader        DontDelete|Function 2
@end
*/
KJS_DEFINE_PROTOTYPE(JSXMLHttpRequestProto)
KJS_IMPLEMENT_PROTOFUNC(JSXMLHttpRequestProtoFunc)
KJS_IMPLEMENT_PROTOTYPE("JSXMLHttpRequest", JSXMLHttpRequestProto, JSXMLHttpRequestProtoFunc)

JSXMLHttpRequestConstructorImp::JSXMLHttpRequestConstructorImp(ExecState *exec, DocumentImpl *d)
    : doc(d)
{
    putDirect(prototypePropertyName, JSXMLHttpRequestProto::self(exec), DontEnum|DontDelete|ReadOnly);
}

JSXMLHttpRequestConstructorImp::~JSXMLHttpRequestConstructorImp()
{
}

bool JSXMLHttpRequestConstructorImp::implementsConstruct() const
{
  return true;
}

JSObject *JSXMLHttpRequestConstructorImp::construct(ExecState *exec, const List &)
{
  return new JSXMLHttpRequest(exec, doc.get());
}

const ClassInfo JSXMLHttpRequest::info = { "JSXMLHttpRequest", 0, &JSXMLHttpRequestTable, 0 };

/* Source for JSXMLHttpRequestTable.
@begin JSXMLHttpRequestTable 7
  readyState            JSXMLHttpRequest::ReadyState              DontDelete|ReadOnly
  responseText          JSXMLHttpRequest::ResponseText            DontDelete|ReadOnly
  responseXML           JSXMLHttpRequest::ResponseXML             DontDelete|ReadOnly
  status                JSXMLHttpRequest::Status                  DontDelete|ReadOnly
  statusText            JSXMLHttpRequest::StatusText              DontDelete|ReadOnly
  onreadystatechange    JSXMLHttpRequest::Onreadystatechange      DontDelete
  onload                JSXMLHttpRequest::Onload                  DontDelete
@end
*/

bool JSXMLHttpRequest::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot& slot)
{
  return getStaticValueSlot<JSXMLHttpRequest, DOMObject>(exec, &JSXMLHttpRequestTable, this, propertyName, slot);
}

darin's avatar
darin committed
93
JSValue* JSXMLHttpRequest::getValueProperty(ExecState *exec, int token) const
ap's avatar
ap committed
94 95 96 97 98 99
{
  switch (token) {
  case ReadyState:
    return jsNumber(m_impl->getReadyState());
  case ResponseText:
    return jsStringOrNull(m_impl->getResponseText());
darin's avatar
darin committed
100 101 102 103
  case ResponseXML:
    if (DocumentImpl* responseXML = m_impl->getResponseXML())
      return getDOMNode(exec, responseXML);
    return jsUndefined();
104
  case Status: {
ap's avatar
ap committed
105 106
    int status = m_impl->getStatus();
    return status > 0 ? jsNumber(status) : jsUndefined();
107
  }
ap's avatar
ap committed
108 109 110
  case StatusText:
    return jsStringOrUndefined(m_impl->getStatusText());
  case Onreadystatechange:
darin's avatar
darin committed
111 112
   if (JSUnprotectedEventListener* listener = static_cast<JSUnprotectedEventListener*>(m_impl->onReadyStateChangeListener()))
      if (JSObject* listenerObj = listener->listenerObj())
ap's avatar
ap committed
113 114 115
        return listenerObj;
   return jsNull();
  case Onload:
darin's avatar
darin committed
116 117
   if (JSUnprotectedEventListener* listener = static_cast<JSUnprotectedEventListener*>(m_impl->onLoadListener()))
      if (JSObject* listenerObj = listener->listenerObj())
ap's avatar
ap committed
118 119 120
        return listenerObj;
   return jsNull();
  default:
darin's avatar
darin committed
121
    return 0;
ap's avatar
ap committed
122 123 124
  }
}

darin's avatar
darin committed
125
void JSXMLHttpRequest::put(ExecState *exec, const Identifier &propertyName, JSValue* value, int attr)
ap's avatar
ap committed
126 127 128 129
{
  lookupPut<JSXMLHttpRequest,DOMObject>(exec, propertyName, value, attr, &JSXMLHttpRequestTable, this );
}

darin's avatar
darin committed
130
void JSXMLHttpRequest::putValueProperty(ExecState *exec, int token, JSValue* value, int /*attr*/)
ap's avatar
ap committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
  switch(token) {
  case Onreadystatechange:
    m_impl->setOnReadyStateChangeListener(Window::retrieveActive(exec)->getJSUnprotectedEventListener(value, true));
    break;
  case Onload:
    m_impl->setOnLoadListener(Window::retrieveActive(exec)->getJSUnprotectedEventListener(value, true));
    break;
  }
}

void JSXMLHttpRequest::mark()
{
  DOMObject::mark();

darin's avatar
darin committed
146 147
  JSUnprotectedEventListener* onReadyStateChangeListener = static_cast<JSUnprotectedEventListener*>(m_impl->onReadyStateChangeListener());
  JSUnprotectedEventListener* onLoadListener = static_cast<JSUnprotectedEventListener*>(m_impl->onLoadListener());
ap's avatar
ap committed
148 149 150 151 152 153 154 155 156 157 158 159 160

  if (onReadyStateChangeListener)
    onReadyStateChangeListener->mark();

  if (onLoadListener)
    onLoadListener->mark();
}


JSXMLHttpRequest::JSXMLHttpRequest(ExecState *exec, DocumentImpl *d)
  : m_impl(new XMLHttpRequest(d))
{
  setPrototype(JSXMLHttpRequestProto::self(exec));
darin's avatar
darin committed
161
  ScriptInterpreter::putDOMObject(m_impl.get(), this);
ap's avatar
ap committed
162 163 164 165
}

JSXMLHttpRequest::~JSXMLHttpRequest()
{
darin's avatar
darin committed
166 167 168
  m_impl->setOnReadyStateChangeListener(0);
  m_impl->setOnLoadListener(0);
  ScriptInterpreter::forgetDOMObject(m_impl.get());
ap's avatar
ap committed
169 170
}

darin's avatar
darin committed
171
JSValue* JSXMLHttpRequestProtoFunc::callAsFunction(ExecState *exec, JSObject* thisObj, const List& args)
ap's avatar
ap committed
172 173 174 175 176 177 178
{
  if (!thisObj->inherits(&JSXMLHttpRequest::info))
    return throwError(exec, TypeError);

  JSXMLHttpRequest *request = static_cast<JSXMLHttpRequest *>(thisObj);

  switch (id) {
darin's avatar
darin committed
179
  case JSXMLHttpRequest::Abort:
ap's avatar
ap committed
180 181
    request->m_impl->abort();
    return jsUndefined();
darin's avatar
darin committed
182
  case JSXMLHttpRequest::GetAllResponseHeaders:
ap's avatar
ap committed
183
    return jsStringOrUndefined(request->m_impl->getAllResponseHeaders());
darin's avatar
darin committed
184 185
  case JSXMLHttpRequest::GetResponseHeader:
    if (args.size() != 1)
ap's avatar
ap committed
186
      return jsUndefined();
darin's avatar
darin committed
187
    return jsStringOrUndefined(request->m_impl->getResponseHeader(args[0]->toString(exec).domString()));
ap's avatar
ap committed
188 189
  case JSXMLHttpRequest::Open:
    {
darin's avatar
darin committed
190
      if (args.size() < 2 || args.size() > 5)
ap's avatar
ap committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        return jsUndefined();
    
      DOMString method = args[0]->toString(exec).domString();
      KURL url = KURL(Window::retrieveActive(exec)->frame()->document()->completeURL(args[1]->toString(exec).qstring()));

      bool async = true;
      if (args.size() >= 3)
        async = args[2]->toBoolean(exec);
    
      DOMString user;
      if (args.size() >= 4)
        user = args[3]->toString(exec).domString();
      
      DOMString password;
      if (args.size() >= 5)
        password = args[4]->toString(exec).domString();

      request->m_impl->open(method, url, async, user, password);

      return jsUndefined();
    }
  case JSXMLHttpRequest::Send:
    {
darin's avatar
darin committed
214
      if (args.size() > 1)
ap's avatar
ap committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        return jsUndefined();

      DOMString body;

      if (args.size() >= 1) {
        if (args[0]->toObject(exec)->inherits(&DOMDocument::info)) {
          DocumentImpl *doc = static_cast<DocumentImpl *>(static_cast<DOMDocument *>(args[0]->toObject(exec))->impl());
          body = doc->toString().qstring();
        } else {
          // converting certain values (like null) to object can set an exception
          exec->clearException();
          body = args[0]->toString(exec).domString();
        }
      }

      request->m_impl->send(body);

      return jsUndefined();
    }
darin's avatar
darin committed
234 235
  case JSXMLHttpRequest::SetRequestHeader:
    if (args.size() != 2)
ap's avatar
ap committed
236 237 238
      return jsUndefined();
    request->m_impl->setRequestHeader(args[0]->toString(exec).domString(), args[1]->toString(exec).domString());
    return jsUndefined();
darin's avatar
darin committed
239 240
  case JSXMLHttpRequest::OverrideMIMEType:
    if (args.size() != 1)
ap's avatar
ap committed
241 242 243 244 245 246 247 248 249
      return jsUndefined();
    request->m_impl->overrideMIMEType(args[0]->toString(exec).domString());
    return jsUndefined();
  }

  return jsUndefined();
}

} // end namespace