Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_download_job.cpp
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_download_job.h"
28
29 #include "DownloadProxy.h"
30 #include "WKAPICast.h"
31 #include "WKRetainPtr.h"
32 #include "WebURLRequest.h"
33 #include "ewk_download_job_private.h"
34 #include "ewk_url_response_private.h"
35 #include <Ecore.h>
36
37 using namespace WebKit;
38
39 Ewk_Download_Job::Ewk_Download_Job(WebKit::DownloadProxy* download, EwkViewImpl* viewImpl)
40     : m_downloadProxy(download)
41     , m_viewImpl(viewImpl)
42     , m_state(EWK_DOWNLOAD_JOB_STATE_NOT_STARTED)
43     , m_startTime(-1)
44     , m_endTime(-1)
45     , m_downloaded(0)
46 { }
47
48 Ewk_Download_Job* ewk_download_job_ref(Ewk_Download_Job* download)
49 {
50     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
51
52     download->ref();
53
54     return download;
55 }
56
57 void ewk_download_job_unref(Ewk_Download_Job* download)
58 {
59     EINA_SAFETY_ON_NULL_RETURN(download);
60
61     download->deref();
62 }
63
64 /**
65  * @internal
66  * Queries the identifier for this download
67  */
68 uint64_t Ewk_Download_Job::id() const
69 {
70     return m_downloadProxy->downloadID();
71 }
72
73 /**
74  * @internal
75  * Returns the view this download is attached to.
76  * The view is needed to send notification signals.
77  */
78 EwkViewImpl* Ewk_Download_Job::viewImpl() const
79 {
80     return m_viewImpl;
81 }
82
83 Ewk_Download_Job_State ewk_download_job_state_get(const Ewk_Download_Job* download)
84 {
85     EINA_SAFETY_ON_NULL_RETURN_VAL(download, EWK_DOWNLOAD_JOB_STATE_UNKNOWN);
86
87     return download->state();
88 }
89
90 Ewk_Download_Job_State Ewk_Download_Job::state() const
91 {
92     return m_state;
93 }
94
95 Ewk_Url_Request* ewk_download_job_request_get(const Ewk_Download_Job* download)
96 {
97     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
98
99     return download->request();
100 }
101
102 Ewk_Url_Request* Ewk_Download_Job::request() const
103 {
104     if (!m_request) {
105         WKRetainPtr<WKURLRequestRef> wkURLRequest(AdoptWK, toAPI(WebURLRequest::create(m_downloadProxy->request()).leakRef()));
106         m_request = Ewk_Url_Request::create(wkURLRequest.get());
107     }
108
109     return m_request.get();
110 }
111
112 Ewk_Url_Response* ewk_download_job_response_get(const Ewk_Download_Job* download)
113 {
114     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
115
116     return download->response();
117 }
118
119 Ewk_Url_Response* Ewk_Download_Job::response() const
120 {
121     return m_response.get();
122 }
123
124 const char* ewk_download_job_destination_get(const Ewk_Download_Job* download)
125 {
126     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
127
128     return download->destination();
129 }
130
131 const char* Ewk_Download_Job::destination() const
132 {
133     return m_destination;
134 }
135
136 Eina_Bool ewk_download_job_destination_set(Ewk_Download_Job* download, const char* destination)
137 {
138     EINA_SAFETY_ON_NULL_RETURN_VAL(download, false);
139     EINA_SAFETY_ON_NULL_RETURN_VAL(destination, false);
140
141     download->setDestination(destination);
142
143     return true;
144 }
145
146 void Ewk_Download_Job::setDestination(const char* destination)
147 {
148     m_destination = destination;
149 }
150
151 const char* ewk_download_job_suggested_filename_get(const Ewk_Download_Job* download)
152 {
153     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
154
155     return download->suggestedFileName();
156 }
157
158 const char* Ewk_Download_Job::suggestedFileName() const
159 {
160     return m_suggestedFilename;
161 }
162
163 Eina_Bool ewk_download_job_cancel(Ewk_Download_Job* download)
164 {
165     EINA_SAFETY_ON_NULL_RETURN_VAL(download, false);
166
167     return download->cancel();
168 }
169
170 bool Ewk_Download_Job::cancel()
171 {
172     if (m_state != EWK_DOWNLOAD_JOB_STATE_DOWNLOADING)
173         return false;
174
175     m_state = EWK_DOWNLOAD_JOB_STATE_CANCELLING;
176     m_downloadProxy->cancel();
177     return true;
178 }
179
180 double ewk_download_job_estimated_progress_get(const Ewk_Download_Job* download)
181 {
182     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
183
184     return download->estimatedProgress();
185 }
186
187 double Ewk_Download_Job::estimatedProgress() const
188 {
189     if (!m_response)
190         return 0;
191
192     const unsigned long contentLength = m_response->contentLength();
193     if (!contentLength)
194         return 0;
195
196     return static_cast<double>(m_downloaded) / contentLength;
197 }
198
199 double ewk_download_job_elapsed_time_get(const Ewk_Download_Job* download)
200 {
201     EINA_SAFETY_ON_NULL_RETURN_VAL(download, 0);
202
203     return download->elapsedTime();
204 }
205
206 double Ewk_Download_Job::elapsedTime() const
207 {
208     // Download has not started yet.
209     if (m_startTime < 0)
210         return 0;
211
212     // Download had ended, return the time elapsed between the
213     // download start and the end event.
214     if (m_endTime >= 0)
215         return m_endTime - m_startTime;
216
217     // Download is still going.
218     return ecore_time_get() - m_startTime;
219 }
220
221 /**
222  * @internal
223  * Sets the URL @a response for this @a download.
224  */
225 void Ewk_Download_Job::setResponse(PassRefPtr<Ewk_Url_Response> response)
226 {
227     ASSERT(response);
228
229     m_response = response;
230 }
231
232 /**
233  * @internal
234  * Sets the suggested file name for this @a download.
235  */
236 void Ewk_Download_Job::setSuggestedFileName(const char* suggestedFilename)
237 {
238     m_suggestedFilename = suggestedFilename;
239 }
240
241 /**
242  * @internal
243  * Report a given amount of data was received.
244  */
245 void Ewk_Download_Job::incrementReceivedData(uint64_t length)
246 {
247     m_downloaded += length;
248 }
249
250 /**
251  * @internal
252  * Sets the state of the download.
253  */
254 void Ewk_Download_Job::setState(Ewk_Download_Job_State state)
255 {
256     m_state = state;
257
258     switch (state) {
259     case EWK_DOWNLOAD_JOB_STATE_DOWNLOADING:
260         m_startTime = ecore_time_get();
261         break;
262     case EWK_DOWNLOAD_JOB_STATE_FAILED:
263     case EWK_DOWNLOAD_JOB_STATE_CANCELLED:
264     case EWK_DOWNLOAD_JOB_STATE_FINISHED:
265         m_endTime = ecore_time_get();
266         break;
267     default:
268         break;
269     }
270 }