Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / mojo / application / public / cpp / lib / application_runner.cc
blob0418d91b47a45c490c8e4adef6db57eee56df09b
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/application_runner.h"
7 #include "base/at_exit.h"
8 #include "base/command_line.h"
9 #include "base/debug/stack_trace.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/threading/worker_pool.h"
13 #include "mojo/application/public/cpp/application_delegate.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/common/message_pump_mojo.h"
17 namespace mojo {
19 int g_application_runner_argc;
20 const char* const* g_application_runner_argv;
22 ApplicationRunner::ApplicationRunner(ApplicationDelegate* delegate)
23 : delegate_(scoped_ptr<ApplicationDelegate>(delegate)),
24 message_loop_type_(base::MessageLoop::TYPE_CUSTOM),
25 has_run_(false) {}
27 ApplicationRunner::~ApplicationRunner() {}
29 void ApplicationRunner::InitBaseCommandLine() {
30 base::CommandLine::Init(g_application_runner_argc, g_application_runner_argv);
33 void ApplicationRunner::set_message_loop_type(base::MessageLoop::Type type) {
34 DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type);
35 DCHECK(!has_run_);
37 message_loop_type_ = type;
40 MojoResult ApplicationRunner::Run(MojoHandle application_request_handle,
41 bool init_base) {
42 DCHECK(!has_run_);
43 has_run_ = true;
45 scoped_ptr<base::AtExitManager> at_exit;
46 if (init_base) {
47 InitBaseCommandLine();
48 at_exit.reset(new base::AtExitManager);
49 #ifndef NDEBUG
50 base::debug::EnableInProcessStackDumping();
51 #endif
55 scoped_ptr<base::MessageLoop> loop;
56 if (message_loop_type_ == base::MessageLoop::TYPE_CUSTOM)
57 loop.reset(new base::MessageLoop(common::MessagePumpMojo::Create()));
58 else
59 loop.reset(new base::MessageLoop(message_loop_type_));
61 ApplicationImpl impl(delegate_.get(),
62 MakeRequest<Application>(MakeScopedHandle(
63 MessagePipeHandle(application_request_handle))));
64 loop->Run();
65 // It's very common for the delegate to cache the app and terminate on
66 // errors. If we don't delete the delegate before the app we run the risk
67 // of the delegate having a stale reference to the app and trying to use it.
68 // Note that we destruct the message loop first because that might trigger
69 // connection error handlers and they might access objects created by the
70 // delegate.
71 loop.reset();
72 delegate_.reset();
75 // By default the worker pool continues running until all tasks are done or
76 // the process is shut down. However, because the application could be
77 // unloaded before process shutdown, we have to wait for the worker pool to
78 // shut down cleanly.
79 base::WorkerPool::ShutDownCleanly();
81 return MOJO_RESULT_OK;
84 MojoResult ApplicationRunner::Run(MojoHandle application_request_handle) {
85 return Run(application_request_handle, true);
88 } // namespace mojo