| Index: chrome/browser/chromeos/file_system_provider/file_system_plugin/plugineventrouter.cc
|
| diff --git a/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugineventrouter.cc b/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugineventrouter.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..246f344e3f43e1abd913bb5b53289b8f07618aac
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugineventrouter.cc
|
| @@ -0,0 +1,196 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/chromeos/file_system_provider/file_system_plugin/plugineventrouter.h"
|
| +
|
| +#include "base/memory/singleton.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "extensions/browser/event_router.h"
|
| +
|
| +using content::BrowserThread;
|
| +using extensions::Event;
|
| +
|
| +namespace chromeos {
|
| +
|
| +PluginEventRouter::PluginEventRouter() : weak_ptr_factory_(this) {
|
| +}
|
| +
|
| +PluginEventRouter::~PluginEventRouter() {
|
| +}
|
| +
|
| +// Static
|
| +PluginEventRouter* PluginEventRouter::GetInstance() {
|
| + return Singleton<PluginEventRouter>::get();
|
| +}
|
| +
|
| +void PluginEventRouter::AddEventsListener(
|
| + const std::vector<std::string>& event_names,
|
| + PluginEventRouter::PluginEventRouterClient* plugin_ref,
|
| + const std::string& plugin_id) {
|
| + for (std::vector<std::string>::const_iterator it = event_names.begin();
|
| + it != event_names.end(); ++it) {
|
| + AddEventListener(*it, plugin_ref, plugin_id);
|
| + }
|
| +}
|
| +
|
| +void PluginEventRouter::AddEventListener(
|
| + const std::string& event_name,
|
| + PluginEventRouter::PluginEventRouterClient* plugin_ref,
|
| + const std::string& plugin_id) {
|
| + EventListIterator it = listOfListenedEvents_.find(event_name);
|
| + // Empty list
|
| + if (it == listOfListenedEvents_.end()) {
|
| + listOfListenedEvents_.insert(std::pair<std::string, EventToPluginList>(
|
| + event_name, EventToPluginList(event_name, plugin_id, plugin_ref)));
|
| + return;
|
| + }
|
| + it->second.AddPluginInstance(plugin_id, plugin_ref);
|
| +}
|
| +
|
| +void PluginEventRouter::RemoveEventsListeners(
|
| + const std::vector<std::string>& event_names,
|
| + PluginEventRouter::PluginEventRouterClient* plugin_ref,
|
| + const std::string& plugin_id) {
|
| + for (std::vector<std::string>::const_iterator it = event_names.begin();
|
| + it != event_names.end(); ++it) {
|
| + RemoveEventListener(*it, plugin_ref, plugin_id);
|
| + }
|
| +}
|
| +
|
| +void PluginEventRouter::RemoveEventListener(
|
| + const std::string& event_name,
|
| + PluginEventRouter::PluginEventRouterClient* plugin_ref,
|
| + const std::string& plugin_id) {
|
| + EventListIterator it = listOfListenedEvents_.find(event_name);
|
| +
|
| + if (it != listOfListenedEvents_.end()) {
|
| + it->second.RemovePluginInstance(plugin_id, plugin_ref);
|
| + if (it->second.Empty())
|
| + listOfListenedEvents_.erase(it);
|
| + }
|
| +}
|
| +
|
| +bool PluginEventRouter::HasEventListener(const std::string& event_name) {
|
| + // Race
|
| + return listOfListenedEvents_.find(event_name) != listOfListenedEvents_.end();
|
| +}
|
| +
|
| +bool PluginEventRouter::PluginHasEventListener(const std::string& event_name,
|
| + const std::string& plugin_id) {
|
| + // Race
|
| + EventListIterator it = listOfListenedEvents_.find(event_name);
|
| + if (it != listOfListenedEvents_.end())
|
| + return it->second.HasPlugin(plugin_id);
|
| + return false;
|
| +}
|
| +
|
| +void PluginEventRouter::DispatchEventToPlugin(
|
| + const std::string& plugin_id,
|
| + scoped_ptr<extensions::Event> event) {
|
| + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
|
| + BrowserThread::PostTask(
|
| + BrowserThread::IO, FROM_HERE,
|
| + base::Bind(
|
| + base::IgnoreResult(&PluginEventRouter::DispatchEventToPlugin),
|
| + weak_ptr_factory_.GetWeakPtr(), plugin_id,
|
| + base::Passed(event.Pass())));
|
| + return;
|
| + }
|
| + EventListIterator it = listOfListenedEvents_.find(event->event_name);
|
| + if (it != listOfListenedEvents_.end()) {
|
| + it->second.DispatchEventToPlugin(plugin_id, event->event_name,
|
| + event->event_args.Pass());
|
| + }
|
| +}
|
| +
|
| +PluginListeningInstances::PluginListeningInstances(
|
| + const std::string& plugin_id,
|
| + PluginEventRouter::PluginEventRouterClient* instance_)
|
| + : plugin_id_(plugin_id) {
|
| + AddPluginInstance(instance_);
|
| +}
|
| +
|
| +PluginListeningInstances::~PluginListeningInstances() {
|
| +}
|
| +
|
| +void PluginListeningInstances::AddPluginInstance(
|
| + PluginEventRouter::PluginEventRouterClient* instance) {
|
| + SetOfInstancesIterator it = plugin_instances_.find(instance);
|
| + if (it == plugin_instances_.end())
|
| + plugin_instances_.insert(instance);
|
| +}
|
| +
|
| +void PluginListeningInstances::RemovePluginInstance(
|
| + PluginEventRouter::PluginEventRouterClient* instance) {
|
| + SetOfInstancesIterator it = plugin_instances_.find(instance);
|
| + if (it != plugin_instances_.end())
|
| + plugin_instances_.erase(it);
|
| +}
|
| +
|
| +bool PluginListeningInstances::Empty() {
|
| + return plugin_instances_.size() == 0 ? true : false;
|
| +}
|
| +
|
| +void PluginListeningInstances::DispatchEvent(
|
| + std::string event_name,
|
| + scoped_ptr<base::ListValue> event_args) {
|
| + for (SetOfInstancesIterator it = plugin_instances_.begin();
|
| + it != plugin_instances_.end(); ++it) {
|
| + (*it)->OnDispatchEvent(event_name, event_args.Pass());
|
| + }
|
| +}
|
| +
|
| +EventToPluginList::EventToPluginList(
|
| + const std::string& event_name,
|
| + const std::string& plugin_id,
|
| + PluginEventRouter::PluginEventRouterClient* instance)
|
| + : event_name_(event_name) {
|
| + AddPluginInstance(plugin_id, instance);
|
| +}
|
| +
|
| +EventToPluginList::~EventToPluginList() {
|
| +}
|
| +
|
| +void EventToPluginList::AddPluginInstance(
|
| + const std::string& plugin_id,
|
| + PluginEventRouter::PluginEventRouterClient* instance) {
|
| + PluginListIterator it = plugin_list_.find(plugin_id);
|
| + if (it == plugin_list_.end()) {
|
| + plugin_list_.insert(
|
| + std::pair<ListOfPlugins::key_type, ListOfPlugins::mapped_type>(
|
| + plugin_id, PluginListeningInstances(plugin_id, instance)));
|
| + return;
|
| + }
|
| + it->second.AddPluginInstance(instance);
|
| +}
|
| +
|
| +void EventToPluginList::RemovePluginInstance(
|
| + const std::string& plugin_id,
|
| + PluginEventRouter::PluginEventRouterClient* instance) {
|
| + PluginListIterator it = plugin_list_.find(plugin_id);
|
| + if (it != plugin_list_.end()) {
|
| + it->second.RemovePluginInstance(instance);
|
| + if (it->second.Empty())
|
| + plugin_list_.erase(it);
|
| + }
|
| +}
|
| +
|
| +bool EventToPluginList::Empty() {
|
| + return plugin_list_.size() ? true : false;
|
| +}
|
| +
|
| +bool EventToPluginList::HasPlugin(const std::string& plugin_id) {
|
| + return plugin_list_.find(plugin_id) != plugin_list_.end();
|
| +}
|
| +
|
| +void EventToPluginList::DispatchEventToPlugin(
|
| + std::string plugin_id,
|
| + std::string event_name,
|
| + scoped_ptr<base::ListValue> event_args) {
|
| + PluginListIterator it = plugin_list_.find(plugin_id);
|
| + // Dispatch it to all instances
|
| + if (it != plugin_list_.end())
|
| + it->second.DispatchEvent(event_name, event_args.Pass());
|
| +}
|
| +} // namespace chromeos
|
|
|