| Index: chrome/browser/chromeos/file_system_provider/file_system_plugin/plugin_operation_router.cc
 | 
| diff --git a/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugin_operation_router.cc b/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugin_operation_router.cc
 | 
| new file mode 100644
 | 
| index 0000000000000000000000000000000000000000..3aad8c03c4fbde52c06d8d179da3549f6466470e
 | 
| --- /dev/null
 | 
| +++ b/chrome/browser/chromeos/file_system_provider/file_system_plugin/plugin_operation_router.cc
 | 
| @@ -0,0 +1,113 @@
 | 
| +// 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/plugin_operation_router.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 {
 | 
| +namespace file_system_provider {
 | 
| +PluginOperationRouter::PluginOperationRouter() : weak_ptr_factory_(this) {
 | 
| +}
 | 
| +
 | 
| +PluginOperationRouter::~PluginOperationRouter() {
 | 
| +}
 | 
| +
 | 
| +// Static
 | 
| +PluginOperationRouter* PluginOperationRouter::GetInstance() {
 | 
| +  return Singleton<PluginOperationRouter>::get();
 | 
| +}
 | 
| +
 | 
| +void PluginOperationRouter::AddOperationsForListener(
 | 
| +    const std::vector<RequestType>& operation_types,
 | 
| +    PluginOperationRouterClient* plugin_ref,
 | 
| +    const std::string& plugin_id) {
 | 
| +  DCHECK_CURRENTLY_ON(BrowserThread::IO);
 | 
| +  for (
 | 
| +    std::vector<RequestType>::const_iterator it =
 | 
| +        operation_types.begin();
 | 
| +       it != operation_types.end(); ++it) {
 | 
| +    AddOperationForListener(*it, plugin_ref, plugin_id);
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +void PluginOperationRouter::AddOperationForListener(
 | 
| +    RequestType operation_type,
 | 
| +    PluginOperationRouterClient* plugin_ref,
 | 
| +    const std::string& plugin_id) {
 | 
| +  DCHECK_CURRENTLY_ON(BrowserThread::IO);
 | 
| +  OperationsListIterator it = listOfListenedEvents_.find(operation_type);
 | 
| +  // If the event is not in the list add it
 | 
| +  if (it == listOfListenedEvents_.end()) {
 | 
| +    it = listOfListenedEvents_.insert(listOfListenedEvents_.begin(),
 | 
| +                                 std::make_pair(operation_type,
 | 
| +                                                PluginInstances() ) );
 | 
| +  }
 | 
| +  it->second.insert( std::make_pair(plugin_id, plugin_ref) );
 | 
| +}
 | 
| +
 | 
| +void PluginOperationRouter::RemoveOperationsForListener(
 | 
| +    const std::vector<RequestType>& operation_types,
 | 
| +    const std::string& plugin_id) {
 | 
| +  DCHECK_CURRENTLY_ON(BrowserThread::IO);
 | 
| +  for (std::vector<RequestType>::const_iterator
 | 
| +          it = operation_types.begin();
 | 
| +       it != operation_types.end(); ++it) {
 | 
| +    RemoveOperationForListener(*it, plugin_id);
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +void PluginOperationRouter::RemoveOperationForListener(
 | 
| +    RequestType operation_type,
 | 
| +    const std::string& plugin_id) {
 | 
| +  DCHECK_CURRENTLY_ON(BrowserThread::IO);
 | 
| +  OperationsListIterator it = listOfListenedEvents_.find(operation_type);
 | 
| +
 | 
| +  if (it!=listOfListenedEvents_.end()) {
 | 
| +    PluginInstancesIterator plugin_it = it->second.find(plugin_id);
 | 
| +    if (plugin_it!=it->second.end())
 | 
| +      it->second.erase(plugin_it);
 | 
| +    if(it->second.size()==0)
 | 
| +      listOfListenedEvents_.erase(it);
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +bool PluginOperationRouter::PluginHasOperationListener(
 | 
| +    RequestType operation_type,
 | 
| +    const std::string& plugin_id) {
 | 
| +  OperationsListIterator it = listOfListenedEvents_.find(operation_type);
 | 
| +  if (it != listOfListenedEvents_.end())
 | 
| +    return it->second.find(plugin_id)!=it->second.end();
 | 
| +  return false;
 | 
| +}
 | 
| +
 | 
| +void PluginOperationRouter::DispatchOperationToPlugin(
 | 
| +    const std::string& plugin_id,
 | 
| +    scoped_ptr<extensions::Event> request) {
 | 
| +  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
 | 
| +    BrowserThread::PostTask(
 | 
| +        BrowserThread::IO, FROM_HERE,
 | 
| +        base::Bind(
 | 
| +            base::IgnoreResult(&PluginOperationRouter::DispatchOperationToPlugin),
 | 
| +            weak_ptr_factory_.GetWeakPtr(), plugin_id,
 | 
| +            base::Passed(request.Pass())));
 | 
| +    return;
 | 
| +  }
 | 
| +  RequestType type = ProvidedFileSystemAdapter::OperationTypeFromExtensionEvent(
 | 
| +      request->event_name);
 | 
| +  OperationsListIterator it = listOfListenedEvents_.find(type);
 | 
| +  if (it != listOfListenedEvents_.end()) {
 | 
| +    PluginInstancesIterator plugin_it = it->second.find( plugin_id );
 | 
| +    if (plugin_it!=it->second.end())
 | 
| +      plugin_it->second->OnDispatchOperationRequest(type,
 | 
| +            request->event_args.Pass());
 | 
| +  }
 | 
| +}
 | 
| +}  // namespace file_system_provider
 | 
| +}  // namespace chromeos
 | 
| 
 |