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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
// Copyright 2015 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 "blimp/engine/browser/blimp_engine_session.h"
#include "base/lazy_instance.h"
#include "base/strings/utf_string_conversions.h"
#include "blimp/common/create_blimp_message.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/common/proto/control.pb.h"
#include "blimp/engine/browser/blimp_browser_context.h"
#include "blimp/engine/ui/blimp_layout_manager.h"
#include "blimp/engine/ui/blimp_screen.h"
#include "blimp/engine/ui/blimp_ui_context_factory.h"
#include "blimp/net/blimp_connection.h"
#include "blimp/net/blimp_message_multiplexer.h"
#include "blimp/net/null_blimp_message_processor.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_errors.h"
#include "ui/aura/client/default_capture_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/gfx/geometry/size.h"
#include "ui/wm/core/base_focus_rules.h"
#include "ui/wm/core/default_activation_client.h"
#include "ui/wm/core/focus_controller.h"
#if !defined(USE_X11)
#include "blimp/engine/ui/blimp_window_tree_host.h"
#endif
namespace blimp {
namespace engine {
namespace {
const int kDummyTabId = 0;
const float kDefaultScaleFactor = 1.f;
const int kDefaultDisplayWidth = 800;
const int kDefaultDisplayHeight = 600;
base::LazyInstance<blimp::NullBlimpMessageProcessor> g_blimp_message_processor =
LAZY_INSTANCE_INITIALIZER;
// Focus rules that support activating an child window.
class FocusRulesImpl : public wm::BaseFocusRules {
public:
FocusRulesImpl() {}
~FocusRulesImpl() override {}
bool SupportsChildActivation(aura::Window* window) const override {
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl);
};
} // namespace
BlimpEngineSession::BlimpEngineSession(
scoped_ptr<BlimpBrowserContext> browser_context)
: browser_context_(std::move(browser_context)),
screen_(new BlimpScreen),
// TODO(dtrainor, haibinlu): Properly pull these from the BlimpMessageMux.
render_widget_processor_(g_blimp_message_processor.Pointer(),
g_blimp_message_processor.Pointer()) {
screen_->UpdateDisplayScaleAndSize(kDefaultScaleFactor,
gfx::Size(kDefaultDisplayWidth,
kDefaultDisplayHeight));
render_widget_processor_.SetDelegate(kDummyTabId, this);
}
BlimpEngineSession::~BlimpEngineSession() {
render_widget_processor_.RemoveDelegate(kDummyTabId);
}
void BlimpEngineSession::Initialize() {
DCHECK(!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
#if defined(USE_X11)
window_tree_host_.reset(aura::WindowTreeHost::Create(
gfx::Rect(screen_->GetPrimaryDisplay().size())));
#else
context_factory_.reset(new BlimpUiContextFactory());
aura::Env::GetInstance()->set_context_factory(context_factory_.get());
window_tree_host_.reset(new BlimpWindowTreeHost());
#endif
window_tree_host_->InitHost();
window_tree_host_->window()->SetLayoutManager(
new BlimpLayoutManager(window_tree_host_->window()));
focus_client_.reset(new wm::FocusController(new FocusRulesImpl));
aura::client::SetFocusClient(window_tree_host_->window(),
focus_client_.get());
aura::client::SetActivationClient(window_tree_host_->window(),
focus_client_.get());
capture_client_.reset(
new aura::client::DefaultCaptureClient(window_tree_host_->window()));
#if defined(USE_X11)
window_tree_host_->Show();
#endif
window_tree_host_->SetBounds(gfx::Rect(screen_->GetPrimaryDisplay().size()));
}
void BlimpEngineSession::CreateWebContents(const int target_tab_id) {
// TODO(haibinlu): Support more than one active WebContents (crbug/547231).
DCHECK(!web_contents_);
content::WebContents::CreateParams create_params(browser_context_.get(),
nullptr);
scoped_ptr<content::WebContents> new_contents =
make_scoped_ptr(content::WebContents::Create(create_params));
PlatformSetContents(std::move(new_contents));
}
void BlimpEngineSession::CloseWebContents(const int target_tab_id) {
DCHECK(web_contents_);
web_contents_->Close();
}
void BlimpEngineSession::HandleResize(float device_pixel_ratio,
const gfx::Size& size) {
screen_->UpdateDisplayScaleAndSize(device_pixel_ratio, size);
if (web_contents_ && web_contents_->GetRenderViewHost() &&
web_contents_->GetRenderViewHost()->GetWidget()) {
web_contents_->GetRenderViewHost()->GetWidget()->WasResized();
}
}
void BlimpEngineSession::LoadUrl(const int target_tab_id, const GURL& url) {
if (url.is_empty() || !web_contents_)
return;
// TODO(dtrainor, haibinlu): Fix up the URL with url_fixer.h. If that doesn't
// produce a valid spec() then try to build a search query?
content::NavigationController::LoadURLParams params(url);
params.transition_type = ui::PageTransitionFromInt(
ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
web_contents_->GetController().LoadURLWithParams(params);
web_contents_->Focus();
}
void BlimpEngineSession::GoBack(const int target_tab_id) {
if (!web_contents_)
return;
web_contents_->GetController().GoBack();
}
void BlimpEngineSession::GoForward(const int target_tab_id) {
if (!web_contents_)
return;
web_contents_->GetController().GoForward();
}
void BlimpEngineSession::Reload(const int target_tab_id) {
if (!web_contents_)
return;
web_contents_->GetController().Reload(true);
}
void BlimpEngineSession::OnWebInputEvent(
scoped_ptr<blink::WebInputEvent> event) {
if (!web_contents_ || !web_contents_->GetRenderViewHost())
return;
// TODO(dtrainor): Send the input event directly to the render process?
}
void BlimpEngineSession::OnCompositorMessageReceived(
const std::vector<uint8_t>& message) {
// Make sure that We actually have a valid WebContents and RenderViewHost.
if (!web_contents_ || !web_contents_->GetRenderViewHost())
return;
content::RenderWidgetHost* host =
web_contents_->GetRenderViewHost()->GetWidget();
// Make sure we actually have a valid RenderWidgetHost.
if (!host)
return;
host->HandleCompositorProto(message);
}
void BlimpEngineSession::ProcessMessage(
scoped_ptr<BlimpMessage> message,
const net::CompletionCallback& callback) {
DCHECK(message->type() == BlimpMessage::CONTROL ||
message->type() == BlimpMessage::NAVIGATION);
if (message->type() == BlimpMessage::CONTROL) {
switch (message->control().type()) {
case ControlMessage::CREATE_TAB:
CreateWebContents(message->target_tab_id());
break;
case ControlMessage::CLOSE_TAB:
CloseWebContents(message->target_tab_id());
case ControlMessage::SIZE:
HandleResize(message->control().size().device_pixel_ratio(),
gfx::Size(message->control().size().width(),
message->control().size().height()));
break;
default:
NOTIMPLEMENTED();
}
} else if (message->type() == BlimpMessage::NAVIGATION && web_contents_) {
switch (message->navigation().type()) {
case NavigationMessage::LOAD_URL:
LoadUrl(message->target_tab_id(),
GURL(message->navigation().load_url().url()));
break;
case NavigationMessage::GO_BACK:
GoBack(message->target_tab_id());
break;
case NavigationMessage::GO_FORWARD:
GoForward(message->target_tab_id());
break;
case NavigationMessage::RELOAD:
Reload(message->target_tab_id());
break;
default:
NOTIMPLEMENTED();
}
}
if (!callback.is_null()) {
callback.Run(net::OK);
}
}
void BlimpEngineSession::AddNewContents(content::WebContents* source,
content::WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_rect,
bool user_gesture,
bool* was_blocked) {
NOTIMPLEMENTED();
}
content::WebContents* BlimpEngineSession::OpenURLFromTab(
content::WebContents* source,
const content::OpenURLParams& params) {
// CURRENT_TAB is the only one we implement for now.
if (params.disposition != CURRENT_TAB) {
NOTIMPLEMENTED();
return nullptr;
}
// TODO(haibinlu): Add helper method to get LoadURLParams from OpenURLParams.
content::NavigationController::LoadURLParams load_url_params(params.url);
load_url_params.source_site_instance = params.source_site_instance;
load_url_params.referrer = params.referrer;
load_url_params.frame_tree_node_id = params.frame_tree_node_id;
load_url_params.transition_type = params.transition;
load_url_params.extra_headers = params.extra_headers;
load_url_params.should_replace_current_entry =
params.should_replace_current_entry;
if (params.transferred_global_request_id != content::GlobalRequestID()) {
// If transferred_global_request_id is set, then
// the navigation is being transferred from one RenderViewHost to another.
// Preserve the request-id and renderer-initiated flag.
load_url_params.is_renderer_initiated = params.is_renderer_initiated;
load_url_params.transferred_global_request_id =
params.transferred_global_request_id;
} else if (params.is_renderer_initiated) {
load_url_params.is_renderer_initiated = true;
}
source->GetController().LoadURLWithParams(load_url_params);
return source;
}
void BlimpEngineSession::RequestToLockMouse(content::WebContents* web_contents,
bool user_gesture,
bool last_unlocked_by_target) {
web_contents->GotResponseToLockMouseRequest(true);
}
void BlimpEngineSession::CloseContents(content::WebContents* source) {
if (source == web_contents_.get()) {
Observe(nullptr);
web_contents_.reset();
}
}
void BlimpEngineSession::ActivateContents(content::WebContents* contents) {
contents->GetRenderViewHost()->GetWidget()->Focus();
}
void BlimpEngineSession::ForwardCompositorProto(
const std::vector<uint8_t>& proto) {
render_widget_processor_.SendCompositorMessage(kDummyTabId, proto);
}
void BlimpEngineSession::NavigationStateChanged(
content::WebContents* source,
content::InvalidateTypes changed_flags) {
if (source != web_contents_.get() || !changed_flags)
return;
NavigationMessage* navigation_message;
scoped_ptr<BlimpMessage> message =
CreateBlimpMessage(&navigation_message, kDummyTabId);
navigation_message->set_type(NavigationMessage::NAVIGATION_STATE_CHANGED);
NavigationStateChangeMessage* details =
navigation_message->mutable_navigation_state_change();
if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_URL)
details->set_url(source->GetURL().spec());
if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_TAB) {
// TODO(dtrainor): Serialize the favicon?
NOTIMPLEMENTED();
}
if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_TITLE)
details->set_title(base::UTF16ToUTF8(source->GetTitle()));
if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_LOAD)
details->set_loading(source->IsLoading());
// TODO(dtrainor, haibinlu): Send to the right BlimpMessageProcessor.
g_blimp_message_processor.Pointer()->ProcessMessage(
std::move(message),
net::CompletionCallback());
}
void BlimpEngineSession::RenderViewHostChanged(
content::RenderViewHost* old_host,
content::RenderViewHost* new_host) {
render_widget_processor_.OnRenderWidgetInitialized(kDummyTabId);
}
void BlimpEngineSession::PlatformSetContents(
scoped_ptr<content::WebContents> new_contents) {
new_contents->SetDelegate(this);
Observe(new_contents.get());
web_contents_ = std::move(new_contents);
aura::Window* parent = window_tree_host_->window();
aura::Window* content = web_contents_->GetNativeView();
if (!parent->Contains(content))
parent->AddChild(content);
content->Show();
}
} // namespace engine
} // namespace blimp
|