Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / launcher / launcher.cc
index 9b2c1fb..70a9d8c 100644 (file)
 #include "base/message_loop/message_loop.h"
 #include "base/path_service.h"
 #include "mojo/examples/aura_demo/demo_screen.h"
-#include "mojo/examples/aura_demo/root_window_host_mojo.h"
-#include "mojo/examples/compositor_app/compositor_host.h"
-#include "mojo/examples/compositor_app/gles2_client_impl.h"
-#include "mojo/public/bindings/lib/remote_ptr.h"
+#include "mojo/examples/aura_demo/window_tree_host_mojo.h"
+#include "mojo/public/bindings/allocation_scope.h"
+#include "mojo/public/bindings/remote_ptr.h"
 #include "mojo/public/gles2/gles2_cpp.h"
+#include "mojo/public/shell/application.h"
 #include "mojo/public/system/core.h"
 #include "mojo/public/system/macros.h"
+#include "mojom/launcher.h"
 #include "mojom/native_viewport.h"
 #include "mojom/shell.h"
 #include "ui/aura/client/aura_constants.h"
@@ -93,21 +94,12 @@ class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
     }
   }
 
-  // ui::InputMethodDelegate:
-  virtual bool DispatchKeyEventPostIME(
-      const base::NativeEvent& event) OVERRIDE {
-    ui::TranslatedKeyEvent aura_event(event, false /* is_char */);
-    return root_->GetDispatcher()->AsWindowTreeHostDelegate()->OnHostKeyEvent(
-        &aura_event);
-  }
-
-  virtual bool DispatchFabricatedKeyEventPostIME(ui::EventType type,
-                                                 ui::KeyboardCode key_code,
-                                                 int flags) OVERRIDE {
-    ui::TranslatedKeyEvent aura_event(type == ui::ET_KEY_PRESSED, key_code,
-                                      flags);
-    return root_->GetDispatcher()->AsWindowTreeHostDelegate()->OnHostKeyEvent(
-        &aura_event);
+  // ui::internal::InputMethodDelegate:
+  virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) OVERRIDE {
+    ui::TranslatedKeyEvent aura_event(event);
+    ui::EventDispatchDetails details =
+        root_->GetDispatcher()->OnEventFromSource(&aura_event);
+    return aura_event.handled() || details.dispatcher_destroyed;
   }
 
   aura::Window* root_;
@@ -139,9 +131,19 @@ class LauncherWindowTreeClient : public aura::client::WindowTreeClient {
   DISALLOW_COPY_AND_ASSIGN(LauncherWindowTreeClient);
 };
 
