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/event_router.h"
14 #include "chrome/browser/extensions/extension_function_registry.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/stream_handle.h"
24 const char kOnExecuteMimeTypeHandler[] =
25 "streamsPrivate.onExecuteMimeTypeHandler";
29 namespace extensions {
32 StreamsPrivateAPI* StreamsPrivateAPI::Get(Profile* profile) {
33 return GetFactoryInstance()->GetForProfile(profile);
36 StreamsPrivateAPI::StreamsPrivateAPI(Profile* profile)
38 weak_ptr_factory_(this) {
39 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
40 content::Source<Profile>(profile));
43 StreamsPrivateAPI::~StreamsPrivateAPI() {
46 void StreamsPrivateAPI::ExecuteMimeTypeHandler(
47 const std::string& extension_id,
48 const content::WebContents* web_contents,
49 scoped_ptr<content::StreamHandle> stream,
50 int64 expected_content_size) {
51 // Create the event's arguments value.
52 scoped_ptr<base::ListValue> event_args(new base::ListValue());
53 event_args->Append(new base::StringValue(stream->GetMimeType()));
54 event_args->Append(new base::StringValue(stream->GetOriginalURL().spec()));
55 event_args->Append(new base::StringValue(stream->GetURL().spec()));
57 new base::FundamentalValue(ExtensionTabUtil::GetTabId(web_contents)));
60 if (expected_content_size <= INT_MAX)
61 size = expected_content_size;
62 event_args->Append(new base::FundamentalValue(size));
64 scoped_ptr<Event> event(new Event(events::kOnExecuteMimeTypeHandler,
67 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
68 extension_id, event.Pass());
70 GURL url = stream->GetURL();
71 streams_[extension_id][url] = make_linked_ptr(stream.release());
74 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> >
75 g_factory = LAZY_INSTANCE_INITIALIZER;
78 ProfileKeyedAPIFactory<StreamsPrivateAPI>*
79 StreamsPrivateAPI::GetFactoryInstance() {
80 return &g_factory.Get();
83 void StreamsPrivateAPI::Observe(int type,
84 const content::NotificationSource& source,
85 const content::NotificationDetails& details) {
86 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
87 const Extension* extension =
88 content::Details<const UnloadedExtensionInfo>(details)->extension;
89 streams_.erase(extension->id());
92 } // namespace extensions