summaryrefslogtreecommitdiffstats
path: root/mojo/shell
diff options
context:
space:
mode:
authorrockot <rockot@chromium.org>2015-06-04 17:30:52 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-05 00:32:18 +0000
commitb814a5850da5aa473ad526eef4b41a82b05037a0 (patch)
treed1730ed4fccfc7fda63bd51f30891a95e61ba915 /mojo/shell
parent87c39e56c03c089751e4ae22dcea9ca7cf17c741 (diff)
downloadchromium_src-b814a5850da5aa473ad526eef4b41a82b05037a0.zip
chromium_src-b814a5850da5aa473ad526eef4b41a82b05037a0.tar.gz
chromium_src-b814a5850da5aa473ad526eef4b41a82b05037a0.tar.bz2
Embed a mojo ApplicationManager in content/browser
This embeds mojo/shell's ApplicationManager in content/browser and provides a way for arbitrary browser code to connect to Mojo apps as if the browser itself were a Mojo app. This is a basic implementation of Mojo app support which only loads static apps either in the browser process or a (per-app) utility process. Future CLs will address connection to apps from arbitrary render frames (i.e. connection requests which include the requestor's origin) as well as refactoring the utility process code further so that it serves strictly as a Mojo app runner. BUG=492422 Review URL: https://codereview.chromium.org/1149833007 Cr-Commit-Position: refs/heads/master@{#332974}
Diffstat (limited to 'mojo/shell')
-rw-r--r--mojo/shell/BUILD.gn2
-rw-r--r--mojo/shell/static_application_loader.cc95
-rw-r--r--mojo/shell/static_application_loader.h69
3 files changed, 166 insertions, 0 deletions
diff --git a/mojo/shell/BUILD.gn b/mojo/shell/BUILD.gn
index 331af9a..2e599b6f 100644
--- a/mojo/shell/BUILD.gn
+++ b/mojo/shell/BUILD.gn
@@ -26,6 +26,8 @@ source_set("shell") {
"query_util.h",
"shell_impl.cc",
"shell_impl.h",
+ "static_application_loader.cc",
+ "static_application_loader.h",
"switches.cc",
"switches.h",
]
diff --git a/mojo/shell/static_application_loader.cc b/mojo/shell/static_application_loader.cc
new file mode 100644
index 0000000..a0f90a8
--- /dev/null
+++ b/mojo/shell/static_application_loader.cc
@@ -0,0 +1,95 @@
+// 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.
+
+#include "mojo/shell/static_application_loader.h"
+
+#include "base/bind.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/task_runner.h"
+#include "base/thread_task_runner_handle.h"
+#include "base/threading/simple_thread.h"
+#include "mojo/application/public/cpp/application_delegate.h"
+#include "mojo/application/public/cpp/application_runner.h"
+#include "mojo/application/public/interfaces/application.mojom.h"
+#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
+
+namespace mojo {
+namespace shell {
+
+namespace {
+
+class RunnerThread : public base::SimpleThread {
+ public:
+ RunnerThread(const GURL& url,
+ InterfaceRequest<Application> request,
+ scoped_refptr<base::TaskRunner> exit_task_runner,
+ const base::Closure& exit_callback,
+ const StaticApplicationLoader::ApplicationFactory& factory)
+ : base::SimpleThread("Mojo Application: " + url.spec()),
+ request_(request.Pass()),
+ exit_task_runner_(exit_task_runner),
+ exit_callback_(exit_callback),
+ factory_(factory) {}
+
+ void Run() override {
+ scoped_ptr<ApplicationRunner> runner(
+ new ApplicationRunner(factory_.Run().release()));
+ runner->Run(request_.PassMessagePipe().release().value(),
+ false /* init_base */);
+ exit_task_runner_->PostTask(FROM_HERE, exit_callback_);
+ }
+
+ private:
+ InterfaceRequest<Application> request_;
+ scoped_refptr<base::TaskRunner> exit_task_runner_;
+ base::Closure exit_callback_;
+ StaticApplicationLoader::ApplicationFactory factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(RunnerThread);
+};
+
+} // namespace
+
+StaticApplicationLoader::StaticApplicationLoader(
+ const ApplicationFactory& factory)
+ : StaticApplicationLoader(factory, base::Closure()) {
+}
+
+StaticApplicationLoader::StaticApplicationLoader(
+ const ApplicationFactory& factory,
+ const base::Closure& quit_callback)
+ : factory_(factory), quit_callback_(quit_callback), weak_factory_(this) {
+}
+
+StaticApplicationLoader::~StaticApplicationLoader() {
+ if (thread_)
+ StopAppThread();
+}
+
+void StaticApplicationLoader::Load(const GURL& url,
+ InterfaceRequest<Application> request) {
+ if (thread_)
+ return;
+
+ // If the application's thread quits on its own before this loader dies, we
+ // reset the Thread object, allowing future Load requests to be fulfilled
+ // with a new app instance.
+ auto exit_callback = base::Bind(&StaticApplicationLoader::StopAppThread,
+ weak_factory_.GetWeakPtr());
+ thread_.reset(
+ new RunnerThread(url, request.Pass(), base::ThreadTaskRunnerHandle::Get(),
+ exit_callback, factory_));
+ thread_->Start();
+}
+
+void StaticApplicationLoader::StopAppThread() {
+ thread_->Join();
+ thread_.reset();
+ if (!quit_callback_.is_null())
+ quit_callback_.Run();
+}
+
+} // namespace shell
+} // namespace mojo
diff --git a/mojo/shell/static_application_loader.h b/mojo/shell/static_application_loader.h
new file mode 100644
index 0000000..cbf611a
--- /dev/null
+++ b/mojo/shell/static_application_loader.h
@@ -0,0 +1,69 @@
+// 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 MOJO_SHELL_STATIC_APPLICATION_LOADER_H_
+#define MOJO_SHELL_STATIC_APPLICATION_LOADER_H_
+
+#include <list>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
+#include "mojo/shell/application_loader.h"
+
+namespace base {
+class SimpleThread;
+}
+
+namespace mojo {
+class ApplicationDelegate;
+}
+
+namespace mojo {
+namespace shell {
+
+// An ApplicationLoader which loads a single type of app from a given
+// ApplicationDelegate factory. A Load() request is fulfilled by creating an
+// instance of the app on a new thread. Only one instance of the app will run at
+// a time. Any Load requests received while the app is running will be dropped.
+class StaticApplicationLoader : public mojo::shell::ApplicationLoader {
+ public:
+ using ApplicationFactory =
+ base::Callback<scoped_ptr<mojo::ApplicationDelegate>()>;
+
+ // Constructs a static loader for |factory|.
+ explicit StaticApplicationLoader(const ApplicationFactory& factory);
+
+ // Constructs a static loader for |factory| with a closure that will be called
+ // when the loaded application quits.
+ StaticApplicationLoader(const ApplicationFactory& factory,
+ const base::Closure& quit_callback);
+
+ ~StaticApplicationLoader() override;
+
+ // mojo::shell::ApplicationLoader:
+ void Load(const GURL& url,
+ mojo::InterfaceRequest<mojo::Application> request) override;
+
+ private:
+ void StopAppThread();
+
+ // The factory used t create new instances of the application delegate.
+ ApplicationFactory factory_;
+
+ // If not null, this is run when the loaded application quits.
+ base::Closure quit_callback_;
+
+ // Thread for the application if currently running.
+ scoped_ptr<base::SimpleThread> thread_;
+
+ base::WeakPtrFactory<StaticApplicationLoader> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(StaticApplicationLoader);
+};
+
+} // namespace shell
+} // namespace mojo
+
+#endif // MOJO_SHELL_STATIC_APPLICATION_LOADER_H_