- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / streams_private / streams_private_api.cc
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.
4
5 #include "chrome/browser/extensions/api/streams_private/streams_private_api.h"
6
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"
21
22 namespace events {
23
24 const char kOnExecuteMimeTypeHandler[] =
25     "streamsPrivate.onExecuteMimeTypeHandler";
26
27 }  // namespace events
28
29 namespace extensions {
30
31 // static
32 StreamsPrivateAPI* StreamsPrivateAPI::Get(Profile* profile) {
33   return GetFactoryInstance()->GetForProfile(profile);
34 }
35
36 StreamsPrivateAPI::StreamsPrivateAPI(Profile* profile)
37     : profile_(profile),
38       weak_ptr_factory_(this) {
39   registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
40                  content::Source<Profile>(profile));
41 }
42
43 StreamsPrivateAPI::~StreamsPrivateAPI() {
44 }
45
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()));
56   event_args->Append(
57       new base::FundamentalValue(ExtensionTabUtil::GetTabId(web_contents)));
58
59   int size = -1;
60   if (expected_content_size <= INT_MAX)
61     size = expected_content_size;
62   event_args->Append(new base::FundamentalValue(size));
63
64   scoped_ptr<Event> event(new Event(events::kOnExecuteMimeTypeHandler,
65                                     event_args.Pass()));
66
67   ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
68       extension_id, event.Pass());
69
70   GURL url = stream->GetURL();
71   streams_[extension_id][url] = make_linked_ptr(stream.release());
72 }
73
74 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> >
75     g_factory = LAZY_INSTANCE_INITIALIZER;
76
77 // static
78 ProfileKeyedAPIFactory<StreamsPrivateAPI>*
79     StreamsPrivateAPI::GetFactoryInstance() {
80   return &g_factory.Get();
81 }
82
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());
90   }
91 }
92 }  // namespace extensions