OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h" | 5 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | |
8 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
9 #include "base/stl_util.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/values.h" | 8 #include "base/values.h" |
12 #include "chrome/browser/chrome_notification_types.h" | |
13 #include "chrome/browser/extensions/extension_tab_util.h" | 9 #include "chrome/browser/extensions/extension_tab_util.h" |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/extensions/api/streams_private.h" | 10 #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" | 11 #include "content/public/browser/stream_handle.h" |
19 #include "extensions/browser/event_router.h" | 12 #include "extensions/browser/event_router.h" |
20 #include "extensions/browser/extension_function_registry.h" | 13 #include "extensions/browser/extension_function_registry.h" |
| 14 #include "extensions/browser/extension_registry.h" |
21 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
22 | 16 |
23 namespace { | 17 namespace { |
24 | 18 |
25 void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers, | 19 void CreateResponseHeadersDictionary(const net::HttpResponseHeaders* headers, |
26 base::DictionaryValue* result) { | 20 base::DictionaryValue* result) { |
27 if (!headers) | 21 if (!headers) |
28 return; | 22 return; |
29 | 23 |
30 void* iter = NULL; | 24 void* iter = NULL; |
(...skipping 16 matching lines...) Expand all Loading... |
47 namespace extensions { | 41 namespace extensions { |
48 | 42 |
49 namespace streams_private = api::streams_private; | 43 namespace streams_private = api::streams_private; |
50 | 44 |
51 // static | 45 // static |
52 StreamsPrivateAPI* StreamsPrivateAPI::Get(content::BrowserContext* context) { | 46 StreamsPrivateAPI* StreamsPrivateAPI::Get(content::BrowserContext* context) { |
53 return GetFactoryInstance()->Get(context); | 47 return GetFactoryInstance()->Get(context); |
54 } | 48 } |
55 | 49 |
56 StreamsPrivateAPI::StreamsPrivateAPI(content::BrowserContext* context) | 50 StreamsPrivateAPI::StreamsPrivateAPI(content::BrowserContext* context) |
57 : profile_(Profile::FromBrowserContext(context)), weak_ptr_factory_(this) { | 51 : browser_context_(context), |
58 registrar_.Add(this, | 52 weak_ptr_factory_(this), |
59 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 53 extension_registry_observer_(this) { |
60 content::Source<Profile>(profile_)); | 54 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
61 } | 55 } |
62 | 56 |
63 StreamsPrivateAPI::~StreamsPrivateAPI() { | 57 StreamsPrivateAPI::~StreamsPrivateAPI() { |
64 } | 58 } |
65 | 59 |
66 void StreamsPrivateAPI::ExecuteMimeTypeHandler( | 60 void StreamsPrivateAPI::ExecuteMimeTypeHandler( |
67 const std::string& extension_id, | 61 const std::string& extension_id, |
68 const content::WebContents* web_contents, | 62 const content::WebContents* web_contents, |
69 scoped_ptr<content::StreamHandle> stream, | 63 scoped_ptr<content::StreamHandle> stream, |
70 int64 expected_content_size) { | 64 int64 expected_content_size) { |
71 // Create the event's arguments value. | 65 // Create the event's arguments value. |
72 streams_private::StreamInfo info; | 66 streams_private::StreamInfo info; |
73 info.mime_type = stream->GetMimeType(); | 67 info.mime_type = stream->GetMimeType(); |
74 info.original_url = stream->GetOriginalURL().spec(); | 68 info.original_url = stream->GetOriginalURL().spec(); |
75 info.stream_url = stream->GetURL().spec(); | 69 info.stream_url = stream->GetURL().spec(); |
76 info.tab_id = ExtensionTabUtil::GetTabId(web_contents); | 70 info.tab_id = ExtensionTabUtil::GetTabId(web_contents); |
77 | 71 |
78 int size = -1; | 72 int size = -1; |
79 if (expected_content_size <= INT_MAX) | 73 if (expected_content_size <= INT_MAX) |
80 size = expected_content_size; | 74 size = expected_content_size; |
81 info.expected_content_size = size; | 75 info.expected_content_size = size; |
82 | 76 |
83 CreateResponseHeadersDictionary(stream->GetResponseHeaders().get(), | 77 CreateResponseHeadersDictionary(stream->GetResponseHeaders().get(), |
84 &info.response_headers.additional_properties); | 78 &info.response_headers.additional_properties); |
85 | 79 |
86 scoped_ptr<Event> event( | 80 scoped_ptr<Event> event( |
87 new Event(streams_private::OnExecuteMimeTypeHandler::kEventName, | 81 new Event(streams_private::OnExecuteMimeTypeHandler::kEventName, |
88 streams_private::OnExecuteMimeTypeHandler::Create(info))); | 82 streams_private::OnExecuteMimeTypeHandler::Create(info))); |
89 | 83 |
90 EventRouter::Get(profile_)->DispatchEventToExtension( | 84 EventRouter::Get(browser_context_) |
91 extension_id, event.Pass()); | 85 ->DispatchEventToExtension(extension_id, event.Pass()); |
92 | 86 |
93 GURL url = stream->GetURL(); | 87 GURL url = stream->GetURL(); |
94 streams_[extension_id][url] = make_linked_ptr(stream.release()); | 88 streams_[extension_id][url] = make_linked_ptr(stream.release()); |
95 } | 89 } |
96 | 90 |
| 91 void StreamsPrivateAPI::OnExtensionUnloaded( |
| 92 content::BrowserContext* browser_context, |
| 93 const Extension* extension, |
| 94 UnloadedExtensionInfo::Reason reason) { |
| 95 streams_.erase(extension->id()); |
| 96 } |
| 97 |
97 static base::LazyInstance<BrowserContextKeyedAPIFactory<StreamsPrivateAPI> > | 98 static base::LazyInstance<BrowserContextKeyedAPIFactory<StreamsPrivateAPI> > |
98 g_factory = LAZY_INSTANCE_INITIALIZER; | 99 g_factory = LAZY_INSTANCE_INITIALIZER; |
99 | 100 |
100 // static | 101 // static |
101 BrowserContextKeyedAPIFactory<StreamsPrivateAPI>* | 102 BrowserContextKeyedAPIFactory<StreamsPrivateAPI>* |
102 StreamsPrivateAPI::GetFactoryInstance() { | 103 StreamsPrivateAPI::GetFactoryInstance() { |
103 return g_factory.Pointer(); | 104 return g_factory.Pointer(); |
104 } | 105 } |
105 | 106 |
106 void StreamsPrivateAPI::Observe(int type, | |
107 const content::NotificationSource& source, | |
108 const content::NotificationDetails& details) { | |
109 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED) { | |
110 const Extension* extension = | |
111 content::Details<const UnloadedExtensionInfo>(details)->extension; | |
112 streams_.erase(extension->id()); | |
113 } | |
114 } | |
115 } // namespace extensions | 107 } // namespace extensions |
OLD | NEW |