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/runner/android/background_application_loader.h"
8 #include "base/run_loop.h"
9 #include "mojo/shell/application_manager.h"
14 BackgroundApplicationLoader::BackgroundApplicationLoader(
15 scoped_ptr
<ApplicationLoader
> real_loader
,
16 const std::string
& thread_name
,
17 base::MessageLoop::Type message_loop_type
)
18 : loader_(real_loader
.Pass()),
19 message_loop_type_(message_loop_type
),
20 thread_name_(thread_name
),
21 message_loop_created_(true, false) {
24 BackgroundApplicationLoader::~BackgroundApplicationLoader() {
29 void BackgroundApplicationLoader::Load(
31 InterfaceRequest
<Application
> application_request
) {
32 DCHECK(application_request
.is_pending());
34 // TODO(tim): It'd be nice if we could just have each Load call
35 // result in a new thread like DynamicService{Loader, Runner}. But some
36 // loaders are creating multiple ApplicationImpls (NetworkApplicationLoader)
37 // sharing a delegate (etc). So we have to keep it single threaded, wait
38 // for the thread to initialize, and post to the TaskRunner for subsequent
39 // Load calls for now.
40 thread_
.reset(new base::DelegateSimpleThread(this, thread_name_
));
42 message_loop_created_
.Wait();
43 DCHECK(task_runner_
.get());
46 task_runner_
->PostTask(
48 base::Bind(&BackgroundApplicationLoader::LoadOnBackgroundThread
,
49 base::Unretained(this), url
,
50 base::Passed(&application_request
)));
53 void BackgroundApplicationLoader::Run() {
54 base::MessageLoop
message_loop(message_loop_type_
);
56 task_runner_
= message_loop
.task_runner();
57 quit_closure_
= loop
.QuitClosure();
58 message_loop_created_
.Signal();
61 // Destroy |loader_| on the thread it's actually used on.
65 void BackgroundApplicationLoader::LoadOnBackgroundThread(
67 InterfaceRequest
<Application
> application_request
) {
68 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
69 loader_
->Load(url
, application_request
.Pass());