1/*
2 * Copyright (C) 2012 Intel Corporation. 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ewk_url_scheme_request.h"
28
29#include "WKData.h"
30#include "WKRetainPtr.h"
31#include "WKSoupRequestManager.h"
32#include "WKString.h"
33#include <wtf/text/CString.h>
34
35using namespace WebKit;
36
37/**
38 * \struct _Ewk_Url_Scheme_Request
39 * @brief Contains the URL scheme request data.
40 */
41struct _Ewk_Url_Scheme_Request {
42 unsigned int __ref; /**< the reference count of the object */
43 WKRetainPtr<WKSoupRequestManagerRef> wkRequestManager;
44 const char* url;
45 uint64_t requestID;
46 const char* scheme;
47 const char* path;
48
49 _Ewk_Url_Scheme_Request(WKSoupRequestManagerRef manager, const char* _url, uint64_t _requestID)
50 : __ref(1)
51 , wkRequestManager(manager)
52 , url(eina_stringshare_add(_url))
53 , requestID(_requestID)
54 {
55 SoupURI* soupURI = soup_uri_new(_url);
56 scheme = eina_stringshare_add(soupURI->scheme);
57 path = eina_stringshare_add(soupURI->path);
58 soup_uri_free(soupURI);
59 }
60};
61
62void ewk_url_scheme_request_ref(Ewk_Url_Scheme_Request* request)
63{
64 EINA_SAFETY_ON_NULL_RETURN(request);
65 ++request->__ref;
66}
67
68void ewk_url_scheme_request_unref(Ewk_Url_Scheme_Request* request)
69{
70 EINA_SAFETY_ON_NULL_RETURN(request);
71
72 if (--request->__ref)
73 return;
74
75 eina_stringshare_del(request->url);
76 eina_stringshare_del(request->scheme);
77 eina_stringshare_del(request->path);
78 delete request;
79}
80
81const char* ewk_url_scheme_request_scheme_get(const Ewk_Url_Scheme_Request* request)
82{
83 EINA_SAFETY_ON_NULL_RETURN_VAL(request, 0);
84
85 return request->scheme;
86}
87
88const char* ewk_url_scheme_request_url_get(const Ewk_Url_Scheme_Request* request)
89{
90 EINA_SAFETY_ON_NULL_RETURN_VAL(request, 0);
91
92 return request->url;
93}
94
95const char* ewk_url_scheme_request_path_get(const Ewk_Url_Scheme_Request* request)
96{
97 EINA_SAFETY_ON_NULL_RETURN_VAL(request, 0);
98
99 return request->path;
100}
101
102/**
103 * @internal
104 * Returns the #Ewk_Url_Scheme_Request identifier.
105 */
106uint64_t ewk_url_scheme_request_id_get(const Ewk_Url_Scheme_Request* request)
107{
108 EINA_SAFETY_ON_NULL_RETURN_VAL(request, 0);
109
110 return request->requestID;
111}
112
113Eina_Bool ewk_url_scheme_request_finish(const Ewk_Url_Scheme_Request* request, void* contentData, unsigned int contentLength, const char* mimeType)
114{
115 EINA_SAFETY_ON_NULL_RETURN_VAL(request, false);
116
117 WKRetainPtr<WKDataRef> wkData(AdoptWK, WKDataCreate(contentLength ? reinterpret_cast<const unsigned char*>(contentData) : 0, contentLength));
118 WKRetainPtr<WKStringRef> wkMimeType = mimeType ? adoptWK(WKStringCreateWithUTF8CString(mimeType)) : 0;
119
120 // In case of empty reply an empty WKDataRef is sent to the WebProcess.
121 WKSoupRequestManagerDidHandleURIRequest(request->wkRequestManager.get(), wkData.get(), contentLength, wkMimeType.get(), request->requestID);
122
123 return true;
124}
125
126/**
127 * @internal
128 * Constructs a Ewk_Url_Scheme_Request.
129 */
130Ewk_Url_Scheme_Request* ewk_url_scheme_request_new(WKSoupRequestManagerRef requestManager, WKURLRef url, uint64_t requestID)
131{
132 EINA_SAFETY_ON_NULL_RETURN_VAL(requestManager, 0);
133 EINA_SAFETY_ON_NULL_RETURN_VAL(url, 0);
134
135 return new Ewk_Url_Scheme_Request(requestManager, toImpl(url)->string().utf8().data(), requestID);
136}