1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/streams_private/streams_private_api.h"
7 #include "base/json/json_writer.h"
8 #include "base/lazy_instance.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/extensions/api/streams_private.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_source.h"
18 #include "content/public/browser/stream_handle.h"
19 #include "extensions/browser/event_router.h"
20 #include "extensions/browser/extension_function_registry.h"
21 #include "extensions/browser/extension_system.h"
22 #include "net/http/http_response_headers.h"
26 void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers,
27 base::DictionaryValue* result) {
32 std::string header_name;
33 std::string header_value;
34 while (headers->EnumerateHeaderLines(&iter, &header_name, &header_value)) {
35 base::Value* existing_value = NULL;
36 if (result->Get(header_name, &existing_value)) {
37 base::StringValue* existing_string_value =
38 static_cast<base::StringValue*>(existing_value);
39 existing_string_value->GetString()->append(", ").append(header_value);
41 result->SetString(header_name, header_value);
48 namespace extensions {
50 namespace streams_private = api::streams_private;
53 StreamsPrivateAPI* StreamsPrivateAPI::Get(content::BrowserContext* context) {
54 return GetFactoryInstance()->Get(context);
57 StreamsPrivateAPI::StreamsPrivateAPI(content::BrowserContext* context)
58 : profile_(Profile::FromBrowserContext(context)), weak_ptr_factory_(this) {
60 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
61 content::Source<Profile>(profile_));
64 StreamsPrivateAPI::~StreamsPrivateAPI() {
67 void StreamsPrivateAPI::ExecuteMimeTypeHandler(
68 const std::string& extension_id,
69 const content::WebContents* web_contents,
70 scoped_ptr<content::StreamHandle> stream,
71 int64 expected_content_size) {
72 // Create the event's arguments value.
73 streams_private::StreamInfo info;
74 info.mime_type = stream->GetMimeType();
75 info.original_url = stream->GetOriginalURL().spec();
76 info.stream_url = stream->GetURL().spec();
77 info.tab_id = ExtensionTabUtil::GetTabId(web_contents);
80 if (expected_content_size <= INT_MAX)
81 size = expected_content_size;
82 info.expected_content_size = size;
84 CreateResponseHeadersDictionary(stream->GetResponseHeaders().get(),
85 &info.response_headers.additional_properties);
87 scoped_ptr<Event> event(
88 new Event(streams_private::OnExecuteMimeTypeHandler::kEventName,
89 streams_private::OnExecuteMimeTypeHandler::Create(info)));
91 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
92 extension_id, event.Pass());
94 GURL url = stream->GetURL();
95 streams_[extension_id][url] = make_linked_ptr(stream.release());
98 static base::LazyInstance<BrowserContextKeyedAPIFactory<StreamsPrivateAPI> >
99 g_factory = LAZY_INSTANCE_INITIALIZER;
102 BrowserContextKeyedAPIFactory<StreamsPrivateAPI>*
103 StreamsPrivateAPI::GetFactoryInstance() {
104 return g_factory.Pointer();
107 void StreamsPrivateAPI::Observe(int type,
108 const content::NotificationSource& source,
109 const content::NotificationDetails& details) {
110 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) {
111 const Extension* extension =
112 content::Details<const UnloadedExtensionInfo>(details)->extension;
113 streams_.erase(extension->id());
116 } // namespace extensions