OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chromeos/file_system_provider/operations/operation.h" | 5 #include "chrome/browser/chromeos/file_system_provider/operations/operation.h" |
6 | 6 |
| 7 #include "chrome/browser/chromeos/file_system_provider/file_system_plugin/plugin
eventrouter.h" |
7 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | 8 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
8 #include "extensions/browser/event_router.h" | 9 #include "extensions/browser/event_router.h" |
9 | 10 |
10 namespace chromeos { | 11 namespace chromeos { |
11 namespace file_system_provider { | 12 namespace file_system_provider { |
12 namespace operations { | 13 namespace operations { |
13 namespace { | 14 namespace { |
14 | |
15 // Default implementation for dispatching an event. Can be replaced for unit | 15 // Default implementation for dispatching an event. Can be replaced for unit |
16 // tests by Operation::SetDispatchEventImplForTest(). | 16 // tests by Operation::SetDispatchEventImplForTest(). |
17 bool DispatchEventImpl(extensions::EventRouter* event_router, | 17 template <int source = Source_Type::extension> |
18 const std::string& extension_id, | 18 bool DispatchEventImpl( |
19 scoped_ptr<extensions::Event> event) { | 19 typename Source_Traits<source>::EventRouterType* event_router, |
| 20 const std::string& extension_id, |
| 21 scoped_ptr<extensions::Event> event) { |
20 if (!event_router->ExtensionHasEventListener(extension_id, event->event_name)) | 22 if (!event_router->ExtensionHasEventListener(extension_id, event->event_name)) |
21 return false; | 23 return false; |
22 | 24 |
23 event_router->DispatchEventToExtension(extension_id, event.Pass()); | 25 event_router->DispatchEventToExtension(extension_id, event.Pass()); |
24 return true; | 26 return true; |
25 } | 27 } |
26 | 28 |
| 29 template <> |
| 30 bool DispatchEventImpl<Source_Type::plugin>( |
| 31 typename Source_Traits<Source_Type::plugin>::EventRouterType* event_router, |
| 32 const std::string& source_id, |
| 33 scoped_ptr<extensions::Event> event) { |
| 34 if (!event_router->PluginHasEventListener(event->event_name, source_id)) |
| 35 return false; |
| 36 |
| 37 event_router->DispatchEventToPlugin(source_id, event.Pass()); |
| 38 return true; |
| 39 } |
| 40 |
27 } // namespace | 41 } // namespace |
28 | 42 |
29 Operation::Operation(extensions::EventRouter* event_router, | 43 template <int source> |
30 const ProvidedFileSystemInfo& file_system_info) | 44 Operation<source>::Operation( |
| 45 typename Operation<source>::EventRouterType* event_router, |
| 46 const ProvidedFileSystemInfo& file_system_info) |
31 : file_system_info_(file_system_info), | 47 : file_system_info_(file_system_info), |
32 dispatch_event_impl_(base::Bind(&DispatchEventImpl, | 48 dispatch_event_impl_(base::Bind(&DispatchEventImpl<source>, |
33 event_router, | 49 event_router, |
34 file_system_info_.extension_id())) { | 50 file_system_info_.source_id())) { |
| 51 } |
| 52 template <int source> |
| 53 Operation<source>::~Operation() { |
35 } | 54 } |
36 | 55 |
37 Operation::~Operation() { | 56 template <int source> |
38 } | 57 void Operation<source>::SetDispatchEventImplForTesting( |
39 | |
40 void Operation::SetDispatchEventImplForTesting( | |
41 const DispatchEventImplCallback& callback) { | 58 const DispatchEventImplCallback& callback) { |
42 dispatch_event_impl_ = callback; | 59 dispatch_event_impl_ = callback; |
43 } | 60 } |
44 | 61 |
45 bool Operation::SendEvent(int request_id, | 62 template <int source> |
46 const std::string& event_name, | 63 bool Operation<source>::SendEvent(int request_id, |
47 scoped_ptr<base::ListValue> event_args) { | 64 const std::string& event_name, |
| 65 scoped_ptr<base::ListValue> event_args) { |
48 return dispatch_event_impl_.Run( | 66 return dispatch_event_impl_.Run( |
49 make_scoped_ptr(new extensions::Event(event_name, event_args.Pass()))); | 67 make_scoped_ptr(new extensions::Event(event_name, event_args.Pass()))); |
50 } | 68 } |
51 | 69 |
| 70 template class Operation<Source_Type::extension>; |
| 71 template class Operation<Source_Type::plugin>; |
| 72 |
52 } // namespace operations | 73 } // namespace operations |
53 } // namespace file_system_provider | 74 } // namespace file_system_provider |
54 } // namespace chromeos | 75 } // namespace chromeos |
OLD | NEW |