1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/browser/api/mime_handler_private/mime_handler_private.h"
7 #include "content/public/browser/stream_handle.h"
8 #include "content/public/browser/stream_info.h"
9 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
10 #include "extensions/common/constants.h"
11 #include "net/http/http_response_headers.h"
12 #include "third_party/mojo/src/mojo/public/cpp/bindings/map.h"
14 namespace extensions
{
17 mojo::Map
<mojo::String
, mojo::String
> CreateResponseHeadersMap(
18 const net::HttpResponseHeaders
* headers
) {
19 std::map
<std::string
, std::string
> result
;
21 return mojo::Map
<mojo::String
, mojo::String
>::From(result
);
24 std::string header_name
;
25 std::string header_value
;
26 while (headers
->EnumerateHeaderLines(&iter
, &header_name
, &header_value
)) {
27 auto& current_value
= result
[header_name
];
28 if (!current_value
.empty())
29 current_value
+= ", ";
30 current_value
+= header_value
;
32 return mojo::Map
<mojo::String
, mojo::String
>::From(result
);
38 void MimeHandlerServiceImpl::Create(
39 base::WeakPtr
<StreamContainer
> stream_container
,
40 mojo::InterfaceRequest
<mime_handler::MimeHandlerService
> request
) {
41 mojo::BindToRequest(new MimeHandlerServiceImpl(stream_container
), &request
);
44 MimeHandlerServiceImpl::MimeHandlerServiceImpl(
45 base::WeakPtr
<StreamContainer
> stream_container
)
46 : stream_(stream_container
), weak_factory_(this) {
49 MimeHandlerServiceImpl::~MimeHandlerServiceImpl() {
52 void MimeHandlerServiceImpl::GetStreamInfo(
53 const mojo::Callback
<void(mime_handler::StreamInfoPtr
)>& callback
) {
55 callback
.Run(mime_handler::StreamInfoPtr());
58 callback
.Run(mojo::ConvertTo
<mime_handler::StreamInfoPtr
>(*stream_
));
61 void MimeHandlerServiceImpl::AbortStream(
62 const mojo::Callback
<void()>& callback
) {
67 stream_
->Abort(base::Bind(&MimeHandlerServiceImpl::OnStreamClosed
,
68 weak_factory_
.GetWeakPtr(), callback
));
71 void MimeHandlerServiceImpl::OnStreamClosed(
72 const mojo::Callback
<void()>& callback
) {
76 } // namespace extensions
80 extensions::mime_handler::StreamInfoPtr TypeConverter
<
81 extensions::mime_handler::StreamInfoPtr
,
82 extensions::StreamContainer
>::Convert(const extensions::StreamContainer
&
84 if (!stream
.stream_info()->handle
)
85 return extensions::mime_handler::StreamInfoPtr();
87 auto result
= extensions::mime_handler::StreamInfo::New();
88 result
->embedded
= stream
.embedded();
89 result
->tab_id
= stream
.tab_id();
90 const content::StreamInfo
* info
= stream
.stream_info();
91 result
->mime_type
= info
->mime_type
;
92 result
->original_url
= info
->original_url
.spec();
93 result
->stream_url
= info
->handle
->GetURL().spec();
94 result
->response_headers
=
95 extensions::CreateResponseHeadersMap(info
->response_headers
.get());