summaryrefslogtreecommitdiffstats
path: root/mojo/shell
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-29 19:31:23 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-29 19:31:23 +0000
commit1124dea06ec4f030992f95ab653ab3759b694fde (patch)
treec0937830d343965eb1540010b22c8290b5c68c99 /mojo/shell
parent6ba332af6e2c3b5039bbf3ecd7aac2df11e82217 (diff)
downloadchromium_src-1124dea06ec4f030992f95ab653ab3759b694fde.zip
chromium_src-1124dea06ec4f030992f95ab653ab3759b694fde.tar.gz
chromium_src-1124dea06ec4f030992f95ab653ab3759b694fde.tar.bz2
Changes view manager test to connect via shell
Previously code was linking with ViewManager, now I'm connecting through the shell. As part of this I'm adding a general class that should make it easy to connect to any server through the shell. For the time being I'm making the shell explicitly link with the viewmanager. Eventually that'll have to change. BUG=365012 TEST=covered by tests R=davemoore@chromium.org Review URL: https://codereview.chromium.org/256133002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266937 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/shell')
-rw-r--r--mojo/shell/context.cc69
-rw-r--r--mojo/shell/run.cc1
-rw-r--r--mojo/shell/shell_test_helper.cc87
-rw-r--r--mojo/shell/shell_test_helper.h66
4 files changed, 218 insertions, 5 deletions
diff --git a/mojo/shell/context.cc b/mojo/shell/context.cc
index d5bb30e..86f06c3 100644
--- a/mojo/shell/context.cc
+++ b/mojo/shell/context.cc
@@ -4,10 +4,13 @@
#include "mojo/shell/context.h"
-#include "base/command_line.h"
#include "build/build_config.h"
+#include "base/command_line.h"
+#include "base/lazy_instance.h"
+#include "base/memory/scoped_vector.h"
#include "mojo/embedder/embedder.h"
#include "mojo/gles2/gles2_support_impl.h"
+#include "mojo/public/cpp/shell/application.h"
#include "mojo/service_manager/service_loader.h"
#include "mojo/service_manager/service_manager.h"
#include "mojo/services/native_viewport/native_viewport_service.h"
@@ -22,8 +25,62 @@
#include "mojo/shell/dbus_service_loader_linux.h"
#endif // defined(OS_LINUX)
+#if defined(USE_AURA)
+#include "mojo/services/view_manager/root_node_manager.h"
+#include "mojo/services/view_manager/view_manager_connection.h"
+#endif
+
namespace mojo {
namespace shell {
+namespace {
+
+// Used to ensure we only init once.
+class Setup {
+ public:
+ Setup() {
+ embedder::Init();
+ gles2::GLES2SupportImpl::Init();
+ }
+
+ ~Setup() {
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(Setup);
+};
+
+static base::LazyInstance<Setup> setup = LAZY_INSTANCE_INITIALIZER;
+
+#if defined(USE_AURA)
+class ViewManagerLoader : public ServiceLoader {
+ public:
+ ViewManagerLoader() {}
+ virtual ~ViewManagerLoader() {}
+
+ private:
+ virtual void LoadService(ServiceManager* manager,
+ const GURL& url,
+ ScopedShellHandle shell_handle) OVERRIDE {
+ scoped_ptr<Application> app(new Application(shell_handle.Pass()));
+ app->AddServiceConnector(
+ new ServiceConnector<services::view_manager::ViewManagerConnection,
+ services::view_manager::RootNodeManager>(
+ &root_node_manager_));
+ apps_.push_back(app.release());
+ }
+
+ virtual void OnServiceError(ServiceManager* manager,
+ const GURL& url) OVERRIDE {
+ }
+
+ services::view_manager::RootNodeManager root_node_manager_;
+ ScopedVector<Application> apps_;
+
+ DISALLOW_COPY_AND_ASSIGN(ViewManagerLoader);
+};
+#endif
+
+} // namespace
class Context::NativeViewportServiceLoader : public ServiceLoader {
public:
@@ -54,9 +111,7 @@ Context::Context()
task_runners_.cache_runner(),
scoped_ptr<net::NetworkDelegate>(new NetworkDelegate()),
storage_.profile_path()) {
- embedder::Init();
- gles2::GLES2SupportImpl::Init();
-
+ setup.Get();
CommandLine* cmdline = CommandLine::ForCurrentProcess();
scoped_ptr<DynamicServiceRunnerFactory> runner_factory;
if (cmdline->HasSwitch(switches::kEnableMultiprocess))
@@ -70,6 +125,12 @@ Context::Context()
service_manager_.SetLoaderForURL(
scoped_ptr<ServiceLoader>(new NativeViewportServiceLoader(this)),
GURL("mojo:mojo_native_viewport_service"));
+#if defined(USE_AURA)
+ // TODO(sky): need a better way to find this. It shouldn't be linked in.
+ service_manager_.SetLoaderForURL(
+ scoped_ptr<ServiceLoader>(new ViewManagerLoader()),
+ GURL("mojo:mojo_view_manager"));
+#endif
#if defined(OS_LINUX)
service_manager_.SetLoaderForScheme(
diff --git a/mojo/shell/run.cc b/mojo/shell/run.cc
index edc9c82..d032d7d 100644
--- a/mojo/shell/run.cc
+++ b/mojo/shell/run.cc
@@ -37,7 +37,6 @@ void Run(Context* context) {
ScopedMessagePipeHandle no_handle;
context->service_manager()->Connect(GURL(*it), no_handle.Pass());
}
- // TODO(davemoore): Currently we leak |service_manager|.
}
} // namespace shell
diff --git a/mojo/shell/shell_test_helper.cc b/mojo/shell/shell_test_helper.cc
new file mode 100644
index 0000000..c530140
--- /dev/null
+++ b/mojo/shell/shell_test_helper.cc
@@ -0,0 +1,87 @@
+// Copyright 2014 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.
+
+#include "mojo/shell/shell_test_helper.h"
+
+#include "base/command_line.h"
+#include "base/logging.h"
+#include "base/message_loop/message_loop.h"
+#include "mojo/shell/context.h"
+#include "mojo/shell/init.h"
+
+namespace mojo {
+namespace shell {
+
+// State used on the background thread. Be careful, this is created on the main
+// thread than passed to the shell thread. Destruction happens on the shell
+// thread.
+struct ShellTestHelper::State {
+ scoped_ptr<Context> context;
+ scoped_ptr<ServiceManager::TestAPI> test_api;
+ ScopedShellHandle shell_handle;
+};
+
+namespace {
+
+void StartShellOnShellThread(ShellTestHelper::State* state) {
+ state->context.reset(new Context);
+ state->test_api.reset(
+ new ServiceManager::TestAPI(state->context->service_manager()));
+ state->shell_handle = state->test_api->GetShellHandle();
+}
+
+} // namespace
+
+class ShellTestHelper::TestShellClient : public ShellClient {
+ public:
+ TestShellClient() {}
+ virtual ~TestShellClient() {}
+
+ // ShellClient:
+ virtual void AcceptConnection(
+ const mojo::String& url,
+ ScopedMessagePipeHandle client_handle) OVERRIDE {
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestShellClient);
+};
+
+ShellTestHelper::ShellTestHelper()
+ : shell_thread_("Test Shell Thread"),
+ state_(NULL) {
+ CommandLine::Init(0, NULL);
+ mojo::shell::InitializeLogging();
+}
+
+ShellTestHelper::~ShellTestHelper() {
+ if (state_) {
+ // |state_| contains data created on the background thread. Destroy it
+ // there so that there aren't any race conditions.
+ shell_thread_.message_loop()->DeleteSoon(FROM_HERE, state_);
+ state_ = NULL;
+ }
+}
+
+void ShellTestHelper::Init() {
+ DCHECK(!state_);
+ state_ = new State;
+ shell_thread_.Start();
+ shell_thread_.message_loop()->message_loop_proxy()->PostTaskAndReply(
+ FROM_HERE,
+ base::Bind(&StartShellOnShellThread, state_),
+ base::Bind(&ShellTestHelper::OnShellStarted, base::Unretained(this)));
+ run_loop_.reset(new base::RunLoop);
+ run_loop_->Run();
+}
+
+void ShellTestHelper::OnShellStarted() {
+ DCHECK(state_);
+ shell_client_.reset(new TestShellClient);
+ shell_.reset(state_->shell_handle.Pass(), shell_client_.get());
+ run_loop_->Quit();
+}
+
+} // namespace shell
+} // namespace mojo
diff --git a/mojo/shell/shell_test_helper.h b/mojo/shell/shell_test_helper.h
new file mode 100644
index 0000000..53d84a3
--- /dev/null
+++ b/mojo/shell/shell_test_helper.h
@@ -0,0 +1,66 @@
+// Copyright 2014 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 MOJO_SHELL_SHELL_TEST_HELPER_
+#define MOJO_SHELL_SHELL_TEST_HELPER_
+
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "base/threading/thread.h"
+#include "mojo/public/cpp/bindings/remote_ptr.h"
+#include "mojo/public/cpp/environment/environment.h"
+#include "mojo/public/interfaces/shell/shell.mojom.h"
+
+namespace base {
+class MessageLoopProxy;
+class RunLoop;
+}
+
+namespace mojo {
+namespace shell {
+
+// ShellTestHelper is useful for tests to establish a connection to the Shell.
+// ShellTestHelper does this by spawning a thread and connecting. Invoke Init()
+// to do this. Once done, shell() returns the handle to the Shell.
+class ShellTestHelper {
+ public:
+ struct State;
+
+ ShellTestHelper();
+ ~ShellTestHelper();
+
+ void Init();
+
+ // Returns a handle to the Shell. ShellTestHelper owns the shell.
+ Shell* shell() { return shell_.get(); }
+
+ private:
+ class TestShellClient;
+
+ // Invoked once connection has been established.
+ void OnShellStarted();
+
+ Environment environment_;
+
+ base::Thread shell_thread_;
+
+ // If non-null we're in Init() and waiting for connection.
+ scoped_ptr<base::RunLoop> run_loop_;
+
+ // See comment in declaration for details.
+ State* state_;
+
+ // Client interface for the shell.
+ scoped_ptr<TestShellClient> shell_client_;
+
+ RemotePtr<Shell> shell_;
+
+ DISALLOW_COPY_AND_ASSIGN(ShellTestHelper);
+};
+
+} // namespace shell
+} // namespace mojo
+
+#endif // MOJO_SHELL_SHELL_TEST_HELPER_