| 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.: |
| 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 // Although the typename is always specified explicitly by the caller, the |
| 44 // parameter is required in order for SFINAE to work correctly. It is not used |
| 45 // and can be nullptr, though. |
| 46 // |
| 47 // In order to avoid collisions, kArcServiceName should be the fully-qualified |
| 48 // name of the class. |
| 49 template <typename T> |
| 50 decltype(T::kArcServiceName, std::string()) GetArcServiceName(T* unused) { |
| 51 if (strlen(T::kArcServiceName) == 0) |
| 52 LOG(ERROR) << "kArcServiceName[] should be a fully-qualified class name."; |
| 53 return T::kArcServiceName; |
| 54 } |
| 55 |
| 56 template <typename T> |
| 57 std::string GetArcServiceName(...) { |
| 58 return std::string(); |
| 59 } |
| 60 |
| 61 } // namespace internal |
| 62 |
| 25 // Manages creation and destruction of services that communicate with the ARC | 63 // Manages creation and destruction of services that communicate with the ARC |
| 26 // instance via the ArcBridgeService. | 64 // instance via the ArcBridgeService. |
| 27 class ArcServiceManager { | 65 class ArcServiceManager { |
| 28 public: | 66 public: |
| 29 class Observer { | 67 class Observer { |
| 30 public: | 68 public: |
| 31 // Called when ArcServiceManager is being shut down. Observer | 69 // Called when ArcServiceManager is being shut down. Observer |
| 32 // implementation should clean up ARC related stuff here. One of the | 70 // implementation should clean up ARC related stuff here. One of the |
| 33 // typical use cases is calling ArcServiceManager::RemoveObserver(). | 71 // typical use cases is calling ArcServiceManager::RemoveObserver(). |
| 34 virtual void OnArcShutdown() = 0; | 72 virtual void OnArcShutdown() = 0; |
| 35 | 73 |
| 36 // Called when intent filters are added or removed. | 74 // Called when intent filters are added or removed. |
| 37 virtual void OnIntentFiltersUpdated() = 0; | 75 virtual void OnIntentFiltersUpdated() = 0; |
| 38 | 76 |
| 39 protected: | 77 protected: |
| 40 virtual ~Observer() = default; | 78 virtual ~Observer() = default; |
| 41 }; | 79 }; |
| 42 | 80 |
| 43 explicit ArcServiceManager( | 81 explicit ArcServiceManager( |
| 44 scoped_refptr<base::TaskRunner> blocking_task_runner); | 82 scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 45 ~ArcServiceManager(); | 83 ~ArcServiceManager(); |
| 46 | 84 |
| 47 // |arc_bridge_service| can only be accessed on the thread that this | 85 // |arc_bridge_service| can only be accessed on the thread that this |
| 48 // class was created on. | 86 // class was created on. |
| 49 ArcBridgeService* arc_bridge_service(); | 87 ArcBridgeService* arc_bridge_service(); |
| 50 | 88 |
| 51 // Adds a service to the managed services list. | 89 // Adds a service to the managed services list. Returns false if another |
| 52 void AddService(std::unique_ptr<ArcService> service); | 90 // named service with that name had already been added. |
| 91 template <typename T> |
| 92 bool AddService(std::unique_ptr<T> service) { |
| 93 return AddServiceInternal(internal::GetArcServiceName<T>(nullptr), |
| 94 std::move(service)); |
| 95 } |
| 96 |
| 97 // Gets the named service from the managed services list. This uses SFINAE, so |
| 98 // you can only call this function if the service specified by T provides a |
| 99 // static member variable called kArcServiceName[] (otherwise this will not |
| 100 // compile). |
| 101 template <typename T> |
| 102 T* GetService() { |
| 103 return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName)); |
| 104 } |
| 53 | 105 |
| 54 // Gets the global instance of the ARC Service Manager. This can only be | 106 // Gets the global instance of the ARC Service Manager. This can only be |
| 55 // called on the thread that this class was created on. | 107 // called on the thread that this class was created on. |
| 56 static ArcServiceManager* Get(); | 108 static ArcServiceManager* Get(); |
| 57 | 109 |
| 58 void AddObserver(Observer* observer); | 110 void AddObserver(Observer* observer); |
| 59 void RemoveObserver(Observer* observer); | 111 void RemoveObserver(Observer* observer); |
| 60 | 112 |
| 61 // Called to shut down all ARC services. | 113 // Called to shut down all ARC services. |
| 62 void Shutdown(); | 114 void Shutdown(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 74 } | 126 } |
| 75 | 127 |
| 76 // Returns the IntentHelperObserver instance owned by ArcServiceManager. | 128 // Returns the IntentHelperObserver instance owned by ArcServiceManager. |
| 77 ArcIntentHelperObserver* intent_helper_observer() { | 129 ArcIntentHelperObserver* intent_helper_observer() { |
| 78 return intent_helper_observer_.get(); | 130 return intent_helper_observer_.get(); |
| 79 } | 131 } |
| 80 | 132 |
| 81 private: | 133 private: |
| 82 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. | 134 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. |
| 83 | 135 |
| 136 // Helper methods for AddService and GetService. |
| 137 bool AddServiceInternal(const std::string& name, |
| 138 std::unique_ptr<ArcService> service); |
| 139 ArcService* GetNamedServiceInternal(const std::string& name); |
| 140 |
| 84 base::ThreadChecker thread_checker_; | 141 base::ThreadChecker thread_checker_; |
| 85 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 142 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
| 86 | 143 |
| 87 // An object for observing the ArcIntentHelper instance in |services_|. | 144 // An object for observing the ArcIntentHelper instance in |services_|. |
| 88 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; | 145 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; |
| 89 | 146 |
| 90 std::unique_ptr<ArcBridgeService> arc_bridge_service_; | 147 std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
| 91 std::vector<std::unique_ptr<ArcService>> services_; | 148 std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_; |
| 92 scoped_refptr<ActivityIconLoader> icon_loader_; | 149 scoped_refptr<ActivityIconLoader> icon_loader_; |
| 93 scoped_refptr<LocalActivityResolver> activity_resolver_; | 150 scoped_refptr<LocalActivityResolver> activity_resolver_; |
| 94 | 151 |
| 95 base::ObserverList<Observer> observer_list_; | 152 base::ObserverList<Observer> observer_list_; |
| 96 | 153 |
| 97 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); | 154 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); |
| 98 }; | 155 }; |
| 99 | 156 |
| 100 } // namespace arc | 157 } // namespace arc |
| 101 | 158 |
| 102 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 159 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
| OLD | NEW |