Home | History | Annotate | Download | only in shell
      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/shell/ui_application_loader_android.h"
      6 
      7 #include "base/bind.h"
      8 #include "mojo/application_manager/application_manager.h"
      9 #include "mojo/shell/context.h"
     10 
     11 namespace mojo {
     12 
     13 class UIApplicationLoader::UILoader {
     14  public:
     15   explicit UILoader(ApplicationLoader* loader) : loader_(loader) {}
     16   ~UILoader() {}
     17 
     18   void Load(ApplicationManager* manager,
     19             const GURL& url,
     20             ScopedMessagePipeHandle shell_handle) {
     21     scoped_refptr<LoadCallbacks> callbacks(
     22         new ApplicationLoader::SimpleLoadCallbacks(shell_handle.Pass()));
     23     loader_->Load(manager, url, callbacks);
     24   }
     25 
     26   void OnApplicationError(ApplicationManager* manager, const GURL& url) {
     27     loader_->OnApplicationError(manager, url);
     28   }
     29 
     30  private:
     31   ApplicationLoader* loader_;  // Owned by UIApplicationLoader
     32 
     33   DISALLOW_COPY_AND_ASSIGN(UILoader);
     34 };
     35 
     36 UIApplicationLoader::UIApplicationLoader(
     37     scoped_ptr<ApplicationLoader> real_loader,
     38     shell::Context* context)
     39     : loader_(real_loader.Pass()), context_(context) {
     40 }
     41 
     42 UIApplicationLoader::~UIApplicationLoader() {
     43   context_->ui_loop()->PostTask(
     44       FROM_HERE,
     45       base::Bind(&UIApplicationLoader::ShutdownOnUIThread,
     46                  base::Unretained(this)));
     47 }
     48 
     49 void UIApplicationLoader::Load(ApplicationManager* manager,
     50                                const GURL& url,
     51                                scoped_refptr<LoadCallbacks> callbacks) {
     52   ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
     53   if (!shell_handle.is_valid())
     54     return;
     55   context_->ui_loop()->PostTask(
     56       FROM_HERE,
     57       base::Bind(
     58           &UIApplicationLoader::LoadOnUIThread,
     59           base::Unretained(this),
     60           manager,
     61           url,
     62           base::Owned(new ScopedMessagePipeHandle(shell_handle.Pass()))));
     63 }
     64 
     65 void UIApplicationLoader::OnApplicationError(ApplicationManager* manager,
     66                                              const GURL& url) {
     67   context_->ui_loop()->PostTask(
     68       FROM_HERE,
     69       base::Bind(&UIApplicationLoader::OnApplicationErrorOnUIThread,
     70                  base::Unretained(this),
     71                  manager,
     72                  url));
     73 }
     74 
     75 void UIApplicationLoader::LoadOnUIThread(
     76     ApplicationManager* manager,
     77     const GURL& url,
     78     ScopedMessagePipeHandle* shell_handle) {
     79   if (!ui_loader_)
     80     ui_loader_ = new UILoader(loader_.get());
     81   ui_loader_->Load(manager, url, shell_handle->Pass());
     82 }
     83 
     84 void UIApplicationLoader::OnApplicationErrorOnUIThread(
     85     ApplicationManager* manager,
     86     const GURL& url) {
     87   if (!ui_loader_)
     88     ui_loader_ = new UILoader(loader_.get());
     89   ui_loader_->OnApplicationError(manager, url);
     90 }
     91 
     92 void UIApplicationLoader::ShutdownOnUIThread() {
     93   delete ui_loader_;
     94   // Destroy |loader_| on the thread it's actually used on.
     95   loader_.reset();
     96 }
     97 
     98 }  // namespace mojo
     99