Chromium Code Reviews| 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 Name[]; | |
|
Yusuke Sato
2017/01/10 07:11:11
The variable name 'Name' doesn't seem to follow th
Luis Héctor Chávez
2017/01/10 18:41:55
Done.
| |
| 36 // ... | |
| 37 // }; | |
| 38 // | |
| 39 // it can then be retrieved from ArcServiceManager in a type-safe way using | |
| 40 // Get<T>(). This struct allows the use of std::enable_if<...> to choose the | |
| 41 // correct implementation of the AddService() functions to enable this | |
| 42 // functionality. | |
| 43 template <typename T> | |
|
hidehiko
2017/01/10 08:03:12
Optional: How about minimizing template magic? E.g
Luis Héctor Chávez
2017/01/10 18:41:56
Simplified the first function a bit, but done.
| |
| 44 struct NamedServiceTraits { | |
| 45 private: | |
| 46 typedef char No[1]; | |
|
Yusuke Sato
2017/01/10 07:11:11
nit: do we have a reason to skip 0? No can be [0]
hidehiko
2017/01/10 08:03:12
FYI: 0 length is illegal, IIUC.
http://stackoverfl
Luis Héctor Chávez
2017/01/10 18:41:56
This is a commonly-used Method Detector idiom: htt
hidehiko
2017/01/11 05:17:45
Yes, I know, but it was developed in C++03 era, an
| |
| 47 typedef char Yes[2]; | |
| 48 | |
| 49 template <typename U> | |
| 50 static Yes& test(decltype(U::Name) *); | |
|
Yusuke Sato
2017/01/10 07:11:11
nit: no space before *, maybe?
hidehiko
2017/01/10 08:03:12
Optional: Even in this way, trailing return type c
Luis Héctor Chávez
2017/01/10 18:41:56
Acknowledged.
| |
| 51 | |
| 52 template <typename U> | |
| 53 static No& test(...); | |
| 54 | |
| 55 public: | |
| 56 enum : bool { HasName = sizeof(test<T>(nullptr)) == sizeof(Yes) }; | |
|
hidehiko
2017/01/10 08:03:12
With std::{true,false}_type,
constexpr bool kValu
Luis Héctor Chávez
2017/01/10 18:41:56
Acknowledged.
| |
| 57 }; | |
| 58 | |
| 59 } // namespace internal | |
| 60 | |
| 25 // Manages creation and destruction of services that communicate with the ARC | 61 // Manages creation and destruction of services that communicate with the ARC |
| 26 // instance via the ArcBridgeService. | 62 // instance via the ArcBridgeService. |
| 27 class ArcServiceManager { | 63 class ArcServiceManager { |
| 28 public: | 64 public: |
| 29 class Observer { | 65 class Observer { |
| 30 public: | 66 public: |
| 31 // Called when ArcServiceManager is being shut down. Observer | 67 // Called when ArcServiceManager is being shut down. Observer |
| 32 // implementation should clean up ARC related stuff here. One of the | 68 // implementation should clean up ARC related stuff here. One of the |
| 33 // typical use cases is calling ArcServiceManager::RemoveObserver(). | 69 // typical use cases is calling ArcServiceManager::RemoveObserver(). |
| 34 virtual void OnArcShutdown() = 0; | 70 virtual void OnArcShutdown() = 0; |
| 35 | 71 |
| 36 // Called when intent filters are added or removed. | 72 // Called when intent filters are added or removed. |
| 37 virtual void OnIntentFiltersUpdated() = 0; | 73 virtual void OnIntentFiltersUpdated() = 0; |
| 38 | 74 |
| 39 protected: | 75 protected: |
| 40 virtual ~Observer() = default; | 76 virtual ~Observer() = default; |
| 41 }; | 77 }; |
| 42 | 78 |
| 43 explicit ArcServiceManager( | 79 explicit ArcServiceManager( |
| 44 scoped_refptr<base::TaskRunner> blocking_task_runner); | 80 scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 45 ~ArcServiceManager(); | 81 ~ArcServiceManager(); |
| 46 | 82 |
| 47 // |arc_bridge_service| can only be accessed on the thread that this | 83 // |arc_bridge_service| can only be accessed on the thread that this |
| 48 // class was created on. | 84 // class was created on. |
| 49 ArcBridgeService* arc_bridge_service(); | 85 ArcBridgeService* arc_bridge_service(); |
| 50 | 86 |
| 51 // Adds a service to the managed services list. | 87 // Adds a service to the managed services list. |
| 52 void AddService(std::unique_ptr<ArcService> service); | 88 template <typename T, |
| 89 typename std::enable_if<!internal::NamedServiceTraits<T>::HasName, | |
| 90 int>::type = 0> | |
| 91 void AddService(std::unique_ptr<T> service) { | |
| 92 AddServiceInternal(std::move(service)); | |
| 93 } | |
| 94 | |
| 95 // Adds a named service to the managed services list. | |
| 96 template <typename T, | |
| 97 typename std::enable_if<internal::NamedServiceTraits<T>::HasName, | |
| 98 int>::type = 0> | |
| 99 void AddService(std::unique_ptr<T> service) { | |
| 100 AddNamedServiceInternal(std::string(T::Name), std::move(service)); | |
|
Yusuke Sato
2017/01/10 07:11:11
nit: the cast (to string) seems unnecessary
Luis Héctor Chávez
2017/01/10 18:41:56
Done.
| |
| 101 } | |
| 102 | |
| 103 // Gets the named service from the managed services list. | |
|
Yusuke Sato
2017/01/10 07:11:11
For developers who don't want to read the template
hidehiko
2017/01/10 08:03:12
Also, could you explicitly comment that, this will
Luis Héctor Chávez
2017/01/10 18:41:56
Done.
| |
| 104 template <typename T> | |
| 105 T* GetService() { | |
|
Yusuke Sato
2017/01/10 07:11:11
I think GetService<T>() is easy to test. How about
Luis Héctor Chávez
2017/01/10 18:41:56
Done.
| |
| 106 return static_cast<T*>(GetNamedServiceInternal(T::Name)); | |
| 107 } | |
| 53 | 108 |
| 54 // Gets the global instance of the ARC Service Manager. This can only be | 109 // Gets the global instance of the ARC Service Manager. This can only be |
| 55 // called on the thread that this class was created on. | 110 // called on the thread that this class was created on. |
| 56 static ArcServiceManager* Get(); | 111 static ArcServiceManager* Get(); |
| 57 | 112 |
| 58 void AddObserver(Observer* observer); | 113 void AddObserver(Observer* observer); |
| 59 void RemoveObserver(Observer* observer); | 114 void RemoveObserver(Observer* observer); |
| 60 | 115 |
| 61 // Called to shut down all ARC services. | 116 // Called to shut down all ARC services. |
| 62 void Shutdown(); | 117 void Shutdown(); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 74 } | 129 } |
| 75 | 130 |
| 76 // Returns the IntentHelperObserver instance owned by ArcServiceManager. | 131 // Returns the IntentHelperObserver instance owned by ArcServiceManager. |
| 77 ArcIntentHelperObserver* intent_helper_observer() { | 132 ArcIntentHelperObserver* intent_helper_observer() { |
| 78 return intent_helper_observer_.get(); | 133 return intent_helper_observer_.get(); |
| 79 } | 134 } |
| 80 | 135 |
| 81 private: | 136 private: |
| 82 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. | 137 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. |
| 83 | 138 |
| 139 // Helper methods for AddService and GetService. | |
| 140 void AddServiceInternal(std::unique_ptr<ArcService> service); | |
| 141 void AddNamedServiceInternal(const std::string& name, | |
| 142 std::unique_ptr<ArcService> service); | |
| 143 ArcService* GetNamedServiceInternal(const std::string& name); | |
| 144 | |
| 84 base::ThreadChecker thread_checker_; | 145 base::ThreadChecker thread_checker_; |
| 85 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 146 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
| 86 | 147 |
| 87 // An object for observing the ArcIntentHelper instance in |services_|. | 148 // An object for observing the ArcIntentHelper instance in |services_|. |
| 88 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; | 149 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; |
| 89 | 150 |
| 90 std::unique_ptr<ArcBridgeService> arc_bridge_service_; | 151 std::unique_ptr<ArcBridgeService> arc_bridge_service_; |
| 91 std::vector<std::unique_ptr<ArcService>> services_; | 152 std::vector<std::unique_ptr<ArcService>> services_; |
| 153 std::unordered_map<std::string, std::unique_ptr<ArcService>> named_services_; | |
|
hidehiko
2017/01/10 08:03:12
Optional: how about mergine services_ and named_se
Luis Héctor Chávez
2017/01/10 18:41:56
Done.
| |
| 92 scoped_refptr<ActivityIconLoader> icon_loader_; | 154 scoped_refptr<ActivityIconLoader> icon_loader_; |
| 93 scoped_refptr<LocalActivityResolver> activity_resolver_; | 155 scoped_refptr<LocalActivityResolver> activity_resolver_; |
| 94 | 156 |
| 95 base::ObserverList<Observer> observer_list_; | 157 base::ObserverList<Observer> observer_list_; |
| 96 | 158 |
| 97 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); | 159 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); |
| 98 }; | 160 }; |
| 99 | 161 |
| 100 } // namespace arc | 162 } // namespace arc |
| 101 | 163 |
| 102 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ | 164 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ |
| OLD | NEW |