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 Navigator : public InterfaceImpl<navigation::Navigator> { |
| 44 public: |
| 45 explicit Navigator(NestingApp* app) : app_(app) {} |
| 46 private: |
| 47 virtual void Navigate(uint32 node_id, |
| 48 navigation::NavigationDetailsPtr details) OVERRIDE { |
| 49 GURL url(details->url.To<std::string>()); |
| 50 if (!url.is_valid()) { |
| 51 LOG(ERROR) << "URL is invalid."; |
| 52 return; |
| 53 } |
| 54 app_->color_ = url.path().substr(1); |
| 55 app_->NavigateChild(); |
| 56 } |
| 57 NestingApp* app_; |
| 58 DISALLOW_COPY_AND_ASSIGN(Navigator); |
| 59 }; |
| 60 |
36 // Overridden from Application: | 61 // Overridden from Application: |
37 virtual void Initialize() MOJO_OVERRIDE { | 62 virtual void Initialize() MOJO_OVERRIDE { |
38 ViewManager::Create(this, this); | 63 ViewManager::Create(this, this); |
39 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); | 64 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); |
| 65 AddService<Navigator>(this); |
40 } | 66 } |
41 | 67 |
42 // Overridden from ViewManagerDelegate: | 68 // Overridden from ViewManagerDelegate: |
43 virtual void OnRootAdded(ViewManager* view_manager, | 69 virtual void OnRootAdded(ViewManager* view_manager, |
44 ViewTreeNode* root) OVERRIDE { | 70 ViewTreeNode* root) OVERRIDE { |
45 View* view = View::Create(view_manager); | 71 View* view = View::Create(view_manager); |
46 root->SetActiveView(view); | 72 root->SetActiveView(view); |
47 view->SetColor(SK_ColorCYAN); | 73 view->SetColor(SK_ColorCYAN); |
48 view->AddObserver(this); | 74 view->AddObserver(this); |
49 | 75 |
50 ViewTreeNode* nested = ViewTreeNode::Create(view_manager); | 76 nested_ = ViewTreeNode::Create(view_manager); |
51 root->AddChild(nested); | 77 root->AddChild(nested_); |
52 nested->SetBounds(gfx::Rect(20, 20, 50, 50)); | 78 nested_->SetBounds(gfx::Rect(20, 20, 50, 50)); |
53 nested->Embed("mojo:mojo_embedded_app"); | 79 nested_->Embed(kEmbeddedAppURL); |
| 80 |
| 81 if (!navigator_.get()) |
| 82 ConnectTo(kEmbeddedAppURL, &navigator_); |
| 83 |
| 84 NavigateChild(); |
54 } | 85 } |
| 86 |
55 virtual void OnRootRemoved(ViewManager* view_manager, | 87 virtual void OnRootRemoved(ViewManager* view_manager, |
56 ViewTreeNode* root) OVERRIDE { | 88 ViewTreeNode* root) OVERRIDE { |
57 // TODO(beng): reap views & child nodes. | 89 // TODO(beng): reap views & child nodes. |
| 90 nested_ = NULL; |
58 } | 91 } |
59 | 92 |
60 // Overridden from ViewObserver: | 93 // Overridden from ViewObserver: |
61 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 94 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
62 if (event->action == ui::ET_MOUSE_RELEASED) | 95 if (event->action == ui::ET_MOUSE_RELEASED) |
63 window_manager_->CloseWindow(view->node()->id()); | 96 window_manager_->CloseWindow(view->node()->id()); |
64 } | 97 } |
65 | 98 |
| 99 void NavigateChild() { |
| 100 if (!color_.empty() && nested_) { |
| 101 navigation::NavigationDetailsPtr details( |
| 102 navigation::NavigationDetails::New()); |
| 103 details->url = |
| 104 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str()); |
| 105 navigator_->Navigate(nested_->id(), details.Pass()); |
| 106 } |
| 107 } |
| 108 |
| 109 std::string color_; |
| 110 ViewTreeNode* nested_; |
| 111 navigation::NavigatorPtr navigator_; |
66 IWindowManagerPtr window_manager_; | 112 IWindowManagerPtr window_manager_; |
67 | 113 |
68 DISALLOW_COPY_AND_ASSIGN(NestingApp); | 114 DISALLOW_COPY_AND_ASSIGN(NestingApp); |
69 }; | 115 }; |
70 | 116 |
71 } // namespace examples | 117 } // namespace examples |
72 | 118 |
73 // static | 119 // static |
74 Application* Application::Create() { | 120 Application* Application::Create() { |
75 return new examples::NestingApp; | 121 return new examples::NestingApp; |
76 } | 122 } |
77 | 123 |
78 } // namespace mojo | 124 } // namespace mojo |
OLD | NEW |