Moves android specific code into android directory

This moves android specific code out of the shared code and into the
place that needs it.

BUG=none
TEST=none
R=qsr@chromium.org, davemoore@chromium.org

Review URL: https://codereview.chromium.org/805113002
diff --git a/shell/BUILD.gn b/shell/BUILD.gn
index 5468555..c9856e0 100644
--- a/shell/BUILD.gn
+++ b/shell/BUILD.gn
@@ -160,8 +160,6 @@
     "task_runners.h",
     "test_child_process.cc",
     "test_child_process.h",
-    "ui_application_loader_android.cc",
-    "ui_application_loader_android.h",
   ]
 
   deps = [
@@ -192,10 +190,12 @@
 
   if (is_android) {
     sources += [
-      "android/android_handler.h",
       "android/android_handler.cc",
-      "android/android_handler_loader.h",
+      "android/android_handler.h",
       "android/android_handler_loader.cc",
+      "android/android_handler_loader.h",
+      "android/ui_application_loader_android.cc",
+      "android/ui_application_loader_android.h",
     ]
 
     deps += [
@@ -223,7 +223,7 @@
       "android/apk/src/org/chromium/mojo_shell_apk/Bootstrap.java",
       "android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java",
     ]
-    jni_package = "mojo"
+    jni_package = "mojo/shell"
   }
 
   android_library("bootstrap_java") {
diff --git a/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java b/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java
index 1d904c1..43e0c92 100644
--- a/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java
+++ b/shell/android/apk/src/org/chromium/mojo_shell_apk/MojoMain.java
@@ -17,7 +17,7 @@
 /**
  * A placeholder class to call native functions.
  **/
-@JNINamespace("mojo")
+@JNINamespace("mojo::shell")
 public class MojoMain {
     private static final String TAG = "MojoMain";
 
diff --git a/shell/android/library_loader.cc b/shell/android/library_loader.cc
index 963e285..c112db6 100644
--- a/shell/android/library_loader.cc
+++ b/shell/android/library_loader.cc
@@ -15,7 +15,7 @@
 
 base::android::RegistrationMethod kMojoRegisteredMethods[] = {
     {"AndroidHandler", mojo::RegisterAndroidHandlerJni},
-    {"MojoMain", mojo::RegisterMojoMain},
+    {"MojoMain", mojo::shell::RegisterMojoMain},
     {"PlatformViewportAndroid",
      native_viewport::PlatformViewportAndroid::Register},
 };
diff --git a/shell/android/mojo_main.cc b/shell/android/mojo_main.cc
index b8bada5..a91ef2f 100644
--- a/shell/android/mojo_main.cc
+++ b/shell/android/mojo_main.cc
@@ -19,6 +19,11 @@
 #include "jni/MojoMain_jni.h"
 #include "mojo/application_manager/application_loader.h"
 #include "mojo/application_manager/application_manager.h"
+#include "mojo/application_manager/background_shell_application_loader.h"
+#include "services/gles2/gpu_impl.h"
+#include "services/native_viewport/native_viewport_impl.h"
+#include "shell/android/android_handler_loader.h"
+#include "shell/android/ui_application_loader_android.h"
 #include "shell/context.h"
 #include "shell/init.h"
 #include "ui/gl/gl_surface_egl.h"
@@ -26,21 +31,86 @@
 using base::LazyInstance;
 
 namespace mojo {
+namespace shell {
 
 namespace {
 
 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop =
     LAZY_INSTANCE_INITIALIZER;
 
-LazyInstance<scoped_ptr<shell::Context>> g_context = LAZY_INSTANCE_INITIALIZER;
+LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER;
 
 LazyInstance<scoped_ptr<base::android::JavaHandlerThread>> g_shell_thread =
     LAZY_INSTANCE_INITIALIZER;
 
+class NativeViewportApplicationLoader : public ApplicationLoader,
+                                        public ApplicationDelegate,
+                                        public InterfaceFactory<NativeViewport>,
+                                        public InterfaceFactory<Gpu> {
+ public:
+  NativeViewportApplicationLoader() : gpu_state_(new gles2::GpuImpl::State) {}
+  ~NativeViewportApplicationLoader() override {}
+
+ private:
+  // ApplicationLoader implementation.
+  void Load(ApplicationManager* manager,
+            const GURL& url,
+            ScopedMessagePipeHandle shell_handle,
+            LoadCallback callback) override {
+    DCHECK(shell_handle.is_valid());
+    app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
+  }
+
+  void OnApplicationError(ApplicationManager* manager,
+                          const GURL& url) override {}
+
+  // ApplicationDelegate implementation.
+  bool ConfigureIncomingConnection(
+      mojo::ApplicationConnection* connection) override {
+    connection->AddService<NativeViewport>(this);
+    connection->AddService<Gpu>(this);
+    return true;
+  }
+
+  // InterfaceFactory<NativeViewport> implementation.
+  void Create(ApplicationConnection* connection,
+              InterfaceRequest<NativeViewport> request) override {
+    BindToRequest(new native_viewport::NativeViewportImpl(app_.get(), false),
+                  &request);
+  }
+
+  // InterfaceFactory<Gpu> implementation.
+  void Create(ApplicationConnection* connection,
+              InterfaceRequest<Gpu> request) override {
+    new gles2::GpuImpl(request.Pass(), gpu_state_);
+  }
+
+  scoped_refptr<gles2::GpuImpl::State> gpu_state_;
+  scoped_ptr<ApplicationImpl> app_;
+  DISALLOW_COPY_AND_ASSIGN(NativeViewportApplicationLoader);
+};
+
+void ConfigureAndroidServices(Context* context) {
+  context->application_manager()->SetLoaderForURL(
+      make_scoped_ptr(new UIApplicationLoader(
+          make_scoped_ptr(new NativeViewportApplicationLoader()),
+          g_java_message_loop.Get().get())),
+      GURL("mojo:native_viewport_service"));
+
+  // Android handler is bundled with the Mojo shell, because it uses the
+  // MojoShell application as the JNI bridge to bootstrap execution of other
+  // Android Mojo apps that need JNI.
+  context->application_manager()->SetLoaderForURL(
+      make_scoped_ptr(new BackgroundShellApplicationLoader(
+          make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
+          base::MessageLoop::TYPE_DEFAULT)),
+      GURL("mojo:android_handler"));
+}
+
 void RunShell(std::vector<GURL> app_urls) {
-  shell::Context* context = g_context.Pointer()->get();
+  Context* context = g_context.Pointer()->get();
   context->Init();
-  context->set_ui_loop(g_java_message_loop.Get().get());
+  ConfigureAndroidServices(context);
   for (std::vector<GURL>::const_iterator it = app_urls.begin();
        it != app_urls.end(); ++it) {
     context->Run(*it);
@@ -62,12 +132,12 @@
   base::FilePath mojo_shell_file_path(
       base::android::ConvertJavaStringToUTF8(env, mojo_shell_path));
   base::CommandLine::ForCurrentProcess()->SetProgram(mojo_shell_file_path);
-  mojo::shell::InitializeLogging();
+  InitializeLogging();
 
   // We want ~MessageLoop to happen prior to ~Context. Initializing
   // LazyInstances is akin to stack-allocating objects; their destructors
   // will be invoked first-in-last-out.
-  shell::Context* shell_context = new shell::Context();
+  Context* shell_context = new Context();
   shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath(
       base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory)));
   g_context.Get().reset(shell_context);
@@ -103,4 +173,5 @@
   return RegisterNativesImpl(env);
 }
 
+}  // namespace shell
 }  // namespace mojo
diff --git a/shell/android/mojo_main.h b/shell/android/mojo_main.h
index 65fec20..bc2e9f1 100644
--- a/shell/android/mojo_main.h
+++ b/shell/android/mojo_main.h
@@ -8,9 +8,11 @@
 #include <jni.h>
 
 namespace mojo {
+namespace shell {
 
 bool RegisterMojoMain(JNIEnv* env);
 
+}  // namespace shell
 }  // namespace mojo
 
 #endif  // SHELL_ANDROID_MOJO_MAIN_H_
diff --git a/shell/ui_application_loader_android.cc b/shell/android/ui_application_loader_android.cc
similarity index 85%
rename from shell/ui_application_loader_android.cc
rename to shell/android/ui_application_loader_android.cc
index 7c2f2e3..ca4ff8b 100644
--- a/shell/ui_application_loader_android.cc
+++ b/shell/android/ui_application_loader_android.cc
@@ -2,22 +2,22 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "shell/ui_application_loader_android.h"
+#include "shell/android/ui_application_loader_android.h"
 
 #include "base/bind.h"
+#include "base/message_loop/message_loop.h"
 #include "mojo/application_manager/application_manager.h"
-#include "shell/context.h"
 
 namespace mojo {
 
 UIApplicationLoader::UIApplicationLoader(
     scoped_ptr<ApplicationLoader> real_loader,
-    shell::Context* context)
-    : loader_(real_loader.Pass()), context_(context) {
+    base::MessageLoop* ui_message_loop)
+    : loader_(real_loader.Pass()), ui_message_loop_(ui_message_loop) {
 }
 
 UIApplicationLoader::~UIApplicationLoader() {
-  context_->ui_loop()->PostTask(
+  ui_message_loop_->PostTask(
       FROM_HERE, base::Bind(&UIApplicationLoader::ShutdownOnUIThread,
                             base::Unretained(this)));
 }
@@ -27,7 +27,7 @@
                                ScopedMessagePipeHandle shell_handle,
                                LoadCallback callback) {
   DCHECK(shell_handle.is_valid());
-  context_->ui_loop()->PostTask(
+  ui_message_loop_->PostTask(
       FROM_HERE,
       base::Bind(&UIApplicationLoader::LoadOnUIThread, base::Unretained(this),
                  manager, url, base::Passed(&shell_handle)));
@@ -35,7 +35,7 @@
 
 void UIApplicationLoader::OnApplicationError(ApplicationManager* manager,
                                              const GURL& url) {
-  context_->ui_loop()->PostTask(
+  ui_message_loop_->PostTask(
       FROM_HERE, base::Bind(&UIApplicationLoader::OnApplicationErrorOnUIThread,
                             base::Unretained(this), manager, url));
 }
diff --git a/shell/ui_application_loader_android.h b/shell/android/ui_application_loader_android.h
similarity index 83%
rename from shell/ui_application_loader_android.h
rename to shell/android/ui_application_loader_android.h
index 64b13f0..7de34c7 100644
--- a/shell/ui_application_loader_android.h
+++ b/shell/android/ui_application_loader_android.h
@@ -2,28 +2,28 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef SHELL_UI_APPLICATION_LOADER_ANDROID_H_
-#define SHELL_UI_APPLICATION_LOADER_ANDROID_H_
+#ifndef SHELL_ANDROID_UI_APPLICATION_LOADER_ANDROID_H_
+#define SHELL_ANDROID_UI_APPLICATION_LOADER_ANDROID_H_
 
 #include "base/macros.h"
 #include "base/memory/scoped_ptr.h"
 #include "mojo/application_manager/application_loader.h"
 
+namespace base {
+class MessageLoop;
+}
+
 namespace mojo {
 
 class ApplicationManager;
 
-namespace shell {
-class Context;
-}
-
 // ApplicationLoader implementation that creates a background thread and issues
 // load
 // requests there.
 class UIApplicationLoader : public ApplicationLoader {
  public:
   UIApplicationLoader(scoped_ptr<ApplicationLoader> real_loader,
-                      shell::Context* context);
+                      base::MessageLoop* ui_message_loop);
   ~UIApplicationLoader() override;
 
   // ApplicationLoader overrides:
@@ -49,11 +49,11 @@
   void ShutdownOnUIThread();
 
   scoped_ptr<ApplicationLoader> loader_;
-  shell::Context* context_;
+  base::MessageLoop* ui_message_loop_;
 
   DISALLOW_COPY_AND_ASSIGN(UIApplicationLoader);
 };
 
 }  // namespace mojo
 
-#endif  // SHELL_UI_APPLICATION_LOADER_ANDROID_H_
+#endif  // SHELL_ANDROID_UI_APPLICATION_LOADER_ANDROID_H_
diff --git a/shell/context.cc b/shell/context.cc
index b034fc5..c38de5e 100644
--- a/shell/context.cc
+++ b/shell/context.cc
@@ -31,15 +31,8 @@
 #include "shell/in_process_dynamic_service_runner.h"
 #include "shell/out_of_process_dynamic_service_runner.h"
 #include "shell/switches.h"
-#include "shell/ui_application_loader_android.h"
 #include "url/gurl.h"
 
-#if defined(OS_ANDROID)
-#include "services/gles2/gpu_impl.h"
-#include "services/native_viewport/native_viewport_impl.h"
-#include "shell/android/android_handler_loader.h"
-#endif  // defined(OS_ANDROID)
-
 namespace mojo {
 namespace shell {
 namespace {
@@ -122,56 +115,6 @@
 
 }  // namespace
 
-#if defined(OS_ANDROID)
-class Context::NativeViewportApplicationLoader
-    : public ApplicationLoader,
-      public ApplicationDelegate,
-      public InterfaceFactory<NativeViewport>,
-      public InterfaceFactory<Gpu> {
- public:
-  NativeViewportApplicationLoader() : gpu_state_(new gles2::GpuImpl::State) {}
-  virtual ~NativeViewportApplicationLoader() {}
-
- private:
-  // ApplicationLoader implementation.
-  virtual void Load(ApplicationManager* manager,
-                    const GURL& url,
-                    ScopedMessagePipeHandle shell_handle,
-                    LoadCallback callback) override {
-    DCHECK(shell_handle.is_valid());
-    app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
-  }
-
-  virtual void OnApplicationError(ApplicationManager* manager,
-                                  const GURL& url) override {}
-
-  // ApplicationDelegate implementation.
-  virtual bool ConfigureIncomingConnection(
-      mojo::ApplicationConnection* connection) override {
-    connection->AddService<NativeViewport>(this);
-    connection->AddService<Gpu>(this);
-    return true;
-  }
-
-  // InterfaceFactory<NativeViewport> implementation.
-  virtual void Create(ApplicationConnection* connection,
-                      InterfaceRequest<NativeViewport> request) override {
-    BindToRequest(new native_viewport::NativeViewportImpl(app_.get(), false),
-                  &request);
-  }
-
-  // InterfaceFactory<Gpu> implementation.
-  virtual void Create(ApplicationConnection* connection,
-                      InterfaceRequest<Gpu> request) override {
-    new gles2::GpuImpl(request.Pass(), gpu_state_);
-  }
-
-  scoped_refptr<gles2::GpuImpl::State> gpu_state_;
-  scoped_ptr<ApplicationImpl> app_;
-  DISALLOW_COPY_AND_ASSIGN(NativeViewportApplicationLoader);
-};
-#endif
-
 Context::Context() : application_manager_(this) {
   DCHECK(!base::MessageLoop::current());
 }
@@ -231,17 +174,6 @@
   application_manager_.set_default_loader(
       scoped_ptr<ApplicationLoader>(dynamic_application_loader));
 
-// The native viewport service synchronously waits for certain messages. If we
-// don't run it on its own thread we can easily deadlock. Long term native
-// viewport should run its own process so that this isn't an issue.
-#if defined(OS_ANDROID)
-  application_manager_.SetLoaderForURL(
-      scoped_ptr<ApplicationLoader>(new UIApplicationLoader(
-          scoped_ptr<ApplicationLoader>(new NativeViewportApplicationLoader()),
-          this)),
-      GURL("mojo:native_viewport_service"));
-#endif
-
   if (command_line->HasSwitch(switches::kSpy)) {
     spy_.reset(
         new mojo::Spy(&application_manager_,
@@ -251,20 +183,6 @@
     // was implemented by src\mojo\spy\websocket_server.h and .cc.
   }
 
-#if defined(OS_ANDROID)
-  {
-    // Android handler is bundled with the Mojo shell, because it uses the
-    // MojoShell application as the JNI bridge to bootstrap execution of other
-    // Android Mojo apps that need JNI.
-    scoped_ptr<BackgroundShellApplicationLoader> loader(
-        new BackgroundShellApplicationLoader(
-            scoped_ptr<ApplicationLoader>(new AndroidHandlerLoader()),
-            "android_handler", base::MessageLoop::TYPE_DEFAULT));
-    application_manager_.SetLoaderForURL(loader.Pass(),
-                                         GURL("mojo:android_handler"));
-  }
-#endif
-
   tracing::TraceDataCollectorPtr trace_data_collector_ptr;
   application_manager_.ConnectToService(GURL("mojo:tracing"),
                                         &trace_data_collector_ptr);
diff --git a/shell/context.h b/shell/context.h
index 71ef68b..b7f129a 100644
--- a/shell/context.h
+++ b/shell/context.h
@@ -12,10 +12,6 @@
 #include "shell/mojo_url_resolver.h"
 #include "shell/task_runners.h"
 
-#if defined(OS_ANDROID)
-#include "base/android/scoped_java_ref.h"
-#endif  // defined(OS_ANDROID)
-
 namespace mojo {
 
 class Spy;
@@ -43,11 +39,6 @@
   ApplicationManager* application_manager() { return &application_manager_; }
   MojoURLResolver* mojo_url_resolver() { return &mojo_url_resolver_; }
 
-#if defined(OS_ANDROID)
-  base::MessageLoop* ui_loop() const { return ui_loop_; }
-  void set_ui_loop(base::MessageLoop* ui_loop) { ui_loop_ = ui_loop; }
-#endif  // defined(OS_ANDROID)
-
  private:
   class NativeViewportApplicationLoader;
 
@@ -61,9 +52,6 @@
   ApplicationManager application_manager_;
   MojoURLResolver mojo_url_resolver_;
   scoped_ptr<Spy> spy_;
-#if defined(OS_ANDROID)
-  base::MessageLoop* ui_loop_;
-#endif  // defined(OS_ANDROID)
 
   DISALLOW_COPY_AND_ASSIGN(Context);
 };