1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "mojo/examples/window_manager/window_manager.mojom.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/interface_factory_with_context.h"
#include "mojo/services/public/cpp/view_manager/node.h"
#include "mojo/services/public/cpp/view_manager/node_observer.h"
#include "mojo/services/public/cpp/view_manager/view.h"
#include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
#include "mojo/services/public/cpp/view_manager/view_observer.h"
#include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
#include "ui/events/event_constants.h"
#include "url/gurl.h"
namespace mojo {
namespace examples {
namespace {
const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
}
class NestingApp;
class NavigatorImpl : public InterfaceImpl<Navigator> {
public:
explicit NavigatorImpl(NestingApp* app) : app_(app) {}
private:
virtual void Navigate(
uint32 node_id,
NavigationDetailsPtr navigation_details,
ResponseDetailsPtr response_details) OVERRIDE;
NestingApp* app_;
DISALLOW_COPY_AND_ASSIGN(NavigatorImpl);
};
// An app that embeds another app.
// TODO(davemoore): Is this the right name?
class NestingApp
: public ApplicationDelegate,
public ViewManagerDelegate,
public ViewObserver,
public NodeObserver,
public InterfaceFactoryWithContext<NavigatorImpl, NestingApp> {
public:
NestingApp()
: InterfaceFactoryWithContext(this),
nested_(NULL),
view_manager_client_factory_(this) {}
virtual ~NestingApp() {}
void set_color(const std::string& color) { color_ = color; }
void NavigateChild() {
if (!color_.empty() && nested_) {
NavigationDetailsPtr details(NavigationDetails::New());
details->url =
base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str());
ResponseDetailsPtr response_details(ResponseDetails::New());
navigator_->Navigate(
nested_->id(), details.Pass(), response_details.Pass());
}
}
private:
// Overridden from ApplicationImpl:
virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
MOJO_OVERRIDE {
connection->ConnectToService(&window_manager_);
connection->AddService(&view_manager_client_factory_);
connection->AddService(this);
// TODO(davemoore): Is this ok?
if (!navigator_) {
connection->ConnectToApplication(
kEmbeddedAppURL)->ConnectToService(&navigator_);
}
return true;
}
// Overridden from ViewManagerDelegate:
virtual void OnEmbed(ViewManager* view_manager, Node* root) OVERRIDE {
root->AddObserver(this);
View* view = View::Create(view_manager);
root->SetActiveView(view);
view->SetColor(SK_ColorCYAN);
view->AddObserver(this);
nested_ = Node::Create(view_manager);
root->AddChild(nested_);
nested_->SetBounds(gfx::Rect(20, 20, 50, 50));
nested_->Embed(kEmbeddedAppURL);
NavigateChild();
}
virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
base::MessageLoop::current()->Quit();
}
// Overridden from ViewObserver:
virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
if (event->action == EVENT_TYPE_MOUSE_RELEASED)
window_manager_->CloseWindow(view->node()->id());
}
// Overridden from NodeObserver:
virtual void OnNodeDestroyed(Node* node) OVERRIDE {
// TODO(beng): reap views & child nodes.
nested_ = NULL;
}
std::string color_;
Node* nested_;
NavigatorPtr navigator_;
IWindowManagerPtr window_manager_;
ViewManagerClientFactory view_manager_client_factory_;
DISALLOW_COPY_AND_ASSIGN(NestingApp);
};
void NavigatorImpl::Navigate(uint32 node_id,
NavigationDetailsPtr navigation_details,
ResponseDetailsPtr response_details) {
GURL url(navigation_details->url.To<std::string>());
if (!url.is_valid()) {
LOG(ERROR) << "URL is invalid.";
return;
}
app_->set_color(url.path().substr(1));
app_->NavigateChild();
}
} // namespace examples
// static
ApplicationDelegate* ApplicationDelegate::Create() {
return new examples::NestingApp;
}
} // namespace mojo
|