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/interface_factory_impl.h" | 15 #include "mojo/public/cpp/application/interface_factory_impl.h" |
16 #include "mojo/services/public/cpp/view_manager/view.h" | 16 #include "mojo/services/public/cpp/view_manager/view.h" |
17 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
20 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 20 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
22 #include "ui/events/event_constants.h" | 22 #include "ui/events/event_constants.h" |
23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
24 #include "url/url_util.h" | 24 #include "url/url_util.h" |
25 | 25 |
26 namespace mojo { | 26 namespace mojo { |
27 namespace examples { | 27 namespace examples { |
| 28 |
| 29 const SkColor kColors[] = { SK_ColorYELLOW, |
| 30 SK_ColorRED, |
| 31 SK_ColorGREEN, |
| 32 SK_ColorMAGENTA }; |
| 33 |
28 class EmbeddedApp; | 34 class EmbeddedApp; |
29 | 35 |
30 class NavigatorImpl : public InterfaceImpl<Navigator> { | |
31 public: | |
32 explicit NavigatorImpl(EmbeddedApp* app) : app_(app) {} | |
33 | |
34 private: | |
35 virtual void Navigate( | |
36 uint32 view_id, | |
37 NavigationDetailsPtr navigation_details, | |
38 ResponseDetailsPtr response_details) OVERRIDE; | |
39 | |
40 EmbeddedApp* app_; | |
41 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | |
42 }; | |
43 | |
44 class EmbeddedApp | 36 class EmbeddedApp |
45 : public ApplicationDelegate, | 37 : public ApplicationDelegate, |
46 public ViewManagerDelegate, | 38 public ViewManagerDelegate, |
47 public ViewObserver { | 39 public ViewObserver { |
48 public: | 40 public: |
49 EmbeddedApp() | 41 EmbeddedApp() { |
50 : navigator_factory_(this), | |
51 view_manager_(NULL), | |
52 view_manager_client_factory_(this) { | |
53 url::AddStandardScheme("mojo"); | 42 url::AddStandardScheme("mojo"); |
54 } | 43 } |
55 virtual ~EmbeddedApp() {} | 44 virtual ~EmbeddedApp() {} |
56 | 45 |
57 void SetViewColor(uint32 view_id, SkColor color) { | |
58 pending_view_colors_[view_id] = color; | |
59 ProcessPendingViewColor(view_id); | |
60 } | |
61 | |
62 private: | 46 private: |
63 | 47 |
64 // Overridden from ApplicationDelegate: | 48 // Overridden from ApplicationDelegate: |
65 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 49 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 50 view_manager_client_factory_.reset( |
| 51 new ViewManagerClientFactory(app->shell(), this)); |
| 52 |
66 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like | 53 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like |
67 // embedder should be able to specify the SP embeddee receives, then | 54 // embedder should be able to specify the SP embeddee receives, then |
68 // communication can be anonymous. | 55 // communication can be anonymous. |
69 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | 56 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); |
70 } | 57 } |
71 | 58 |
72 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 59 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
73 MOJO_OVERRIDE { | 60 MOJO_OVERRIDE { |
74 connection->AddService(&view_manager_client_factory_); | 61 connection->AddService(view_manager_client_factory_.get()); |
75 connection->AddService(&navigator_factory_); | |
76 return true; | 62 return true; |
77 } | 63 } |
78 | 64 |
79 // Overridden from ViewManagerDelegate: | 65 // Overridden from ViewManagerDelegate: |
80 virtual void OnEmbed(ViewManager* view_manager, | 66 virtual void OnEmbed(ViewManager* view_manager, |
81 View* root, | 67 View* root, |
82 ServiceProviderImpl* exported_services, | 68 ServiceProviderImpl* exported_services, |
83 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 69 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
84 root->AddObserver(this); | 70 root->AddObserver(this); |
85 roots_[root->id()] = root; | 71 roots_[root->id()] = root; |
86 ProcessPendingViewColor(root->id()); | 72 root->SetColor(kColors[next_color_++ % arraysize(kColors)]); |
87 } | 73 } |
88 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 74 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
89 base::MessageLoop::current()->Quit(); | 75 base::MessageLoop::current()->Quit(); |
90 } | 76 } |
91 | 77 |
92 // Overridden from ViewObserver: | 78 // Overridden from ViewObserver: |
93 virtual void OnViewDestroyed(View* view) OVERRIDE { | 79 virtual void OnViewDestroyed(View* view) OVERRIDE { |
94 DCHECK(roots_.find(view->id()) != roots_.end()); | 80 DCHECK(roots_.find(view->id()) != roots_.end()); |
95 roots_.erase(view->id()); | 81 roots_.erase(view->id()); |
96 } | 82 } |
97 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 83 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
98 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { | 84 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { |
99 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 85 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
100 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 86 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
101 nav_details->request->url = | 87 nav_details->request->url = |
102 "http://www.aaronboodman.com/z_dropbox/test.html"; | 88 "http://www.aaronboodman.com/z_dropbox/test.html"; |
103 navigator_host_->RequestNavigate(view->id(), TARGET_SOURCE_NODE, | 89 navigator_host_->RequestNavigate(view->id(), TARGET_SOURCE_NODE, |
104 nav_details.Pass()); | 90 nav_details.Pass()); |
105 } | 91 } |
106 } | 92 } |
107 } | 93 } |
108 | 94 |
109 void ProcessPendingViewColor(uint32 view_id) { | |
110 RootMap::iterator root = roots_.find(view_id); | |
111 if (root == roots_.end()) | |
112 return; | |
113 | |
114 PendingViewColors::iterator color = pending_view_colors_.find(view_id); | |
115 if (color == pending_view_colors_.end()) | |
116 return; | |
117 | |
118 root->second->SetColor(color->second); | |
119 pending_view_colors_.erase(color); | |
120 } | |
121 | |
122 InterfaceFactoryImplWithContext<NavigatorImpl, EmbeddedApp> | |
123 navigator_factory_; | |
124 | |
125 ViewManager* view_manager_; | |
126 NavigatorHostPtr navigator_host_; | 95 NavigatorHostPtr navigator_host_; |
127 ViewManagerClientFactory view_manager_client_factory_; | 96 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
128 | 97 |
129 typedef std::map<Id, View*> RootMap; | 98 typedef std::map<Id, View*> RootMap; |
130 RootMap roots_; | 99 RootMap roots_; |
131 | 100 |
132 // We can receive navigations for views we don't have yet. | 101 int next_color_; |
133 typedef std::map<uint32, SkColor> PendingViewColors; | |
134 PendingViewColors pending_view_colors_; | |
135 | 102 |
136 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 103 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
137 }; | 104 }; |
138 | 105 |
139 void NavigatorImpl::Navigate(uint32 view_id, | |
140 NavigationDetailsPtr navigation_details, | |
141 ResponseDetailsPtr response_details) { | |
142 GURL url(navigation_details->request->url.To<std::string>()); | |
143 if (!url.is_valid()) { | |
144 LOG(ERROR) << "URL is invalid."; | |
145 return; | |
146 } | |
147 // TODO(aa): Verify new URL is same origin as current origin. | |
148 SkColor color = 0x00; | |
149 if (!base::HexStringToUInt(url.path().substr(1), &color)) { | |
150 LOG(ERROR) << "Invalid URL, path not convertible to integer"; | |
151 return; | |
152 } | |
153 app_->SetViewColor(view_id, color); | |
154 } | |
155 | |
156 } // namespace examples | 106 } // namespace examples |
157 } // namespace mojo | 107 } // namespace mojo |
158 | 108 |
159 MojoResult MojoMain(MojoHandle shell_handle) { | 109 MojoResult MojoMain(MojoHandle shell_handle) { |
160 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); | 110 mojo::ApplicationRunnerChromium runner(new mojo::examples::EmbeddedApp); |
161 return runner.Run(shell_handle); | 111 return runner.Run(shell_handle); |
162 } | 112 } |
OLD | NEW |