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, | 30 const SkColor kColors[] = { SK_ColorYELLOW, |
30 SK_ColorRED, | 31 SK_ColorRED, |
31 SK_ColorGREEN, | 32 SK_ColorGREEN, |
32 SK_ColorMAGENTA }; | 33 SK_ColorMAGENTA }; |
33 | 34 |
34 class EmbeddedApp; | 35 struct Window { |
| 36 Window(View* root, scoped_ptr<ServiceProvider> embedder_service_provider) |
| 37 : root(root), |
| 38 embedder_service_provider(embedder_service_provider.Pass()) {} |
| 39 View* root; |
| 40 scoped_ptr<ServiceProvider> embedder_service_provider; |
| 41 }; |
35 | 42 |
36 class EmbeddedApp | 43 class EmbeddedApp |
37 : public ApplicationDelegate, | 44 : public ApplicationDelegate, |
38 public ViewManagerDelegate, | 45 public ViewManagerDelegate, |
39 public ViewObserver { | 46 public ViewObserver { |
40 public: | 47 public: |
41 EmbeddedApp() { | 48 EmbeddedApp() { |
42 url::AddStandardScheme("mojo"); | 49 url::AddStandardScheme("mojo"); |
43 } | 50 } |
44 virtual ~EmbeddedApp() {} | 51 virtual ~EmbeddedApp() {} |
45 | 52 |
46 private: | 53 private: |
47 | 54 |
48 // Overridden from ApplicationDelegate: | 55 // Overridden from ApplicationDelegate: |
49 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 56 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
50 view_manager_client_factory_.reset( | 57 view_manager_client_factory_.reset( |
51 new ViewManagerClientFactory(app->shell(), this)); | 58 new ViewManagerClientFactory(app->shell(), this)); |
52 | |
53 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like | |
54 // embedder should be able to specify the SP embeddee receives, then | |
55 // communication can be anonymous. | |
56 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | |
57 } | 59 } |
58 | 60 |
59 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 61 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
60 MOJO_OVERRIDE { | 62 MOJO_OVERRIDE { |
61 connection->AddService(view_manager_client_factory_.get()); | 63 connection->AddService(view_manager_client_factory_.get()); |
62 return true; | 64 return true; |
63 } | 65 } |
64 | 66 |
65 // Overridden from ViewManagerDelegate: | 67 // Overridden from ViewManagerDelegate: |
66 virtual void OnEmbed(ViewManager* view_manager, | 68 virtual void OnEmbed(ViewManager* view_manager, |
67 View* root, | 69 View* root, |
68 ServiceProviderImpl* exported_services, | 70 ServiceProviderImpl* exported_services, |
69 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 71 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
70 root->AddObserver(this); | 72 root->AddObserver(this); |
71 roots_[root->id()] = root; | 73 windows_[root->id()] = new Window(root, imported_services.Pass()); |
72 root->SetColor(kColors[next_color_++ % arraysize(kColors)]); | 74 root->SetColor(kColors[next_color_++ % arraysize(kColors)]); |
73 } | 75 } |
74 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 76 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
75 base::MessageLoop::current()->Quit(); | 77 base::MessageLoop::current()->Quit(); |
76 } | 78 } |
77 | 79 |
78 // Overridden from ViewObserver: | 80 // Overridden from ViewObserver: |
79 virtual void OnViewDestroyed(View* view) OVERRIDE { | 81 virtual void OnViewDestroyed(View* view) OVERRIDE { |
80 DCHECK(roots_.find(view->id()) != roots_.end()); | 82 DCHECK(windows_.find(view->id()) != windows_.end()); |
81 roots_.erase(view->id()); | 83 windows_.erase(view->id()); |
82 } | 84 } |
83 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 85 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
84 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { | 86 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { |
85 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 87 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
86 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 88 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
87 nav_details->request->url = | 89 nav_details->request->url = |
88 "http://www.aaronboodman.com/z_dropbox/test.html"; | 90 "http://www.aaronboodman.com/z_dropbox/test.html"; |
89 navigator_host_->RequestNavigate(view->id(), TARGET_SOURCE_NODE, | 91 NavigatorHostPtr navigator_host; |
90 nav_details.Pass()); | 92 ConnectToService(windows_[view->id()]->embedder_service_provider.get(), |
| 93 &navigator_host); |
| 94 navigator_host->RequestNavigate(TARGET_SOURCE_NODE, nav_details.Pass()); |
91 } | 95 } |
92 } | 96 } |
93 } | 97 } |
94 | 98 |
95 NavigatorHostPtr navigator_host_; | |
96 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | 99 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
97 | 100 |
98 typedef std::map<Id, View*> RootMap; | 101 typedef std::map<Id, Window*> WindowMap; |
99 RootMap roots_; | 102 WindowMap windows_; |
100 | 103 |
101 int next_color_; | 104 int next_color_; |
102 | 105 |
103 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 106 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
104 }; | 107 }; |
105 | 108 |
106 } // namespace examples | 109 } // namespace examples |
107 } // namespace mojo | 110 } // namespace mojo |
108 | 111 |
109 MojoResult MojoMain(MojoHandle shell_handle) { | 112 MojoResult MojoMain(MojoHandle shell_handle) { |
110 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); | 113 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); |
111 return runner.Run(shell_handle); | 114 return runner.Run(shell_handle); |
112 } | 115 } |
OLD | NEW |