Home | History | Annotate | Download | only in application
      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.
      4 
      5 #include "mojo/application/application_runner_chromium.h"
      6 
      7 #include "base/at_exit.h"
      8 #include "base/command_line.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "mojo/common/message_pump_mojo.h"
     12 #include "mojo/public/cpp/application/application_delegate.h"
     13 #include "mojo/public/cpp/application/application_impl.h"
     14 
     15 namespace mojo {
     16 
     17 // static
     18 void ApplicationImpl::Terminate() {
     19   if (base::MessageLoop::current()->is_running())
     20     base::MessageLoop::current()->Quit();
     21 }
     22 
     23 ApplicationRunnerChromium::ApplicationRunnerChromium(
     24     ApplicationDelegate* delegate)
     25     : delegate_(scoped_ptr<ApplicationDelegate>(delegate)),
     26       message_loop_type_(base::MessageLoop::TYPE_CUSTOM),
     27       has_run_(false) {}
     28 
     29 ApplicationRunnerChromium::~ApplicationRunnerChromium() {}
     30 
     31 void ApplicationRunnerChromium::set_message_loop_type(
     32     base::MessageLoop::Type type) {
     33   DCHECK_NE(base::MessageLoop::TYPE_CUSTOM, type);
     34   DCHECK(!has_run_);
     35 
     36   message_loop_type_ = type;
     37 }
     38 
     39 MojoResult ApplicationRunnerChromium::Run(MojoHandle shell_handle) {
     40   DCHECK(!has_run_);
     41   has_run_ = true;
     42 
     43   base::CommandLine::Init(0, NULL);
     44 #if !defined(COMPONENT_BUILD)
     45   base::AtExitManager at_exit;
     46 #endif
     47 
     48   {
     49     scoped_ptr<base::MessageLoop> loop;
     50     if (message_loop_type_ == base::MessageLoop::TYPE_CUSTOM)
     51       loop.reset(new base::MessageLoop(common::MessagePumpMojo::Create()));
     52     else
     53       loop.reset(new base::MessageLoop(message_loop_type_));
     54 
     55     ApplicationImpl impl(delegate_.get(),
     56                          MakeScopedHandle(MessagePipeHandle(shell_handle)));
     57     loop->Run();
     58   }
     59   delegate_.reset();
     60   return MOJO_RESULT_OK;
     61 }
     62 
     63 }  // namespace mojo
     64