OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/engine/session/tab.h" | |
6 | |
7 #include "base/strings/utf_string_conversions.h" | |
8 #include "base/trace_event/trace_event.h" | |
9 #include "blimp/common/create_blimp_message.h" | |
10 #include "blimp/common/proto/blimp_message.pb.h" | |
11 #include "blimp/common/proto/render_widget.pb.h" | |
12 #include "blimp/common/proto/tab_control.pb.h" | |
13 #include "blimp/engine/common/blimp_user_agent.h" | |
14 #include "blimp/engine/feature/engine_render_widget_feature.h" | |
15 #include "blimp/net/blimp_message_processor.h" | |
16 #include "content/public/browser/navigation_controller.h" | |
17 #include "content/public/browser/navigation_entry.h" | |
18 #include "content/public/browser/render_view_host.h" | |
19 #include "content/public/browser/render_widget_host.h" | |
20 #include "content/public/browser/render_widget_host_view.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "content/public/common/form_field_data.h" | |
23 #include "content/public/common/renderer_preferences.h" | |
24 #include "ui/aura/window.h" | |
25 #include "ui/gfx/geometry/size.h" | |
26 | |
27 namespace blimp { | |
28 namespace engine { | |
29 | |
30 Tab::Tab(std::unique_ptr<content::WebContents> web_contents, | |
31 const int tab_id, | |
32 EngineRenderWidgetFeature* render_widget_feature, | |
33 BlimpMessageProcessor* navigation_message_sender) | |
34 : web_contents_(std::move(web_contents)), | |
35 tab_id_(tab_id), | |
36 render_widget_feature_(render_widget_feature), | |
37 navigation_message_sender_(navigation_message_sender), | |
38 page_load_tracker_(web_contents_.get(), this), | |
39 current_form_request_id_(0), | |
40 weak_factory_(this) { | |
41 DCHECK(render_widget_feature_); | |
42 DCHECK(navigation_message_sender_); | |
43 | |
44 // A Tab is created upon client's request, thus an updated | |
45 // user agent info (containing client OS info) is available, and we will use | |
46 // that to override user agent string from BlimpContentRendererClient. | |
47 web_contents_->SetUserAgentOverride(GetBlimpEngineUserAgent()); | |
48 | |
49 render_widget_feature_->SetDelegate(tab_id_, this); | |
50 | |
51 Observe(web_contents_.get()); | |
52 } | |
53 | |
54 Tab::~Tab() { | |
55 render_widget_feature_->RemoveDelegate(tab_id_); | |
56 } | |
57 | |
58 void Tab::Resize(float device_pixel_ratio, const gfx::Size& size_in_dips) { | |
59 DVLOG(1) << "Resize to " << size_in_dips.ToString() << ", " | |
60 << device_pixel_ratio; | |
61 web_contents_->GetNativeView()->SetBounds(gfx::Rect(size_in_dips)); | |
62 | |
63 if (web_contents_->GetRenderViewHost() && | |
64 web_contents_->GetRenderViewHost()->GetWidget()) { | |
65 web_contents_->GetRenderViewHost()->GetWidget()->WasResized(); | |
66 } | |
67 } | |
68 | |
69 void Tab::LoadUrl(const GURL& url) { | |
70 TRACE_EVENT1("blimp", "Tab::LoadUrl", "URL", url.spec()); | |
71 DVLOG(1) << "Load URL " << url << " in tab " << tab_id_; | |
72 if (!url.is_valid()) { | |
73 VLOG(1) << "Dropping invalid URL " << url; | |
74 return; | |
75 } | |
76 | |
77 content::NavigationController::LoadURLParams params(url); | |
78 params.transition_type = ui::PageTransitionFromInt( | |
79 ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR); | |
80 params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; | |
81 web_contents_->GetController().LoadURLWithParams(params); | |
82 web_contents_->Focus(); | |
83 } | |
84 | |
85 void Tab::GoBack() { | |
86 if (!web_contents_->GetController().CanGoBack()) { | |
87 DLOG(ERROR) << "Ignoring back in tab " << tab_id_; | |
88 return; | |
89 } | |
90 DVLOG(1) << "Back in tab " << tab_id_; | |
91 web_contents_->GetController().GoBack(); | |
92 } | |
93 | |
94 void Tab::GoForward() { | |
95 if (!web_contents_->GetController().CanGoForward()) { | |
96 DLOG(ERROR) << "Ignoring forward in tab " << tab_id_; | |
97 return; | |
98 } | |
99 DVLOG(1) << "Forward in tab " << tab_id_; | |
100 web_contents_->GetController().GoForward(); | |
101 } | |
102 | |
103 void Tab::Reload() { | |
104 DVLOG(1) << "Reload in tab " << tab_id_; | |
105 web_contents_->GetController().Reload(content::ReloadType::NORMAL, true); | |
106 } | |
107 | |
108 void Tab::RenderViewCreated(content::RenderViewHost* render_view_host) { | |
109 DCHECK(render_view_host); | |
110 web_contents_->GetMutableRendererPrefs()->caret_blink_interval = 0; | |
111 render_view_host->SyncRendererPrefs(); | |
112 | |
113 render_widget_feature_->OnRenderWidgetCreated(tab_id_, | |
114 render_view_host->GetWidget()); | |
115 } | |
116 | |
117 void Tab::RenderViewHostChanged(content::RenderViewHost* old_host, | |
118 content::RenderViewHost* new_host) { | |
119 render_widget_feature_->OnRenderWidgetInitialized(tab_id_, | |
120 new_host->GetWidget()); | |
121 } | |
122 | |
123 void Tab::RenderViewDeleted(content::RenderViewHost* render_view_host) { | |
124 render_widget_feature_->OnRenderWidgetDeleted(tab_id_, | |
125 render_view_host->GetWidget()); | |
126 } | |
127 | |
128 void Tab::NavigationStateChanged(content::InvalidateTypes changed_flags) { | |
129 DCHECK(changed_flags); | |
130 | |
131 NavigationMessage* navigation_message; | |
132 std::unique_ptr<BlimpMessage> message = | |
133 CreateBlimpMessage(&navigation_message, tab_id_); | |
134 navigation_message->set_type(NavigationMessage::NAVIGATION_STATE_CHANGED); | |
135 NavigationStateChangeMessage* details = | |
136 navigation_message->mutable_navigation_state_changed(); | |
137 | |
138 if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_URL) | |
139 details->set_url(web_contents_->GetURL().spec()); | |
140 | |
141 if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_TAB) { | |
142 // TODO(dtrainor): Serialize the favicon? crbug.com/597094. | |
143 DVLOG(3) << "Tab favicon changed"; | |
144 } | |
145 | |
146 if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_TITLE) | |
147 details->set_title(base::UTF16ToUTF8(web_contents_->GetTitle())); | |
148 | |
149 if (changed_flags & content::InvalidateTypes::INVALIDATE_TYPE_LOAD) | |
150 details->set_loading(web_contents_->IsLoading()); | |
151 | |
152 navigation_message_sender_->ProcessMessage(std::move(message), | |
153 net::CompletionCallback()); | |
154 } | |
155 | |
156 void Tab::SendPageLoadStatusUpdate(PageLoadStatus load_status) { | |
157 NavigationMessage* navigation_message = nullptr; | |
158 std::unique_ptr<BlimpMessage> message = | |
159 CreateBlimpMessage(&navigation_message, tab_id_); | |
160 navigation_message->set_type(NavigationMessage::NAVIGATION_STATE_CHANGED); | |
161 navigation_message->mutable_navigation_state_changed() | |
162 ->set_page_load_completed(load_status == PageLoadStatus::LOADED); | |
163 | |
164 navigation_message_sender_->ProcessMessage(std::move(message), | |
165 net::CompletionCallback()); | |
166 } | |
167 | |
168 void Tab::OnWebGestureEvent(content::RenderWidgetHost* render_widget_host, | |
169 std::unique_ptr<blink::WebGestureEvent> event) { | |
170 TRACE_EVENT1("blimp", "Tab::OnWebGestureEvent", "type", event->type()); | |
171 render_widget_host->ForwardGestureEvent(*event); | |
172 } | |
173 | |
174 void Tab::ShowTextInputUI() { | |
175 current_form_request_id_++; | |
176 content::FormFieldDataCallback callback = | |
177 base::Bind(&Tab::ProcessTextInputInfo, weak_factory_.GetWeakPtr(), | |
178 current_form_request_id_); | |
179 | |
180 content::RenderFrameHost* focused_frame = web_contents()->GetFocusedFrame(); | |
181 if (focused_frame) { | |
182 focused_frame->RequestFocusedFormFieldData(callback); | |
183 } | |
184 } | |
185 | |
186 void Tab::HideTextInputUI() { | |
187 current_form_request_id_++; | |
188 render_widget_feature_->SendHideImeRequest( | |
189 tab_id(), | |
190 web_contents()->GetRenderWidgetHostView()->GetRenderWidgetHost()); | |
191 } | |
192 | |
193 void Tab::ProcessTextInputInfo(int request_id, | |
194 const content::FormFieldData& field) { | |
195 if (field.text_input_type == ui::TEXT_INPUT_TYPE_NONE) | |
196 return; | |
197 | |
198 // Discard the results for old requests. | |
199 if (request_id < current_form_request_id_) { | |
200 return; | |
201 } | |
202 | |
203 // TODO(shaktisahu): Remove adding RenderWidgetHost info to the proto. | |
204 render_widget_feature_->SendShowImeRequest( | |
205 tab_id(), | |
206 web_contents()->GetRenderWidgetHostView()->GetRenderWidgetHost(), field); | |
207 } | |
208 | |
209 void Tab::OnCompositorMessageReceived( | |
210 content::RenderWidgetHost* render_widget_host, | |
211 const std::vector<uint8_t>& message) { | |
212 TRACE_EVENT0("blimp", "Tab::OnCompositorMessageReceived"); | |
213 | |
214 render_widget_host->HandleCompositorProto(message); | |
215 } | |
216 | |
217 } // namespace engine | |
218 } // namespace blimp | |
OLD | NEW |