Index: mojo/application/content_handler.cc |
diff --git a/mojo/application/content_handler.cc b/mojo/application/content_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e9fb0dab8e32a888ad4eab38015e0b1bb1ea889d |
--- /dev/null |
+++ b/mojo/application/content_handler.cc |
@@ -0,0 +1,144 @@ |
+// 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/application/content_handler.h" |
+ |
+#include <set> |
+ |
+#include "base/bind.h" |
+#include "base/threading/platform_thread.h" |
+#include "mojo/application/application_runner_chromium.h" |
+#include "mojo/common/message_pump_mojo.h" |
+#include "mojo/public/cpp/application/application_connection.h" |
+#include "mojo/public/cpp/application/application_delegate.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/interface_factory_impl.h" |
+#include "mojo/public/cpp/bindings/interface_impl.h" |
+#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h" |
+ |
+namespace mojo { |
+ |
+namespace { |
+ |
+class ApplicationThread : public base::PlatformThread::Delegate { |
+ public: |
+ ApplicationThread( |
+ scoped_refptr<base::MessageLoopProxy> handler_thread, |
+ const base::Callback<void(ApplicationThread*)>& termination_callback, |
+ mojo::ContentHandlerDelegate* handler_delegate, |
+ ShellPtr shell, |
+ URLResponsePtr response) |
+ : handler_thread_(handler_thread), |
+ termination_callback_(termination_callback), |
+ handler_delegate_(handler_delegate), |
+ shell_(shell.Pass()), |
+ response_(response.Pass()) {} |
+ |
+ private: |
+ void ThreadMain() override { |
+ base::MessageLoop loop(common::MessagePumpMojo::Create()); |
+ application_ = |
+ handler_delegate_->CreateApplication(shell_.Pass(), response_.Pass()); |
+ loop.Run(); |
+ handler_thread_->PostTask(FROM_HERE, |
+ base::Bind(termination_callback_, this)); |
+ } |
+ |
+ scoped_refptr<base::MessageLoopProxy> handler_thread_; |
+ base::Callback<void(ApplicationThread*)> termination_callback_; |
+ mojo::ContentHandlerDelegate* handler_delegate_; |
+ ShellPtr shell_; |
+ URLResponsePtr response_; |
+ scoped_ptr<ApplicationDelegate> application_delegate_; |
Aaron Boodman
2014/10/31 08:24:01
This is not used.
qsr
2014/10/31 12:10:44
Done.
|
+ scoped_ptr<mojo::InterfaceImpl<mojo::Application>> application_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ApplicationThread); |
+}; |
+ |
+class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
+ public: |
+ explicit ContentHandlerImpl(mojo::ContentHandlerDelegate* delegate) |
+ : delegate_(delegate) {} |
+ ~ContentHandlerImpl() { |
+ for (auto thread : active_threads_) { |
+ base::PlatformThread::Join(thread.second); |
+ delete thread.first; |
+ } |
+ } |
+ |
+ private: |
+ // Overridden from ContentHandler: |
+ virtual void StartApplication(ShellPtr shell, |
+ URLResponsePtr response) override { |
+ ApplicationThread* thread = new ApplicationThread( |
+ base::MessageLoopProxy::current(), |
+ base::Bind(&ContentHandlerImpl::OnThreadEnd, base::Unretained(this)), |
+ delegate_, |
+ shell.Pass(), |
+ response.Pass()); |
+ base::PlatformThreadHandle handle; |
+ bool launched = base::PlatformThread::Create(0, thread, &handle); |
+ DCHECK(launched); |
+ active_threads_[thread] = handle; |
+ } |
+ |
+ void OnThreadEnd(ApplicationThread* thread) { |
+ DCHECK(active_threads_.find(thread) != active_threads_.end()); |
+ base::PlatformThreadHandle handle = active_threads_[thread]; |
+ active_threads_.erase(thread); |
+ base::PlatformThread::Join(handle); |
+ delete thread; |
+ // TODO(qsr): If the last active thread exited, quit this application. Not |
Aaron Boodman
2014/10/31 08:24:01
Ah, because we never remove the entry from url_to_
qsr
2014/10/31 12:10:44
I tried, and the simplest thing resulted in crashe
|
+ // possible at the moment as the shell doesn't support the content handler |
+ // quitting. |
+ } |
+ |
+ mojo::ContentHandlerDelegate* delegate_; |
+ std::map<ApplicationThread*, base::PlatformThreadHandle> active_threads_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl); |
+}; |
+ |
+class ContentHandlerApp : public ApplicationDelegate { |
Aaron Boodman
2014/10/31 08:24:01
I like the naming better, thanks.
qsr
2014/10/31 12:10:44
Acknowledged.
|
+ public: |
+ explicit ContentHandlerApp(scoped_ptr<ContentHandlerDelegate> delegate) |
+ : delegate_(delegate.Pass()), content_handler_factory_(delegate_.get()) {} |
+ |
+ private: |
+ // Overridden from ApplicationDelegate: |
+ virtual void Initialize(ApplicationImpl* app) override { |
+ delegate_->Initialize(app); |
+ } |
+ |
+ virtual bool ConfigureIncomingConnection( |
+ ApplicationConnection* connection) override { |
+ connection->AddService(&content_handler_factory_); |
+ return delegate_->ConfigureIncomingConnection(connection); |
+ } |
+ |
+ virtual bool ConfigureOutgoingConnection( |
+ ApplicationConnection* connection) override { |
+ return delegate_->ConfigureOutgoingConnection(connection); |
+ } |
+ |
+ scoped_ptr<ContentHandlerDelegate> delegate_; |
+ InterfaceFactoryImplWithContext<ContentHandlerImpl, |
+ mojo::ContentHandlerDelegate> |
+ content_handler_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ContentHandlerApp); |
+}; |
+ |
+} // namespace |
+ |
+// static |
+MojoResult ContentHandlerRunner::Run( |
+ MojoHandle shell_handle, |
+ scoped_ptr<mojo::ContentHandlerDelegate> delegate) { |
+ mojo::ApplicationRunnerChromium runner( |
+ new ContentHandlerApp(delegate.Pass())); |
+ return runner.Run(shell_handle); |
+} |
+ |
+} // namespace mojo |