Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: components/arc/arc_service_manager.h

Issue 2622843002: arc: Provide a per-service getter for ArcServiceManager (Closed)
Patch Set: Addressed feedback Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // In order to avoid collisions, kArcServiceName should be the fully-qualified
hidehiko 2017/01/11 05:17:45 So, as you explicitly specify the template typenam
Luis Héctor Chávez 2017/01/11 16:01:36 Done.
44 // name of the class.
45 template <typename T>
46 decltype(T::kArcServiceName, std::string()) GetArcServiceName(T* unused) {
47 if (strlen(T::kArcServiceName) == 0)
hidehiko 2017/01/11 05:17:45 Optional: or, static_assert for compile time check
Luis Héctor Chávez 2017/01/11 16:01:36 Tried that before, compiler didn't like it since T
48 LOG(ERROR) << "kArcServiceName[] should be a fully-qualified class name.";
49 return T::kArcServiceName;
50 }
51
52 template <typename T>
53 std::string GetArcServiceName(...) {
54 return std::string();
55 }
56
57 } // namespace internal
58
25 // Manages creation and destruction of services that communicate with the ARC 59 // Manages creation and destruction of services that communicate with the ARC
26 // instance via the ArcBridgeService. 60 // instance via the ArcBridgeService.
27 class ArcServiceManager { 61 class ArcServiceManager {
28 public: 62 public:
29 class Observer { 63 class Observer {
30 public: 64 public:
31 // Called when ArcServiceManager is being shut down. Observer 65 // Called when ArcServiceManager is being shut down. Observer
32 // implementation should clean up ARC related stuff here. One of the 66 // implementation should clean up ARC related stuff here. One of the
33 // typical use cases is calling ArcServiceManager::RemoveObserver(). 67 // typical use cases is calling ArcServiceManager::RemoveObserver().
34 virtual void OnArcShutdown() = 0; 68 virtual void OnArcShutdown() = 0;
35 69
36 // Called when intent filters are added or removed. 70 // Called when intent filters are added or removed.
37 virtual void OnIntentFiltersUpdated() = 0; 71 virtual void OnIntentFiltersUpdated() = 0;
38 72
39 protected: 73 protected:
40 virtual ~Observer() = default; 74 virtual ~Observer() = default;
41 }; 75 };
42 76
43 explicit ArcServiceManager( 77 explicit ArcServiceManager(
44 scoped_refptr<base::TaskRunner> blocking_task_runner); 78 scoped_refptr<base::TaskRunner> blocking_task_runner);
45 ~ArcServiceManager(); 79 ~ArcServiceManager();
46 80
47 // |arc_bridge_service| can only be accessed on the thread that this 81 // |arc_bridge_service| can only be accessed on the thread that this
48 // class was created on. 82 // class was created on.
49 ArcBridgeService* arc_bridge_service(); 83 ArcBridgeService* arc_bridge_service();
50 84
51 // Adds a service to the managed services list. 85 // Adds a service to the managed services list. Returns false if another
52 void AddService(std::unique_ptr<ArcService> service); 86 // named service with that name had already been added.
87 template <typename T>
88 bool AddService(std::unique_ptr<T> service) {
89 return AddServiceInternal(internal::GetArcServiceName<T>(nullptr),
90 std::move(service));
91 }
92
93 // Gets the named service from the managed services list. This uses SFINAE, so
94 // you can only call this function if the service specified by T provides a
95 // static member variable called kArcServiceName[] (otherwise this will not
96 // compile).
97 template <typename T>
98 T* GetService() {
99 return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName));
100 }
53 101
54 // Gets the global instance of the ARC Service Manager. This can only be 102 // Gets the global instance of the ARC Service Manager. This can only be
55 // called on the thread that this class was created on. 103 // called on the thread that this class was created on.
56 static ArcServiceManager* Get(); 104 static ArcServiceManager* Get();
57 105
58 void AddObserver(Observer* observer); 106 void AddObserver(Observer* observer);
59 void RemoveObserver(Observer* observer); 107 void RemoveObserver(Observer* observer);
60 108
61 // Called to shut down all ARC services. 109 // Called to shut down all ARC services.
62 void Shutdown(); 110 void Shutdown();
(...skipping 11 matching lines...) Expand all
74 } 122 }
75 123
76 // Returns the IntentHelperObserver instance owned by ArcServiceManager. 124 // Returns the IntentHelperObserver instance owned by ArcServiceManager.
77 ArcIntentHelperObserver* intent_helper_observer() { 125 ArcIntentHelperObserver* intent_helper_observer() {
78 return intent_helper_observer_.get(); 126 return intent_helper_observer_.get();
79 } 127 }
80 128
81 private: 129 private:
82 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc. 130 class IntentHelperObserverImpl; // implemented in arc_service_manager.cc.
83 131
132 // Helper methods for AddService and GetService.
133 bool AddServiceInternal(const std::string& name,
134 std::unique_ptr<ArcService> service);
135 ArcService* GetNamedServiceInternal(const std::string& name);
136
84 base::ThreadChecker thread_checker_; 137 base::ThreadChecker thread_checker_;
85 scoped_refptr<base::TaskRunner> blocking_task_runner_; 138 scoped_refptr<base::TaskRunner> blocking_task_runner_;
86 139
87 // An object for observing the ArcIntentHelper instance in |services_|. 140 // An object for observing the ArcIntentHelper instance in |services_|.
88 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_; 141 std::unique_ptr<ArcIntentHelperObserver> intent_helper_observer_;
89 142
90 std::unique_ptr<ArcBridgeService> arc_bridge_service_; 143 std::unique_ptr<ArcBridgeService> arc_bridge_service_;
91 std::vector<std::unique_ptr<ArcService>> services_; 144 std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_;
92 scoped_refptr<ActivityIconLoader> icon_loader_; 145 scoped_refptr<ActivityIconLoader> icon_loader_;
93 scoped_refptr<LocalActivityResolver> activity_resolver_; 146 scoped_refptr<LocalActivityResolver> activity_resolver_;
94 147
95 base::ObserverList<Observer> observer_list_; 148 base::ObserverList<Observer> observer_list_;
96 149
97 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager); 150 DISALLOW_COPY_AND_ASSIGN(ArcServiceManager);
98 }; 151 };
99 152
100 } // namespace arc 153 } // namespace arc
101 154
102 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_ 155 #endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698