2009-01-23 David Levin <levin@chromium.org>
authorap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Jan 2009 16:07:37 +0000 (16:07 +0000)
committerap@webkit.org <ap@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 23 Jan 2009 16:07:37 +0000 (16:07 +0000)
        Reviewed by Alexey Proskuryakov.

        Bug 22720: Make XMLHttpRequest work in Workers
        <https://bugs.webkit.org/show_bug.cgi?id=22720>

        Add copy/adopt for ResourceResponse(Base)/ResourceRequest(Base) to allow the
        data to be passed across threads.

        No observable change in behavior, so no test.

        * platform/network/FormData.cpp:
        (WebCore::FormData::deepCopy):
        * platform/network/FormData.h:
        * platform/network/HTTPHeaderMap.cpp:
        (WebCore::HTTPHeaderMap::copyData):
        (WebCore::HTTPHeaderMap::adopt):
        * platform/network/HTTPHeaderMap.h:
        * platform/network/ResourceRequestBase.cpp:
        (WebCore::ResourceRequestBase::adopt):
        (WebCore::ResourceRequestBase::copyData):
        * platform/network/ResourceRequestBase.h:
        * platform/network/ResourceResponseBase.cpp:
        (WebCore::ResourceResponseBase::adopt):
        (WebCore::ResourceResponseBase::copyData):
        * platform/network/ResourceResponseBase.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@40162 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/platform/network/FormData.cpp
WebCore/platform/network/FormData.h
WebCore/platform/network/HTTPHeaderMap.cpp
WebCore/platform/network/HTTPHeaderMap.h
WebCore/platform/network/ResourceRequestBase.cpp
WebCore/platform/network/ResourceRequestBase.h
WebCore/platform/network/ResourceResponseBase.cpp
WebCore/platform/network/ResourceResponseBase.h

index 548b0b7..039e773 100644 (file)
@@ -1,3 +1,31 @@
+2009-01-23  David Levin  <levin@chromium.org>
+
+        Reviewed by Alexey Proskuryakov.
+
+        Bug 22720: Make XMLHttpRequest work in Workers
+        <https://bugs.webkit.org/show_bug.cgi?id=22720>
+
+        Add copy/adopt for ResourceResponse(Base)/ResourceRequest(Base) to allow the
+        data to be passed across threads.
+
+        No observable change in behavior, so no test.
+
+        * platform/network/FormData.cpp:
+        (WebCore::FormData::deepCopy):
+        * platform/network/FormData.h:
+        * platform/network/HTTPHeaderMap.cpp:
+        (WebCore::HTTPHeaderMap::copyData):
+        (WebCore::HTTPHeaderMap::adopt):
+        * platform/network/HTTPHeaderMap.h:
+        * platform/network/ResourceRequestBase.cpp:
+        (WebCore::ResourceRequestBase::adopt):
+        (WebCore::ResourceRequestBase::copyData):
+        * platform/network/ResourceRequestBase.h:
+        * platform/network/ResourceResponseBase.cpp:
+        (WebCore::ResourceResponseBase::adopt):
+        (WebCore::ResourceResponseBase::copyData):
+        * platform/network/ResourceResponseBase.h:
+
 2009-01-23  Dmitry Titov  <dimich@chromium.org>
 
         Reviewed by Alexey Proskuryakov.
index 0d31856..3cac168 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -90,6 +91,28 @@ PassRefPtr<FormData> FormData::copy() const
     return adoptRef(new FormData(*this));
 }
 
