Commit 85403e29 authored by Wez's avatar Wez Committed by Commit Bot

[Fuchsia] Add ScopedDefaultComponentContext helper.

This can be used to replace the current default ComponentContext with
one backed by a supplied |service_root| channel, e.g. for testing.

Bug: 893229
Change-Id: Ib08caf7527066c5b269d756a1726e1843c6431f5
Reviewed-on: https://chromium-review.googlesource.com/c/1333113
Commit-Queue: Wez <wez@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607788}
parent e349a45e
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <utility> #include <utility>
#include "base/fuchsia/fuchsia_logging.h" #include "base/fuchsia/fuchsia_logging.h"
#include "base/logging.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
namespace base { namespace base {
...@@ -27,6 +28,13 @@ zx::channel ConnectToServiceRoot() { ...@@ -27,6 +28,13 @@ zx::channel ConnectToServiceRoot() {
return client_channel; return client_channel;
} }
std::unique_ptr<ComponentContext>* DefaultComponentContext() {
static base::NoDestructor<std::unique_ptr<ComponentContext>>
component_context_ptr(
std::make_unique<ComponentContext>(ConnectToServiceRoot()));
return component_context_ptr.get();
}
} // namespace } // namespace
ComponentContext::ComponentContext(zx::channel service_root) ComponentContext::ComponentContext(zx::channel service_root)
...@@ -38,9 +46,7 @@ ComponentContext::~ComponentContext() = default; ...@@ -38,9 +46,7 @@ ComponentContext::~ComponentContext() = default;
// static // static
ComponentContext* ComponentContext::GetDefault() { ComponentContext* ComponentContext::GetDefault() {
static base::NoDestructor<ComponentContext> component_context( return DefaultComponentContext()->get();
ConnectToServiceRoot());
return component_context.get();
} }
zx_status_t ComponentContext::ConnectToService(FidlInterfaceRequest request) { zx_status_t ComponentContext::ConnectToService(FidlInterfaceRequest request) {
...@@ -49,5 +55,18 @@ zx_status_t ComponentContext::ConnectToService(FidlInterfaceRequest request) { ...@@ -49,5 +55,18 @@ zx_status_t ComponentContext::ConnectToService(FidlInterfaceRequest request) {
request.TakeChannel().release()); request.TakeChannel().release());
} }
ScopedDefaultComponentContext::ScopedDefaultComponentContext(
zx::channel service_root)
: old_context_(std::move(*DefaultComponentContext())) {
*DefaultComponentContext() =
std::make_unique<ComponentContext>(std::move(service_root));
context_ = DefaultComponentContext()->get();
}
ScopedDefaultComponentContext::~ScopedDefaultComponentContext() {
DCHECK_EQ(DefaultComponentContext()->get(), context_);
*DefaultComponentContext() = std::move(old_context_);
}
} // namespace fuchsia } // namespace fuchsia
} // namespace base } // namespace base
...@@ -61,6 +61,20 @@ class BASE_EXPORT ComponentContext { ...@@ -61,6 +61,20 @@ class BASE_EXPORT ComponentContext {
DISALLOW_COPY_AND_ASSIGN(ComponentContext); DISALLOW_COPY_AND_ASSIGN(ComponentContext);
}; };
// Replaces the default ComponentContext with the supplied |service_root|, and
// restores it when going out-of-scope.
class BASE_EXPORT ScopedDefaultComponentContext {
public:
ScopedDefaultComponentContext(zx::channel service_root);
~ScopedDefaultComponentContext();
private:
ComponentContext* context_;
std::unique_ptr<ComponentContext> old_context_;
DISALLOW_COPY_AND_ASSIGN(ScopedDefaultComponentContext);
};
} // namespace fuchsia } // namespace fuchsia
} // namespace base } // namespace base
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment