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" | 7 #include "base/json/json_writer.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/extensions/event_router.h" | 12 #include "chrome/browser/extensions/event_router.h" |
13 #include "chrome/browser/extensions/extension_function_registry.h" | 13 #include "chrome/browser/extensions/extension_function_registry.h" |
14 #include "chrome/browser/extensions/extension_input_module_constants.h" | 14 #include "chrome/browser/extensions/extension_input_module_constants.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/extensions/mime_types_handler.h" | 17 #include "chrome/common/extensions/mime_types_handler.h" |
| 18 #include "content/public/browser/stream_handle.h" |
18 | 19 |
19 namespace keys = extension_input_module_constants; | 20 namespace keys = extension_input_module_constants; |
20 | 21 |
21 namespace events { | 22 namespace events { |
22 | 23 |
23 const char kOnExecuteMimeTypeHandler[] = | 24 const char kOnExecuteMimeTypeHandler[] = |
24 "streamsPrivate.onExecuteMimeTypeHandler"; | 25 "streamsPrivate.onExecuteMimeTypeHandler"; |
25 | 26 |
26 } // namespace events | 27 } // namespace events |
27 | 28 |
28 namespace extensions { | 29 namespace extensions { |
29 | 30 |
| 31 // static |
| 32 StreamsPrivateAPI* StreamsPrivateAPI::Get(Profile* profile) { |
| 33 return GetFactoryInstance()->GetForProfile(profile); |
| 34 } |
| 35 |
30 StreamsPrivateAPI::StreamsPrivateAPI(Profile* profile) | 36 StreamsPrivateAPI::StreamsPrivateAPI(Profile* profile) |
31 : profile_(profile) { | 37 : profile_(profile), |
| 38 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
32 (new MimeTypesHandlerParser)->Register(); | 39 (new MimeTypesHandlerParser)->Register(); |
| 40 |
| 41 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 42 content::Source<Profile>(profile)); |
33 } | 43 } |
34 | 44 |
35 StreamsPrivateAPI::~StreamsPrivateAPI() { | 45 StreamsPrivateAPI::~StreamsPrivateAPI() { |
36 } | 46 } |
37 | 47 |
| 48 void StreamsPrivateAPI::ExecuteMimeTypeHandler( |
| 49 const std::string& extension_id, |
| 50 scoped_ptr<content::StreamHandle> stream) { |
| 51 // Create the event's arguments value. |
| 52 scoped_ptr<ListValue> event_args(new 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 |
| 57 scoped_ptr<Event> event(new Event(events::kOnExecuteMimeTypeHandler, |
| 58 event_args.Pass())); |
| 59 |
| 60 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 61 extension_id, event.Pass()); |
| 62 |
| 63 streams_[extension_id][stream->GetURL()] = |
| 64 make_linked_ptr(stream.release()); |
| 65 } |
| 66 |
38 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> > | 67 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> > |
39 g_factory = LAZY_INSTANCE_INITIALIZER; | 68 g_factory = LAZY_INSTANCE_INITIALIZER; |
40 | 69 |
41 // static | 70 // static |
42 ProfileKeyedAPIFactory<StreamsPrivateAPI>* | 71 ProfileKeyedAPIFactory<StreamsPrivateAPI>* |
43 StreamsPrivateAPI::GetFactoryInstance() { | 72 StreamsPrivateAPI::GetFactoryInstance() { |
44 return &g_factory.Get(); | 73 return &g_factory.Get(); |
45 } | 74 } |
46 | 75 |
| 76 void StreamsPrivateAPI::Observe(int type, |
| 77 const content::NotificationSource& source, |
| 78 const content::NotificationDetails& details) { |
| 79 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| 80 const Extension* extension = |
| 81 content::Details<const UnloadedExtensionInfo>(details)->extension; |
| 82 streams_.erase(extension->id()); |
| 83 } |
| 84 } |
47 } // namespace extensions | 85 } // namespace extensions |
OLD | NEW |