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> | |
10 #include <type_traits> | |
11 #include <unordered_map> | |
12 #include <utility> | |
9 #include <vector> | 13 #include <vector> |
10 | 14 |
11 #include "base/macros.h" | 15 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
13 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
14 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
15 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
16 #include "components/arc/intent_helper/activity_icon_loader.h" | 20 #include "components/arc/intent_helper/activity_icon_loader.h" |
17 #include "components/arc/intent_helper/local_activity_resolver.h" | 21 #include "components/arc/intent_helper/local_activity_resolver.h" |
18 | 22 |
19 namespace arc { | 23 namespace arc { |
20 | 24 |
21 class ArcBridgeService; | 25 class ArcBridgeService; |
22 class ArcIntentHelperObserver; | 26 class ArcIntentHelperObserver; |
23 class ArcService; | 27 class ArcService; |
24 | 28 |
29 namespace internal { | |
30 | |
31 // If an ArcService is declared with a Name, e.g.: | |
Yusuke Sato
2017/01/10 19:30:57
s/Name/name/ is better now.
Luis Héctor Chávez
2017/01/10 23:48:09
Done.
| |
32 // | |
33 // class MyArcService : public ArcService { | |
34 // public: | |
35 // static const char kArcServiceName[]; | |
36 // ... | |
37 // }; | |
38 // | |
39 // it can then be retrieved from ArcServiceManager in a type-safe way using | |
40 // GetService<T>(). This two functions allow AddService() to get the name only | |
41 // if it was provided, or use an empty string otherwise. | |
42 // | |
43 // In order to avoid collisions, kArcServiceName should be the fully-qualified | |
44 // name of the class. | |
45 template <typename T> | |
46 decltype(T::kArcServiceName, std::string()) GetArcServiceName(T* unused) { | |
47 return T::kArcServiceName; | |
Yusuke Sato
2017/01/10 19:30:57
LOG(ERROR) when T::kArcServiceName is an empty str
Luis Héctor Chávez
2017/01/10 23:48:09
Done.
| |
48 } | |
49 | |
50 template <typename T> | |
51 std::string GetArcServiceName(...) { | |
52 return std::string(); | |
53 } | |
54 | |
55 } // namespace internal | |
56 | |
25 // Manages creation and destruction of services that communicate with the ARC | 57 // Manages creation and destruction of services that communicate with the ARC |
26 // instance via the ArcBridgeService. | 58 // instance via the ArcBridgeService. |
27 class ArcServiceManager { | 59 class ArcServiceManager { |
28 public: | 60 public: |
29 class Observer { | 61 class Observer { |
30 public: | 62 public: |
31 // Called when ArcServiceManager is being shut down. Observer | 63 // Called when ArcServiceManager is being shut down. Observer |
32 // implementation should clean up ARC related stuff here. One of the | 64 // implementation should clean up ARC related stuff here. One of the |
33 // typical use cases is calling ArcServiceManager::RemoveObserver(). | 65 // typical use cases is calling ArcServiceManager::RemoveObserver(). |
34 virtual void OnArcShutdown() = 0; | 66 virtual void OnArcShutdown() = 0; |
35 | 67 |
36 // Called when intent filters are added or removed. | 68 // Called when intent filters are added or removed. |
37 virtual void OnIntentFiltersUpdated() = 0; | 69 virtual void OnIntentFiltersUpdated() = 0; |
38 | 70 |
39 protected: | 71 protected: |
40 virtual ~Observer() = default; | 72 virtual ~Observer() = default; |
41 }; | 73 }; |
42 | 74 |
43 explicit ArcServiceManager( | 75 explicit ArcServiceManager( |
44 scoped_refptr<base::TaskRunner> blocking_task_runner); | 76 scoped_refptr<base::TaskRunner> blocking_task_runner); |
45 ~ArcServiceManager(); | 77 ~ArcServiceManager(); |
46 | 78 |
47 // |arc_bridge_service| can only be accessed on the thread that this | 79 // |arc_bridge_service| can only be accessed on the thread that this |
48 // class was created on. | 80 // class was created on. |
49 ArcBridgeService* arc_bridge_service(); | 81 ArcBridgeService* arc_bridge_service(); |
50 | 82 |
51 // Adds a service to the managed services list. | 83 // Adds a service to the managed services list. Returns false if another |
52 void AddService(std::unique_ptr<ArcService> service); | 84 // named service with that name had already been added. |
85 template <typename T> | |
86 bool AddService(std::unique_ptr<T> service) { | |
87 return AddServiceInternal(internal::GetArcServiceName<T>(nullptr), | |
88 std::move(service)); | |
89 } | |
90 | |
91 // Gets the named service from the managed services list. This uses SFINAE, so | |
92 // you can only call this function if the service specified by T provides a | |
93 // static member variable called kArcServiceName[] (otherwise this will not | |
94 // compile). | |
95 template <typename T> | |
96 T* GetService() { | |
97 return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName)); | |
98 } | |
53 | 99 |
54 // Gets the global instance of the ARC Service Manager. This can only be | 100 // Gets the global instance of the ARC Service Manager. This can only be |
55 // called on the thread that this class was created on. | 101 // called on the thread that this class was created on. |
56 static ArcServiceManager* Get(); | 102 static ArcServiceManager* Get(); |
57 | 103 |
58 void AddObserver(Observer* observer); | 104 void AddObserver(Observer* observer); |
59 void RemoveObserver(Observer* observer); | 105 void RemoveObserver(Observer* observer); |
60 | 106 |
61 // Called to shut down all ARC services. | 107 // Called to shut down all ARC services. |
62 void Shutdown(); | 108 void Shutdown(); |
(...skipping 11 matching lines...) Expand all Loading... | |
74 } | 120 } |
75 | 121 |
76 // Returns the IntentHelperObserver instance owned by ArcServiceManager. | 122 // Returns the IntentHelperObserver instance owned by ArcServiceManager. |
77 ArcIntentHelperObserver* intent_helper_observer() { | 123 ArcIntentHelperObserver* intent_helper_observer() { |
78 return intent_helper_observer_.get(); | 124 return intent_helper_observer_.get(); |
79 } | 125 } |
80 | 126 |
81 private: | 127 private: |
82 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. | 128 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. |
83 | 129 |
130 // Helper methods for AddService and GetService. | |
131 bool AddServiceInternal(const std::string& name, | |
132 std::unique_ptr<ArcService> service); | |
133 ArcService* GetNamedServiceInternal(const std::string& name); | |
134 | |
84 base::ThreadChecker thread_checker_; | 135 base::ThreadChecker thread_checker_; |
85 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 136 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
86 | 137 |
87 // An object for observing the ArcIntentHelper instance in |services_|. | 138 // An object for observing the ArcIntentHelper instance in |services_|. |
88 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; | 139 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; |
89 | 140 |
90 std::unique_ptr<ArcBridgeService> arc_bridge_service_; | 141 std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
91 std::vector<std::unique_ptr<ArcService>> services_; | 142 std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_; |
92 scoped_refptr<ActivityIconLoader> icon_loader_; | 143 scoped_refptr<ActivityIconLoader> icon_loader_; |
93 scoped_refptr<LocalActivityResolver> activity_resolver_; | 144 scoped_refptr<LocalActivityResolver> activity_resolver_; |
94 | 145 |
95 base::ObserverList<Observer> observer_list_; | 146 base::ObserverList<Observer> observer_list_; |
96 | 147 |
97 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); | 148 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); |
98 }; | 149 }; |
99 | 150 |
100 } // namespace arc | 151 } // namespace arc |
101 | 152 |
102 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 153 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
OLD | NEW |