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/strings/stringprintf.h" |
7 #include "mojo/examples/window_manager/window_manager.mojom.h" | 8 #include "mojo/examples/window_manager/window_manager.mojom.h" |
8 #include "mojo/public/cpp/application/application.h" | 9 #include "mojo/public/cpp/application/application.h" |
| 10 #include "mojo/services/navigation/navigation.mojom.h" |
9 #include "mojo/services/public/cpp/view_manager/view.h" | 11 #include "mojo/services/public/cpp/view_manager/view.h" |
10 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
11 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
12 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 14 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
13 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | 15 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
14 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" | 16 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" |
15 #include "ui/events/event_constants.h" | 17 #include "ui/events/event_constants.h" |
| 18 #include "url/gurl.h" |
16 | 19 |
17 using mojo::view_manager::View; | 20 using mojo::view_manager::View; |
18 using mojo::view_manager::ViewManager; | 21 using mojo::view_manager::ViewManager; |
19 using mojo::view_manager::ViewManagerDelegate; | 22 using mojo::view_manager::ViewManagerDelegate; |
20 using mojo::view_manager::ViewObserver; | 23 using mojo::view_manager::ViewObserver; |
21 using mojo::view_manager::ViewTreeNode; | 24 using mojo::view_manager::ViewTreeNode; |
22 using mojo::view_manager::ViewTreeNodeObserver; | 25 using mojo::view_manager::ViewTreeNodeObserver; |
23 | 26 |
24 namespace mojo { | 27 namespace mojo { |
25 namespace examples { | 28 namespace examples { |
26 | 29 |
| 30 namespace { |
| 31 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; |
| 32 } |
| 33 |
27 // An app that embeds another app. | 34 // An app that embeds another app. |
28 class NestingApp : public Application, | 35 class NestingApp : public Application, |
29 public ViewManagerDelegate, | 36 public ViewManagerDelegate, |
30 public ViewObserver { | 37 public ViewObserver { |
31 public: | 38 public: |
32 NestingApp() {} | 39 NestingApp() : nested_(NULL) {} |
33 virtual ~NestingApp() {} | 40 virtual ~NestingApp() {} |
34 | 41 |
35 private: | 42 private: |
| 43 class ViewNavigator : public InterfaceImpl<navigation::ViewNavigator> { |
| 44 public: |
| 45 explicit ViewNavigator(NestingApp* app) : app_(app) {} |
| 46 |
| 47 private: |
| 48 virtual void Navigate(uint32 node_id, |
| 49 navigation::NavigationDetailsPtr details) OVERRIDE { |
| 50 GURL url(details->url.To<std::string>()); |
| 51 if (!url.is_valid()) { |
| 52 LOG(ERROR) << "URL is invalid."; |
| 53 return; |
| 54 } |
| 55 app_->color_ = url.path().substr(1); |
| 56 app_->NavigateChild(); |
| 57 } |
| 58 |
| 59 NestingApp* app_; |
| 60 }; |
| 61 |
36 // Overridden from Application: | 62 // Overridden from Application: |
37 virtual void Initialize() MOJO_OVERRIDE { | 63 virtual void Initialize() MOJO_OVERRIDE { |
38 ViewManager::Create(this, this); | 64 ViewManager::Create(this, this); |
39 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); | 65 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); |
| 66 AddService<ViewNavigator>(this); |
40 } | 67 } |
41 | 68 |
42 // Overridden from ViewManagerDelegate: | 69 // Overridden from ViewManagerDelegate: |
43 virtual void OnRootAdded(ViewManager* view_manager, | 70 virtual void OnRootAdded(ViewManager* view_manager, |
44 ViewTreeNode* root) OVERRIDE { | 71 ViewTreeNode* root) OVERRIDE { |
45 View* view = View::Create(view_manager); | 72 View* view = View::Create(view_manager); |
46 root->SetActiveView(view); | 73 root->SetActiveView(view); |
47 view->SetColor(SK_ColorCYAN); | 74 view->SetColor(SK_ColorCYAN); |
48 view->AddObserver(this); | 75 view->AddObserver(this); |
49 | 76 |
50 ViewTreeNode* nested = ViewTreeNode::Create(view_manager); | 77 nested_ = ViewTreeNode::Create(view_manager); |
51 root->AddChild(nested); | 78 root->AddChild(nested_); |
52 nested->SetBounds(gfx::Rect(20, 20, 50, 50)); | 79 nested_->SetBounds(gfx::Rect(20, 20, 50, 50)); |
53 nested->Embed("mojo:mojo_embedded_app"); | 80 nested_->Embed(kEmbeddedAppURL); |
| 81 |
| 82 if (!view_navigator_.get()) |
| 83 ConnectTo(kEmbeddedAppURL, &view_navigator_); |
| 84 |
| 85 NavigateChild(); |
54 } | 86 } |
| 87 |
55 virtual void OnRootRemoved(ViewManager* view_manager, | 88 virtual void OnRootRemoved(ViewManager* view_manager, |
56 ViewTreeNode* root) OVERRIDE { | 89 ViewTreeNode* root) OVERRIDE { |
57 // TODO(beng): reap views & child nodes. | 90 // TODO(beng): reap views & child nodes. |
| 91 nested_ = NULL; |
58 } | 92 } |
59 | 93 |
60 // Overridden from ViewObserver: | 94 // Overridden from ViewObserver: |
61 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 95 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
62 if (event->action == ui::ET_MOUSE_RELEASED) | 96 if (event->action == ui::ET_MOUSE_RELEASED) |
63 window_manager_->CloseWindow(view->node()->id()); | 97 window_manager_->CloseWindow(view->node()->id()); |
64 } | 98 } |
65 | 99 |
| 100 void NavigateChild() { |
| 101 if (!color_.empty() && nested_) { |
| 102 navigation::NavigationDetailsPtr details( |
| 103 navigation::NavigationDetails::New()); |
| 104 details->url = |
| 105 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str()); |
| 106 view_navigator_->Navigate(nested_->id(), details.Pass()); |
| 107 } |
| 108 } |
| 109 |
| 110 std::string color_; |
| 111 ViewTreeNode* nested_; |
| 112 navigation::ViewNavigatorPtr view_navigator_; |
66 IWindowManagerPtr window_manager_; | 113 IWindowManagerPtr window_manager_; |
67 | 114 |
68 DISALLOW_COPY_AND_ASSIGN(NestingApp); | 115 DISALLOW_COPY_AND_ASSIGN(NestingApp); |
69 }; | 116 }; |
70 | 117 |
71 } // namespace examples | 118 } // namespace examples |
72 | 119 |
73 // static | 120 // static |
74 Application* Application::Create() { | 121 Application* Application::Create() { |
75 return new examples::NestingApp; | 122 return new examples::NestingApp; |
76 } | 123 } |
77 | 124 |
78 } // namespace mojo | 125 } // namespace mojo |
OLD | NEW |