Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / extensions / browser / api / mime_handler_private / mime_handler_private.cc
blobb7d557d18015285cb3bb90a542fb863dfdf0bfaa
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 {
15 namespace {
17 mojo::Map<mojo::String, mojo::String> CreateResponseHeadersMap(
18 const net::HttpResponseHeaders* headers) {
19 std::map<std::string, std::string> result;
20 if (!headers)
21 return mojo::Map<mojo::String, mojo::String>::From(result);
23 void* iter = nullptr;
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);
35 } // namespace
37 // static
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) {
54 if (!stream_) {
55 callback.Run(mime_handler::StreamInfoPtr());
56 return;
58 callback.Run(mojo::ConvertTo<mime_handler::StreamInfoPtr>(*stream_));
61 void MimeHandlerServiceImpl::AbortStream(
62 const mojo::Callback<void()>& callback) {
63 if (!stream_) {
64 callback.Run();
65 return;
67 stream_->Abort(base::Bind(&MimeHandlerServiceImpl::OnStreamClosed,
68 weak_factory_.GetWeakPtr(), callback));
71 void MimeHandlerServiceImpl::OnStreamClosed(
72 const mojo::Callback<void()>& callback) {
73 callback.Run();
76 } // namespace extensions
78 namespace mojo {
80 extensions::mime_handler::StreamInfoPtr TypeConverter<
81 extensions::mime_handler::StreamInfoPtr,
82 extensions::StreamContainer>::Convert(const extensions::StreamContainer&
83 stream) {
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());
96 return result.Pass();
99 } // namespace mojo