OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 5 #ifndef COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
6 #define COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 6 #define COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <type_traits> | 10 #include <type_traits> |
11 #include <unordered_map> | 11 #include <unordered_map> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/observer_list.h" | |
18 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
19 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
20 #include "components/arc/intent_helper/activity_icon_loader.h" | 19 #include "components/arc/intent_helper/activity_icon_loader.h" |
21 #include "components/arc/intent_helper/local_activity_resolver.h" | 20 #include "components/arc/intent_helper/local_activity_resolver.h" |
22 | 21 |
23 namespace arc { | 22 namespace arc { |
24 | 23 |
25 class ArcBridgeService; | 24 class ArcBridgeService; |
26 class ArcIntentHelperObserver; | |
27 class ArcService; | 25 class ArcService; |
28 | 26 |
29 namespace internal { | 27 namespace internal { |
30 | 28 |
31 // If an ArcService is declared with a name, e.g.: | 29 // If an ArcService is declared with a name, e.g.: |
32 // | 30 // |
33 // class MyArcService : public ArcService { | 31 // class MyArcService : public ArcService { |
34 // public: | 32 // public: |
35 // static const char kArcServiceName[]; | 33 // static const char kArcServiceName[]; |
36 // ... | 34 // ... |
(...skipping 20 matching lines...) Expand all Loading... |
57 std::string GetArcServiceName(...) { | 55 std::string GetArcServiceName(...) { |
58 return std::string(); | 56 return std::string(); |
59 } | 57 } |
60 | 58 |
61 } // namespace internal | 59 } // namespace internal |
62 | 60 |
63 // Manages creation and destruction of services that communicate with the ARC | 61 // Manages creation and destruction of services that communicate with the ARC |
64 // instance via the ArcBridgeService. | 62 // instance via the ArcBridgeService. |
65 class ArcServiceManager { | 63 class ArcServiceManager { |
66 public: | 64 public: |
67 class Observer { | |
68 public: | |
69 // Called when intent filters are added or removed. | |
70 virtual void OnIntentFiltersUpdated() = 0; | |
71 | |
72 protected: | |
73 virtual ~Observer() = default; | |
74 }; | |
75 | |
76 explicit ArcServiceManager( | 65 explicit ArcServiceManager( |
77 scoped_refptr<base::TaskRunner> blocking_task_runner); | 66 scoped_refptr<base::TaskRunner> blocking_task_runner); |
78 ~ArcServiceManager(); | 67 ~ArcServiceManager(); |
79 | 68 |
80 // |arc_bridge_service| can only be accessed on the thread that this | 69 // |arc_bridge_service| can only be accessed on the thread that this |
81 // class was created on. | 70 // class was created on. |
82 ArcBridgeService* arc_bridge_service(); | 71 ArcBridgeService* arc_bridge_service(); |
83 | 72 |
84 // Adds a service to the managed services list. Returns false if another | 73 // Adds a service to the managed services list. Returns false if another |
85 // named service with that name had already been added. | 74 // named service with that name had already been added. |
86 template <typename T> | 75 template <typename T> |
87 bool AddService(std::unique_ptr<T> service) { | 76 bool AddService(std::unique_ptr<T> service) { |
88 return AddServiceInternal(internal::GetArcServiceName<T>(nullptr), | 77 return AddServiceInternal(internal::GetArcServiceName<T>(nullptr), |
89 std::move(service)); | 78 std::move(service)); |
90 } | 79 } |
91 | 80 |
92 // Gets the named service from the managed services list. This uses SFINAE, so | 81 // Gets the named service from the managed services list. This uses SFINAE, so |
93 // you can only call this function if the service specified by T provides a | 82 // you can only call this function if the service specified by T provides a |
94 // static member variable called kArcServiceName[] (otherwise this will not | 83 // static member variable called kArcServiceName[] (otherwise this will not |
95 // compile). | 84 // compile). |
96 template <typename T> | 85 template <typename T> |
97 T* GetService() { | 86 T* GetService() { |
98 return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName)); | 87 return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName)); |
99 } | 88 } |
100 | 89 |
| 90 // Does the same as GetService(), but with the global instance. Return nullptr |
| 91 // when the instance hasn't been created or has already been destructed. |
| 92 template <typename T> static T* GetGlobalService() { |
| 93 auto* service_manager = ArcServiceManager::Get(); |
| 94 if (!service_manager) |
| 95 return nullptr; |
| 96 return service_manager->GetService<T>(); |
| 97 } |
| 98 |
101 // Gets the global instance of the ARC Service Manager. This can only be | 99 // Gets the global instance of the ARC Service Manager. This can only be |
102 // called on the thread that this class was created on. | 100 // called on the thread that this class was created on. |
103 static ArcServiceManager* Get(); | 101 static ArcServiceManager* Get(); |
104 | 102 |
105 // Returns if the ARC Service Manager instance exists. | |
106 // DO NOT CALL THIS. This function is a dirty workaround for properly shutting | |
107 // down chrome/browser/chromeos/extensions/file_manager/event_router.cc, and | |
108 // will likely be removed in the future. | |
109 static bool IsInitialized(); | |
110 | |
111 void AddObserver(Observer* observer); | |
112 void RemoveObserver(Observer* observer); | |
113 | |
114 // Called to shut down all ARC services. | 103 // Called to shut down all ARC services. |
115 void Shutdown(); | 104 void Shutdown(); |
116 | 105 |
117 scoped_refptr<base::TaskRunner> blocking_task_runner() const { | 106 scoped_refptr<base::TaskRunner> blocking_task_runner() const { |
118 return blocking_task_runner_; | 107 return blocking_task_runner_; |
119 } | 108 } |
120 | 109 |
121 // Returns the icon loader owned by ArcServiceManager and shared by services. | 110 // Returns the icon loader owned by ArcServiceManager and shared by services. |
122 scoped_refptr<ActivityIconLoader> icon_loader() { return icon_loader_; } | 111 scoped_refptr<ActivityIconLoader> icon_loader() { return icon_loader_; } |
123 | 112 |
124 // Returns the activity resolver owned by ArcServiceManager. | 113 // Returns the activity resolver owned by ArcServiceManager. |
125 scoped_refptr<LocalActivityResolver> activity_resolver() { | 114 scoped_refptr<LocalActivityResolver> activity_resolver() { |
126 return activity_resolver_; | 115 return activity_resolver_; |
127 } | 116 } |
128 | 117 |
129 // Returns the IntentHelperObserver instance owned by ArcServiceManager. | |
130 ArcIntentHelperObserver* intent_helper_observer() { | |
131 return intent_helper_observer_.get(); | |
132 } | |
133 | |
134 private: | 118 private: |
135 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. | 119 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. |
136 | 120 |
137 // Helper methods for AddService and GetService. | 121 // Helper methods for AddService and GetService. |
138 bool AddServiceInternal(const std::string& name, | 122 bool AddServiceInternal(const std::string& name, |
139 std::unique_ptr<ArcService> service); | 123 std::unique_ptr<ArcService> service); |
140 ArcService* GetNamedServiceInternal(const std::string& name); | 124 ArcService* GetNamedServiceInternal(const std::string& name); |
141 | 125 |
142 base::ThreadChecker thread_checker_; | 126 base::ThreadChecker thread_checker_; |
143 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 127 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
144 | 128 |
145 // An object for observing the ArcIntentHelper instance in |services_|. | |
146 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; | |
147 | |
148 std::unique_ptr<ArcBridgeService> arc_bridge_service_; | 129 std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
149 std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_; | 130 std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_; |
150 scoped_refptr<ActivityIconLoader> icon_loader_; | 131 scoped_refptr<ActivityIconLoader> icon_loader_; |
151 scoped_refptr<LocalActivityResolver> activity_resolver_; | 132 scoped_refptr<LocalActivityResolver> activity_resolver_; |
152 | 133 |
153 base::ObserverList<Observer> observer_list_; | |
154 | |
155 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); | 134 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); |
156 }; | 135 }; |
157 | 136 |
158 } // namespace arc | 137 } // namespace arc |
159 | 138 |
160 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 139 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
OLD | NEW |