1 // Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "include/wrapper/cef_stream_resource_handler.h"
6
7 #include <algorithm>
8
9 #include "include/base/cef_logging.h"
10 #include "include/cef_task.h"
11 #include "include/wrapper/cef_helpers.h"
12
CefStreamResourceHandler(const CefString & mime_type,CefRefPtr<CefStreamReader> stream)13 CefStreamResourceHandler::CefStreamResourceHandler(
14 const CefString& mime_type,
15 CefRefPtr<CefStreamReader> stream)
16 : status_code_(200),
17 status_text_("OK"),
18 mime_type_(mime_type),
19 stream_(stream) {
20 DCHECK(!mime_type_.empty());
21 }
22
CefStreamResourceHandler(int status_code,const CefString & status_text,const CefString & mime_type,CefResponse::HeaderMap header_map,CefRefPtr<CefStreamReader> stream)23 CefStreamResourceHandler::CefStreamResourceHandler(
24 int status_code,
25 const CefString& status_text,
26 const CefString& mime_type,
27 CefResponse::HeaderMap header_map,
28 CefRefPtr<CefStreamReader> stream)
29 : status_code_(status_code),
30 status_text_(status_text),
31 mime_type_(mime_type),
32 header_map_(header_map),
33 stream_(stream) {
34 DCHECK(!mime_type_.empty());
35 }
36
Open(CefRefPtr<CefRequest> request,bool & handle_request,CefRefPtr<CefCallback> callback)37 bool CefStreamResourceHandler::Open(CefRefPtr<CefRequest> request,
38 bool& handle_request,
39 CefRefPtr<CefCallback> callback) {
40 DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
41
42 // Continue the request immediately.
43 handle_request = true;
44 return true;
45 }
46
GetResponseHeaders(CefRefPtr<CefResponse> response,int64 & response_length,CefString & redirectUrl)47 void CefStreamResourceHandler::GetResponseHeaders(
48 CefRefPtr<CefResponse> response,
49 int64& response_length,
50 CefString& redirectUrl) {
51 CEF_REQUIRE_IO_THREAD();
52
53 response->SetStatus(status_code_);
54 response->SetStatusText(status_text_);
55 response->SetMimeType(mime_type_);
56
57 if (!header_map_.empty())
58 response->SetHeaderMap(header_map_);
59
60 response_length = stream_ ? -1 : 0;
61 }
62
Read(void * data_out,int bytes_to_read,int & bytes_read,CefRefPtr<CefResourceReadCallback> callback)63 bool CefStreamResourceHandler::Read(
64 void* data_out,
65 int bytes_to_read,
66 int& bytes_read,
67 CefRefPtr<CefResourceReadCallback> callback) {
68 DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO));
69 DCHECK_GT(bytes_to_read, 0);
70 DCHECK(stream_);
71
72 // Read until the buffer is full or until Read() returns 0 to indicate no
73 // more data.
74 bytes_read = 0;
75 int read = 0;
76 do {
77 read = static_cast<int>(
78 stream_->Read(static_cast<char*>(data_out) + bytes_read, 1,
79 bytes_to_read - bytes_read));
80 bytes_read += read;
81 } while (read != 0 && bytes_read < bytes_to_read);
82
83 return (bytes_read > 0);
84 }
85
Cancel()86 void CefStreamResourceHandler::Cancel() {}
87