+PassRefPtr<FormData> FormData::deepCopy() const
+{
+    RefPtr<FormData> formData(create());
+
+    formData->m_alwaysStream = m_alwaysStream;
+
+    size_t n = m_elements.size();
+    formData->m_elements.reserveCapacity(n);
+    for (size_t i = 0; i < n; ++i) {
+        const FormDataElement& e = m_elements[i];
+        switch (e.m_type) {
+        case FormDataElement::data:
+            formData->m_elements.append(FormDataElement(e.m_data));
+            break;
+        case FormDataElement::encodedFile:
+            formData->m_elements.append(FormDataElement(e.m_filename, e.m_shouldGenerateFile));
+            break;
+        }
+    }
+    return formData.release();
+}
+
 void FormData::appendData(const void* data, size_t size)
 {
     if (m_elements.isEmpty() || m_elements.last().m_type != FormDataElement::data)
index cb91fab..5998b1b 100644 (file)
@@ -68,6 +68,7 @@ public:
     static PassRefPtr<FormData> create(const CString&);
     static PassRefPtr<FormData> create(const Vector<char>&);
     PassRefPtr<FormData> copy() const;
+    PassRefPtr<FormData> deepCopy() const;
     ~FormData();
     
     void appendData(const void* data, size_t);
index 4ae3035..aa9c5fa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, Google Inc. All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -38,9 +38,9 @@ using namespace std;
 
 namespace WebCore {
 
-auto_ptr<HTTPHeaderMapData> HTTPHeaderMap::copyData() const
+auto_ptr<CrossThreadHTTPHeaderMapData> HTTPHeaderMap::copyData() const
 {
-    auto_ptr<HTTPHeaderMapData> data(new HTTPHeaderMapData());
+    auto_ptr<CrossThreadHTTPHeaderMapData> data(new CrossThreadHTTPHeaderMapData());
     data->reserveCapacity(size());
 
     HTTPHeaderMap::const_iterator end_it = end();
@@ -50,12 +50,13 @@ auto_ptr<HTTPHeaderMapData> HTTPHeaderMap::copyData() const
     return data;
 }
 
-void HTTPHeaderMap::adopt(auto_ptr<HTTPHeaderMapData> data)
+void HTTPHeaderMap::adopt(auto_ptr<CrossThreadHTTPHeaderMapData> data)
 {
     clear();
-    HTTPHeaderMapData::const_iterator end_it = data->end();
-    for (HTTPHeaderMapData::const_iterator it = data->begin(); it != end_it; ++it) {
-        set(it->first, it->second);
+    size_t dataSize = data->size();
+    for (size_t index = 0; index < dataSize; ++index) {
+        pair<String, String>& header = (*data)[index];
+        set(header.first, header.second);
     }
 }
 
index 126c57d..6da1b90 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 namespace WebCore {
 
-    typedef Vector<std::pair<String, String> > HTTPHeaderMapData;
+    typedef Vector<std::pair<String, String> > CrossThreadHTTPHeaderMapData;
 
     class HTTPHeaderMap : public HashMap<AtomicString, String, CaseFoldingHash> {
     public:
         // Gets a copy of the data suitable for passing to another thread.
-        std::auto_ptr<HTTPHeaderMapData> copyData() const;
+        std::auto_ptr<CrossThreadHTTPHeaderMapData> copyData() const;
 
-        void adopt(std::auto_ptr<HTTPHeaderMapData>);
+        void adopt(std::auto_ptr<CrossThreadHTTPHeaderMapData>);
     };
 
 } // namespace WebCore
index 3984d8e..15469a0 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -26,6 +27,8 @@
 #include "ResourceRequestBase.h"
 #include "ResourceRequest.h"
 
+using namespace std;
+
 namespace WebCore {
 
 inline const ResourceRequest& ResourceRequestBase::asResourceRequest() const
@@ -33,6 +36,57 @@ inline const ResourceRequest& ResourceRequestBase::asResourceRequest() const
     return *static_cast<const ResourceRequest*>(this);
 }
 
+auto_ptr<ResourceRequest> ResourceRequestBase::adopt(auto_ptr<CrossThreadResourceRequestData> data)
+{
+    auto_ptr<ResourceRequest> request(new ResourceRequest());
+    request->setURL(data->m_url);
+    request->setCachePolicy(data->m_cachePolicy);
+    request->setTimeoutInterval(data->m_timeoutInterval);
+    request->setMainDocumentURL(data->m_mainDocumentURL);
+    request->setHTTPMethod(data->m_httpMethod);
+
+    request->updateResourceRequest();
+    request->m_httpHeaderFields.adopt(auto_ptr<CrossThreadHTTPHeaderMapData>(data->m_httpHeaders.release()));
+
+    size_t encodingCount = data->m_responseContentDispositionEncodingFallbackArray.size();
+    if (encodingCount > 0) {
+        String encoding1 = data->m_responseContentDispositionEncodingFallbackArray[0];
+        String encoding2;
+        String encoding3;
+        if (encodingCount > 1) {
+            encoding2 = data->m_responseContentDispositionEncodingFallbackArray[1];
+            if (encodingCount > 2)
+                encoding3 = data->m_responseContentDispositionEncodingFallbackArray[2];
+        }
+        ASSERT(encodingCount <= 3);
+        request->setResponseContentDispositionEncodingFallbackArray(encoding1, encoding2, encoding3);
+    }
+    request->setHTTPBody(data->m_httpBody);
+    request->setAllowHTTPCookies(data->m_allowHTTPCookies);
+    return request;
+}
+
+auto_ptr<CrossThreadResourceRequestData> ResourceRequestBase::copyData() const
+{
+    auto_ptr<CrossThreadResourceRequestData> data(new CrossThreadResourceRequestData());
+    data->m_url = url().copy();
+    data->m_cachePolicy = cachePolicy();
+    data->m_timeoutInterval = timeoutInterval();
+    data->m_mainDocumentURL = mainDocumentURL().copy();
+    data->m_httpMethod = httpMethod().copy();
+    data->m_httpHeaders.adopt(httpHeaderFields().copyData());
+
+    data->m_responseContentDispositionEncodingFallbackArray.reserveCapacity(m_responseContentDispositionEncodingFallbackArray.size());
+    size_t encodingArraySize = m_responseContentDispositionEncodingFallbackArray.size();
+    for (size_t index = 0; index < encodingArraySize; ++index) {
+        data->m_responseContentDispositionEncodingFallbackArray.append(m_responseContentDispositionEncodingFallbackArray[index].copy());
+    }
+    if (m_httpBody)
+        data->m_httpBody = m_httpBody->deepCopy();
+    data->m_allowHTTPCookies = m_allowHTTPCookies;
+    return data;
+}
+
 bool ResourceRequestBase::isEmpty() const
 {
     updateResourceRequest(); 
index 1cbf21e..0f6bb47 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -31,6 +32,9 @@
 #include "KURL.h"
 #include "HTTPHeaderMap.h"
 
+#include <memory>
+#include <wtf/OwnPtr.h>
+
 namespace WebCore {
 
     enum ResourceRequestCachePolicy {
@@ -43,10 +47,16 @@ namespace WebCore {
     const int unspecifiedTimeoutInterval = INT_MAX;
 
     class ResourceRequest;
+    struct CrossThreadResourceRequestData;
 
     // Do not use this type directly.  Use ResourceRequest instead.
     class ResourceRequestBase {
     public:
+        static std::auto_ptr<ResourceRequest> adopt(std::auto_ptr<CrossThreadResourceRequestData>);
+
+        // Gets a copy of the data suitable for passing to another thread.
+        std::auto_ptr<CrossThreadResourceRequestData> copyData() const;
+
         bool isNull() const;
         bool isEmpty() const;
 
@@ -142,6 +152,20 @@ namespace WebCore {
     bool operator==(const ResourceRequestBase&, const ResourceRequestBase&);
     inline bool operator!=(ResourceRequestBase& a, const ResourceRequestBase& b) { return !(a == b); }
 
+    struct CrossThreadResourceRequestData {
+        KURL m_url;
+
+        ResourceRequestCachePolicy m_cachePolicy;
+        double m_timeoutInterval;
+        KURL m_mainDocumentURL;
+
+        String m_httpMethod;
+        OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
+        Vector<String> m_responseContentDispositionEncodingFallbackArray;
+        RefPtr<FormData> m_httpBody;
+        bool m_allowHTTPCookies;
+    };
+
 } // namespace WebCore
 
 #endif // ResourceRequestBase_h
index fb7f5c7..f84e97d 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 
 #include "ResourceResponse.h"
 
+using namespace std;
+
 namespace WebCore {
 
 static void parseCacheHeader(const String& header, Vector<pair<String, String> >& result);
 static void parseCacheControlDirectiveValues(const String& directives, Vector<String>& result);
 
+auto_ptr<ResourceResponse> ResourceResponseBase::adopt(auto_ptr<CrossThreadResourceResponseData> data)
+{
+    auto_ptr<ResourceResponse> response(new ResourceResponse());
+    response->setUrl(data->m_url);
+    response->setMimeType(data->m_mimeType);
+    response->setExpectedContentLength(data->m_expectedContentLength);
+    response->setTextEncodingName(data->m_textEncodingName);
+    response->setSuggestedFilename(data->m_suggestedFilename);
+
+    response->setHTTPStatusCode(data->m_httpStatusCode);
+    response->setHTTPStatusText(data->m_httpStatusText);
+
+    response->lazyInit();
+    response->m_httpHeaderFields.adopt(std::auto_ptr<CrossThreadHTTPHeaderMapData>(data->m_httpHeaders.release()));
+
+    response->setExpirationDate(data->m_expirationDate);
+    response->setLastModifiedDate(data->m_lastModifiedDate);
+    response->m_haveParsedCacheControl = data->m_haveParsedCacheControl;
+    response->m_cacheControlContainsMustRevalidate = data->m_cacheControlContainsMustRevalidate;
+    response->m_cacheControlContainsNoCache = data->m_cacheControlContainsNoCache;
+    return response;
+}
+
+auto_ptr<CrossThreadResourceResponseData> ResourceResponseBase::copyData() const
+{
+    auto_ptr<CrossThreadResourceResponseData> data(new CrossThreadResourceResponseData());
+    data->m_url = url().copy();
+    data->m_mimeType = mimeType().copy();
+    data->m_expectedContentLength = expectedContentLength();
+    data->m_textEncodingName = textEncodingName().copy();
+    data->m_suggestedFilename = suggestedFilename().copy();
+    data->m_httpStatusCode = httpStatusCode();
+    data->m_httpStatusText = httpStatusText().copy();
+    data->m_httpHeaders.adopt(httpHeaderFields().copyData());
+    data->m_expirationDate = expirationDate();
+    data->m_lastModifiedDate = lastModifiedDate();
+    data->m_haveParsedCacheControl = m_haveParsedCacheControl;
+    data->m_cacheControlContainsMustRevalidate = m_cacheControlContainsMustRevalidate;
+    data->m_cacheControlContainsNoCache = m_cacheControlContainsNoCache;
+    return data;
+}
+
 bool ResourceResponseBase::isHTTP() const
 {
     lazyInit();
index 20df8af..abc756e 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2009 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
 #include "HTTPHeaderMap.h"
 #include "KURL.h"
 
+#include <memory>
+
 namespace WebCore {
 
 class ResourceResponse;
+struct CrossThreadResourceResponseData;
 
 // Do not use this class directly, use the class ResponseResponse instead
 class ResourceResponseBase {
 public:
+    static std::auto_ptr<ResourceResponse> adopt(std::auto_ptr<CrossThreadResourceResponseData>);
+
+    // Gets a copy of the data suitable for passing to another thread.
+    std::auto_ptr<CrossThreadResourceResponseData> copyData() const;
+
     bool isNull() const { return m_isNull; }
     bool isHTTP() const;
 
@@ -146,6 +155,22 @@ private:
 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponseBase::compare(a, b); }
 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
 
+struct CrossThreadResourceResponseData {
+    KURL m_url;
+    String m_mimeType;
+    long long m_expectedContentLength;
+    String m_textEncodingName;
+    String m_suggestedFilename;
+    int m_httpStatusCode;
+    String m_httpStatusText;
+    OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
+    time_t m_expirationDate;
+    time_t m_lastModifiedDate;
+    bool m_haveParsedCacheControl : 1;
+    bool m_cacheControlContainsMustRevalidate : 1;
+    bool m_cacheControlContainsNoCache : 1;
+};
+
 } // namespace WebCore
 
 #endif // ResourceResponseBase_h