1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/application/public/cpp/content_handler_factory.h"
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/platform_thread.h"
15 #include "mojo/application/public/cpp/application_connection.h"
16 #include "mojo/application/public/cpp/application_delegate.h"
17 #include "mojo/application/public/cpp/application_impl.h"
18 #include "mojo/application/public/cpp/application_runner.h"
19 #include "mojo/application/public/cpp/interface_factory_impl.h"
20 #include "mojo/message_pump/message_pump_mojo.h"
21 #include "mojo/public/cpp/bindings/strong_binding.h"
27 class ApplicationThread
: public base::PlatformThread::Delegate
{
30 scoped_refptr
<base::SingleThreadTaskRunner
> handler_thread
,
31 const base::Callback
<void(ApplicationThread
*)>& termination_callback
,
32 ContentHandlerFactory::Delegate
* handler_delegate
,
33 InterfaceRequest
<Application
> application_request
,
34 URLResponsePtr response
)
35 : handler_thread_(handler_thread
),
36 termination_callback_(termination_callback
),
37 handler_delegate_(handler_delegate
),
38 application_request_(application_request
.Pass()),
39 response_(response
.Pass()) {}
42 void ThreadMain() override
{
43 handler_delegate_
->RunApplication(application_request_
.Pass(),
45 handler_thread_
->PostTask(FROM_HERE
,
46 base::Bind(termination_callback_
, this));
49 scoped_refptr
<base::SingleThreadTaskRunner
> handler_thread_
;
50 base::Callback
<void(ApplicationThread
*)> termination_callback_
;
51 ContentHandlerFactory::Delegate
* handler_delegate_
;
52 InterfaceRequest
<Application
> application_request_
;
53 URLResponsePtr response_
;
55 DISALLOW_COPY_AND_ASSIGN(ApplicationThread
);
58 class ContentHandlerImpl
: public ContentHandler
{
60 ContentHandlerImpl(ContentHandlerFactory::Delegate
* delegate
,
61 InterfaceRequest
<ContentHandler
> request
)
62 : delegate_(delegate
),
63 binding_(this, request
.Pass()),
64 weak_factory_(this) {}
65 ~ContentHandlerImpl() override
{
66 // We're shutting down and doing cleanup. Cleanup may trigger calls back to
67 // OnThreadEnd(). As we're doing the cleanup here we don't want to do it in
68 // OnThreadEnd() as well. InvalidateWeakPtrs() ensures we don't get any
69 // calls to OnThreadEnd().
70 weak_factory_
.InvalidateWeakPtrs();
71 for (auto thread
: active_threads_
) {
72 base::PlatformThread::Join(thread
.second
);
78 // Overridden from ContentHandler:
79 void StartApplication(InterfaceRequest
<Application
> application_request
,
80 URLResponsePtr response
) override
{
81 ApplicationThread
* thread
= new ApplicationThread(
82 base::ThreadTaskRunnerHandle::Get(),
83 base::Bind(&ContentHandlerImpl::OnThreadEnd
,
84 weak_factory_
.GetWeakPtr()),
85 delegate_
, application_request
.Pass(), response
.Pass());
86 base::PlatformThreadHandle handle
;
87 bool launched
= base::PlatformThread::Create(0, thread
, &handle
);
89 active_threads_
[thread
] = handle
;
92 void OnThreadEnd(ApplicationThread
* thread
) {
93 DCHECK(active_threads_
.find(thread
) != active_threads_
.end());
94 base::PlatformThreadHandle handle
= active_threads_
[thread
];
95 active_threads_
.erase(thread
);
96 base::PlatformThread::Join(handle
);
100 ContentHandlerFactory::Delegate
* delegate_
;
101 std::map
<ApplicationThread
*, base::PlatformThreadHandle
> active_threads_
;
102 StrongBinding
<ContentHandler
> binding_
;
103 base::WeakPtrFactory
<ContentHandlerImpl
> weak_factory_
;
105 DISALLOW_COPY_AND_ASSIGN(ContentHandlerImpl
);
110 ContentHandlerFactory::ContentHandlerFactory(Delegate
* delegate
)
111 : delegate_(delegate
) {
114 ContentHandlerFactory::~ContentHandlerFactory() {
117 void ContentHandlerFactory::ManagedDelegate::RunApplication(
118 InterfaceRequest
<Application
> application_request
,
119 URLResponsePtr response
) {
120 base::MessageLoop
loop(common::MessagePumpMojo::Create());
122 this->CreateApplication(application_request
.Pass(), response
.Pass());
127 void ContentHandlerFactory::Create(ApplicationConnection
* connection
,
128 InterfaceRequest
<ContentHandler
> request
) {
129 new ContentHandlerImpl(delegate_
, request
.Pass());