Skip to content

Commit

Permalink
libbinderwrapper: Add BinderWrapper::GetOrCreateInstance()
Browse files Browse the repository at this point in the history
In libraries relying on binder it is useful to be able to obtain an
existing copy of BinderWrapper or create one if it doesn't exist.

This allows to abstract the actual RPC (binder) from clients who have
no other dependencies on this RPC.

BUG: 23782171
Change-Id: Ie775d3d8ab83d75e38abc7e1385eb39a363555ef
  • Loading branch information
Alex Vakulenko committed Jan 4, 2016
1 parent 5b7563a commit 08c9891
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/binderwrapper/binder_wrapper.h
Expand Up @@ -30,6 +30,7 @@ class BBinder;
class IBinder;

// Wraps libbinder to make it testable.
// NOTE: Static methods of this class are not thread-safe.
class BinderWrapper {
public:
virtual ~BinderWrapper() {}
Expand All @@ -50,6 +51,10 @@ class BinderWrapper {
// InitForTesting().
static BinderWrapper* Get();

// Returns the singleton instance if it was previously created by Create() or
// set by InitForTesting(), or creates a new one by calling Create().
static BinderWrapper* GetOrCreateInstance();

// Gets the binder for communicating with the service identified by
// |service_name|, returning null immediately if it doesn't exist.
virtual sp<IBinder> GetService(const std::string& service_name) = 0;
Expand Down
7 changes: 7 additions & 0 deletions libbinderwrapper/binder_wrapper.cc
Expand Up @@ -50,4 +50,11 @@ BinderWrapper* BinderWrapper::Get() {
return instance_;
}

// static
BinderWrapper* BinderWrapper::GetOrCreateInstance() {
if (!instance_)
instance_ = new RealBinderWrapper();
return instance_;
}

} // namespace android

0 comments on commit 08c9891

Please sign in to comment.