blob: 7d540a0c781dd4e4456285beac8a1b4b8624e8b7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_
#define COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/task_runner.h"
#include "base/threading/thread_checker.h"
#include "components/arc/arc_service.h"
#include "components/arc/intent_helper/local_activity_resolver.h"
namespace arc {
class ArcBridgeService;
namespace internal {
// If an ArcService is declared with a name, e.g.:
//
// class MyArcService : public ArcService {
// public:
// static const char kArcServiceName[];
// ...
// };
//
// it can then be retrieved from ArcServiceManager in a type-safe way using
// GetService<T>(). This two functions allow AddService() to get the name only
// if it was provided, or use an empty string otherwise.
//
// Although the typename is always specified explicitly by the caller, the
// parameter is required in order for SFINAE to work correctly. It is not used
// and can be nullptr, though.
//
// In order to avoid collisions, kArcServiceName should be the fully-qualified
// name of the class.
template <typename T>
decltype(T::kArcServiceName, std::string()) GetArcServiceName(T* unused) {
if (strlen(T::kArcServiceName) == 0)
LOG(ERROR) << "kArcServiceName[] should be a fully-qualified class name.";
return T::kArcServiceName;
}
template <typename T>
std::string GetArcServiceName(...) {
return std::string();
}
} // namespace internal
// Manages creation and destruction of services that communicate with the ARC
// instance via the ArcBridgeService.
class ArcServiceManager {
public:
explicit ArcServiceManager(
scoped_refptr<base::TaskRunner> blocking_task_runner);
~ArcServiceManager();
// |arc_bridge_service| can only be accessed on the thread that this
// class was created on.
ArcBridgeService* arc_bridge_service();
// Adds a service to the managed services list. Returns false if another
// named service with that name had already been added.
template <typename T>
bool AddService(std::unique_ptr<T> service) {
return AddServiceInternal(internal::GetArcServiceName<T>(nullptr),
std::move(service));
}
// Gets the named service from the managed services list. This uses SFINAE, so
// you can only call this function if the service specified by T provides a
// static member variable called kArcServiceName[] (otherwise this will not
// compile).
template <typename T>
T* GetService() {
return static_cast<T*>(GetNamedServiceInternal(T::kArcServiceName));
}
// Does the same as GetService(), but with the global instance. Return nullptr
// when the instance hasn't been created or has already been destructed.
template <typename T> static T* GetGlobalService() {
auto* service_manager = ArcServiceManager::Get();
if (!service_manager)
return nullptr;
return service_manager->GetService<T>();
}
// Gets the global instance of the ARC Service Manager. This can only be
// called on the thread that this class was created on.
static ArcServiceManager* Get();
// Called to shut down all ARC services.
void Shutdown();
scoped_refptr<base::TaskRunner> blocking_task_runner() const {
return blocking_task_runner_;
}
// Returns the activity resolver owned by ArcServiceManager.
scoped_refptr<LocalActivityResolver> activity_resolver() {
return activity_resolver_;
}
private:
class IntentHelperObserverImpl; // implemented in arc_service_manager.cc.
// Helper methods for AddService and GetService.
bool AddServiceInternal(const std::string& name,
std::unique_ptr<ArcService> service);
ArcService* GetNamedServiceInternal(const std::string& name);
base::ThreadChecker thread_checker_;
scoped_refptr<base::TaskRunner> blocking_task_runner_;
std::unique_ptr<ArcBridgeService> arc_bridge_service_;
std::unordered_multimap<std::string, std::unique_ptr<ArcService>> services_;
scoped_refptr<LocalActivityResolver> activity_resolver_;
DISALLOW_COPY_AND_ASSIGN(ArcServiceManager);
};
} // namespace arc
#endif // COMPONENTS_ARC_ARC_SERVICE_MANAGER_H_
|