Chromium Code Reviews| 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 const std::string& mime_type, | |
| 52 const GURL& original_url) { | |
| 53 // Create the event's arguments value. | |
| 54 scoped_ptr<ListValue> event_args(new ListValue()); | |
| 55 event_args->Append(new base::StringValue(mime_type)); | |
|
darin (slow to review)
2013/03/19 06:12:51
it feels like these could be parameters of the Str
Zachary Kuznia
2013/03/19 06:59:35
Done.
| |
| 56 event_args->Append(new base::StringValue(original_url.spec())); | |
| 57 event_args->Append(new base::StringValue(stream->GetUrl().spec())); | |
| 58 | |
| 59 scoped_ptr<Event> event(new Event(events::kOnExecuteMimeTypeHandler, | |
| 60 event_args.Pass())); | |
| 61 | |
| 62 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | |
| 63 extension_id, event.Pass()); | |
| 64 | |
| 65 stream->RegisterCloseCallback(base::Bind(&StreamsPrivateAPI::OnStreamClosed, | |
| 66 weak_ptr_factory_.GetWeakPtr(), | |
| 67 extension_id, | |
| 68 stream->GetUrl())); | |
| 69 streams_[extension_id][stream->GetUrl()] = | |
| 70 make_linked_ptr(stream.release()); | |
| 71 } | |
| 72 | |
| 73 void StreamsPrivateAPI::OnStreamClosed(const std::string& extension_id, | |
| 74 const GURL& url) { | |
| 75 streams_[extension_id].erase(url); | |
| 76 } | |
| 77 | |
| 38 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> > | 78 static base::LazyInstance<ProfileKeyedAPIFactory<StreamsPrivateAPI> > |
| 39 g_factory = LAZY_INSTANCE_INITIALIZER; | 79 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 40 | 80 |
| 41 // static | 81 // static |
| 42 ProfileKeyedAPIFactory<StreamsPrivateAPI>* | 82 ProfileKeyedAPIFactory<StreamsPrivateAPI>* |
| 43 StreamsPrivateAPI::GetFactoryInstance() { | 83 StreamsPrivateAPI::GetFactoryInstance() { |
| 44 return &g_factory.Get(); | 84 return &g_factory.Get(); |
| 45 } | 85 } |
| 46 | 86 |
| 87 void StreamsPrivateAPI::Observe(int type, | |
| 88 const content::NotificationSource& source, | |
| 89 const content::NotificationDetails& details) { | |
| 90 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | |
|
kinuko
2013/03/18 00:28:17
(drive-by) UNLOADED event is dispatched when an ex
Zachary Kuznia
2013/03/18 01:06:55
This is where chrome.omnibox, chrome.push_messagin
kinuko
2013/03/18 01:46:17
I agree that we should handle this case too... and
| |
| 91 const Extension* extension = | |
| 92 content::Details<const UnloadedExtensionInfo>(details)->extension; | |
| 93 streams_.erase(extension->id()); | |
| 94 } | |
| 95 } | |
| 47 } // namespace extensions | 96 } // namespace extensions |
| OLD | NEW |