| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
| 11 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
| 12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
| 13 #include "mojo/public/cpp/application/application_impl.h" | 13 #include "mojo/public/cpp/application/application_impl.h" |
| 14 #include "mojo/public/cpp/application/application_runner_chromium.h" | 14 #include "mojo/public/cpp/application/application_runner_chromium.h" |
| 15 #include "mojo/public/cpp/application/connect.h" |
| 15 #include "mojo/public/cpp/application/interface_factory_impl.h" | 16 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 16 #include "mojo/services/public/cpp/view_manager/view.h" | 17 #include "mojo/services/public/cpp/view_manager/view.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
| 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 20 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 21 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 22 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
| 22 #include "ui/events/event_constants.h" | 23 #include "ui/events/event_constants.h" |
| 23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 24 #include "url/url_util.h" | 25 #include "url/url_util.h" |
| 25 | 26 |
| 26 namespace mojo { | 27 namespace mojo { |
| 27 namespace examples { | 28 namespace examples { |
| 28 | 29 |
| 29 const SkColor kColors[] = {SK_ColorYELLOW, SK_ColorRED, SK_ColorGREEN, | 30 const SkColor kColors[] = {SK_ColorYELLOW, SK_ColorRED, SK_ColorGREEN, |
| 30 SK_ColorMAGENTA}; | 31 SK_ColorMAGENTA}; |
| 31 | 32 |
| 32 class EmbeddedApp; | 33 struct Window { |
| 34 Window(View* root, scoped_ptr<ServiceProvider> embedder_service_provider) |
| 35 : root(root), |
| 36 embedder_service_provider(embedder_service_provider.Pass()) {} |
| 37 View* root; |
| 38 scoped_ptr<ServiceProvider> embedder_service_provider; |
| 39 }; |
| 33 | 40 |
| 34 class EmbeddedApp | 41 class EmbeddedApp |
| 35 : public ApplicationDelegate, | 42 : public ApplicationDelegate, |
| 36 public ViewManagerDelegate, | 43 public ViewManagerDelegate, |
| 37 public ViewObserver { | 44 public ViewObserver { |
| 38 public: | 45 public: |
| 39 EmbeddedApp() { url::AddStandardScheme("mojo"); } | 46 EmbeddedApp() { url::AddStandardScheme("mojo"); } |
| 40 virtual ~EmbeddedApp() {} | 47 virtual ~EmbeddedApp() {} |
| 41 | 48 |
| 42 private: | 49 private: |
| 43 | 50 |
| 44 // Overridden from ApplicationDelegate: | 51 // Overridden from ApplicationDelegate: |
| 45 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 52 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 46 view_manager_client_factory_.reset( | 53 view_manager_client_factory_.reset( |
| 47 new ViewManagerClientFactory(app->shell(), this)); | 54 new ViewManagerClientFactory(app->shell(), this)); |
| 48 | |
| 49 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like | |
| 50 // embedder should be able to specify the SP embeddee receives, then | |
| 51 // communication can be anonymous. | |
| 52 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | |
| 53 } | 55 } |
| 54 | 56 |
| 55 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 57 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 56 MOJO_OVERRIDE { | 58 MOJO_OVERRIDE { |
| 57 connection->AddService(view_manager_client_factory_.get()); | 59 connection->AddService(view_manager_client_factory_.get()); |
| 58 return true; | 60 return true; |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Overridden from ViewManagerDelegate: | 63 // Overridden from ViewManagerDelegate: |
| 62 virtual void OnEmbed(ViewManager* view_manager, | 64 virtual void OnEmbed(ViewManager* view_manager, |
| 63 View* root, | 65 View* root, |
| 64 ServiceProviderImpl* exported_services, | 66 ServiceProviderImpl* exported_services, |
| 65 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 67 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
| 66 root->AddObserver(this); | 68 root->AddObserver(this); |
| 67 roots_[root->id()] = root; | 69 windows_[root->id()] = new Window(root, imported_services.Pass()); |
| 68 root->SetColor(kColors[next_color_++ % arraysize(kColors)]); | 70 root->SetColor(kColors[next_color_++ % arraysize(kColors)]); |
| 69 } | 71 } |
| 70 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 72 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
| 71 base::MessageLoop::current()->Quit(); | 73 base::MessageLoop::current()->Quit(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 // Overridden from ViewObserver: | 76 // Overridden from ViewObserver: |
| 75 virtual void OnViewDestroyed(View* view) OVERRIDE { | 77 virtual void OnViewDestroyed(View* view) OVERRIDE { |
| 76 DCHECK(roots_.find(view->id()) != roots_.end()); | 78 DCHECK(windows_.find(view->id()) != windows_.end()); |
| 77 roots_.erase(view->id()); | 79 windows_.erase(view->id()); |
| 78 } | 80 } |
| 79 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 81 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
| 80 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { | 82 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { |
| 81 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 83 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
| 82 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 84 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
| 83 nav_details->request->url = | 85 nav_details->request->url = |
| 84 "http://www.aaronboodman.com/z_dropbox/test.html"; | 86 "http://www.aaronboodman.com/z_dropbox/test.html"; |
| 85 navigator_host_->RequestNavigate(view->id(), TARGET_SOURCE_NODE, | 87 NavigatorHostPtr navigator_host; |
| 86 nav_details.Pass()); | 88 ConnectToService(windows_[view->id()]->embedder_service_provider.get(), |
| 89 &navigator_host); |
| 90 navigator_host->RequestNavigate(TARGET_SOURCE_NODE, nav_details.Pass()); |
| 87 } | 91 } |
| 88 } | 92 } |
| 89 } | 93 } |
| 90 | 94 |
| 91 NavigatorHostPtr navigator_host_; | |
| 92 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | 95 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
| 93 | 96 |
| 94 typedef std::map<Id, View*> RootMap; | 97 typedef std::map<Id, Window*> WindowMap; |
| 95 RootMap roots_; | 98 WindowMap windows_; |
| 96 | 99 |
| 97 int next_color_; | 100 int next_color_; |
| 98 | 101 |
| 99 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 102 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
| 100 }; | 103 }; |
| 101 | 104 |
| 102 } // namespace examples | 105 } // namespace examples |
| 103 } // namespace mojo | 106 } // namespace mojo |
| 104 | 107 |
| 105 MojoResult MojoMain(MojoHandle shell_handle) { | 108 MojoResult MojoMain(MojoHandle shell_handle) { |
| 106 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); | 109 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); |
| 107 return runner.Run(shell_handle); | 110 return runner.Run(shell_handle); |
| 108 } | 111 } |
| OLD | NEW |