+// Called when the user has submitted a URL by pressing Enter.
+class URLReceiver {
+ public:
+  virtual void OnURLEntered(const std::string& url_text) = 0;
+
+ protected:
+  virtual ~URLReceiver() {}
+};
+
 class LauncherController : public views::TextfieldController {
  public:
-  LauncherController() {}
+  explicit LauncherController(URLReceiver* url_receiver)
+      : url_receiver_(url_receiver) {}
 
   void InitInWindow(aura::Window* parent) {
     views::Widget* widget = new views::Widget;
@@ -162,6 +164,7 @@ class LauncherController : public views::TextfieldController {
     views::Textfield* textfield = new views::Textfield;
     textfield->set_controller(this);
     container->AddChildView(textfield);
+    textfield->RequestFocus();
 
     container->Layout();
 
@@ -174,41 +177,72 @@ class LauncherController : public views::TextfieldController {
                               const ui::KeyEvent& key_event) OVERRIDE {
     if (key_event.key_code() == ui::VKEY_RETURN) {
       GURL url(sender->text());
-      printf("URL: %s\n", url.spec().c_str());
+      printf("Enter pressed with URL: %s\n", url.spec().c_str());
+      url_receiver_->OnURLEntered(url.spec());
     }
     return false;
   }
 
+  URLReceiver* url_receiver_;
+
   DISALLOW_COPY_AND_ASSIGN(LauncherController);
 };
 
-class Launcher : public ShellClient {
+class LauncherImpl : public Application,
+                     public Launcher,
+                     public URLReceiver {
  public:
-  explicit Launcher(ScopedMessagePipeHandle shell_handle)
-      : shell_(shell_handle.Pass(), this) {
+  explicit LauncherImpl(MojoHandle shell_handle)
+      : Application(shell_handle),
+        launcher_controller_(this),
+        pending_show_(false) {
     screen_.reset(DemoScreen::Create());
     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
 
-    ScopedMessagePipeHandle client_handle, native_viewport_handle;
-    CreateMessagePipe(&client_handle, &native_viewport_handle);
-    root_window_host_.reset(new WindowTreeHostMojo(
-        native_viewport_handle.Pass(), gfx::Rect(50, 50, 450, 60),
-        base::Bind(&Launcher::HostContextCreated, base::Unretained(this))));
+    InterfacePipe<NativeViewport, AnyInterface> pipe;
+
     AllocationScope scope;
-    shell_->Connect("mojo:mojo_native_viewport_service", client_handle.Pass());
-  }
+    shell()->Connect("mojo:mojo_native_viewport_service",
+                     pipe.handle_to_peer.Pass());
 
-  virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
-    NOTREACHED() << "Launcher can't be connected to.";
+    window_tree_host_.reset(new WindowTreeHostMojo(
+        pipe.handle_to_self.Pass(), gfx::Rect(50, 50, 450, 60),
+        base::Bind(&LauncherImpl::HostContextCreated, base::Unretained(this))));
   }
 
  private:
+  // Overridden from Application:
+  virtual void AcceptConnection(const mojo::String& url,
+                                ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
+    launcher_client_.reset(
+        MakeScopedHandle(LauncherClientHandle(handle.release().value())).Pass(),
+        this);
+  }
+
+  // Overridden from Launcher:
+  virtual void Show() OVERRIDE {
+    if (!root_window_.get()) {
+      pending_show_ = true;
+      return;
+    }
+    root_window_->host()->Show();
+  }
+  virtual void Hide() OVERRIDE {
+    root_window_->host()->Hide();
+  }
+
+  // Overridden from URLReceiver:
+  virtual void OnURLEntered(const std::string& url_text) OVERRIDE {
+    AllocationScope scope;
+    launcher_client_->OnURLEntered(url_text);
+  }
+
   void HostContextCreated() {
     aura::RootWindow::CreateParams params(gfx::Rect(450, 60));
-    params.host = root_window_host_.get();
+    params.host = window_tree_host_.get();
     root_window_.reset(new aura::RootWindow(params));
-    root_window_host_->set_delegate(root_window_.get());
-    root_window_->Init();
+    window_tree_host_->set_delegate(root_window_.get());
+    root_window_->host()->InitHost();
     root_window_->window()->SetBounds(gfx::Rect(450, 60));
 
     focus_client_.reset(new aura::test::TestFocusClient());
@@ -224,7 +258,10 @@ class Launcher : public ShellClient {
 
     launcher_controller_.InitInWindow(root_window_->window());
 
-    root_window_->host()->Show();
+    if (pending_show_) {
+      pending_show_ = false;
+      Show();
+    }
   }
 
   scoped_ptr<DemoScreen> screen_;
@@ -236,9 +273,11 @@ class Launcher : public ShellClient {
 
   LauncherController launcher_controller_;
 
-  RemotePtr<Shell> shell_;
-  scoped_ptr<WindowTreeHostMojo> root_window_host_;
+  RemotePtr<LauncherClient> launcher_client_;
+  scoped_ptr<WindowTreeHostMojo> window_tree_host_;
   scoped_ptr<aura::RootWindow> root_window_;
+
+  bool pending_show_;
 };
 
 }  // namespace examples
@@ -263,10 +302,8 @@ extern "C" LAUNCHER_EXPORT MojoResult CDECL MojoMain(
   //             MessageLoop is not of TYPE_UI. I think we need a way to build
   //             Aura that doesn't define platform-specific stuff.
   aura::Env::CreateInstance();
-  mojo::examples::Launcher launcher(
-      mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass());
+  mojo::examples::LauncherImpl launcher(shell_handle);
   loop.Run();
 
-  MojoGLES2Terminate();
   return MOJO_RESULT_OK;
 }