Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / android_webview / browser / renderer_host / aw_render_view_host_ext.cc
blobbdbfccd18d222bf7c5b55a88e01cfafd2b1d5b81
1 // Copyright (c) 2012 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.
5 #include "android_webview/browser/renderer_host/aw_render_view_host_ext.h"
7 #include "android_webview/browser/aw_browser_context.h"
8 #include "android_webview/browser/scoped_allow_wait_for_legacy_web_view_api.h"
9 #include "android_webview/common/render_view_messages.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "content/public/browser/android/content_view_core.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/user_metrics.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/frame_navigate_params.h"
21 namespace android_webview {
23 AwRenderViewHostExt::AwRenderViewHostExt(
24 AwRenderViewHostExtClient* client, content::WebContents* contents)
25 : content::WebContentsObserver(contents),
26 client_(client),
27 background_color_(SK_ColorWHITE),
28 has_new_hit_test_data_(false) {
29 DCHECK(client_);
32 AwRenderViewHostExt::~AwRenderViewHostExt() {}
34 void AwRenderViewHostExt::DocumentHasImages(DocumentHasImagesResult result) {
35 DCHECK(CalledOnValidThread());
36 if (!web_contents()->GetRenderViewHost()) {
37 result.Run(false);
38 return;
40 static int next_id = 1;
41 int this_id = next_id++;
42 pending_document_has_images_requests_[this_id] = result;
43 Send(new AwViewMsg_DocumentHasImages(web_contents()->GetRoutingID(),
44 this_id));
47 void AwRenderViewHostExt::ClearCache() {
48 DCHECK(CalledOnValidThread());
49 Send(new AwViewMsg_ClearCache);
52 bool AwRenderViewHostExt::HasNewHitTestData() const {
53 return has_new_hit_test_data_;
56 void AwRenderViewHostExt::MarkHitTestDataRead() {
57 has_new_hit_test_data_ = false;
60 void AwRenderViewHostExt::RequestNewHitTestDataAt(
61 const gfx::PointF& touch_center,
62 const gfx::SizeF& touch_area) {
63 DCHECK(CalledOnValidThread());
64 Send(new AwViewMsg_DoHitTest(web_contents()->GetRoutingID(), touch_center,
65 touch_area));
68 const AwHitTestData& AwRenderViewHostExt::GetLastHitTestData() const {
69 DCHECK(CalledOnValidThread());
70 return last_hit_test_data_;
73 void AwRenderViewHostExt::SetTextZoomFactor(float factor) {
74 DCHECK(CalledOnValidThread());
75 Send(new AwViewMsg_SetTextZoomFactor(web_contents()->GetRoutingID(), factor));
78 void AwRenderViewHostExt::ResetScrollAndScaleState() {
79 DCHECK(CalledOnValidThread());
80 Send(new AwViewMsg_ResetScrollAndScaleState(web_contents()->GetRoutingID()));
83 void AwRenderViewHostExt::SetInitialPageScale(double page_scale_factor) {
84 DCHECK(CalledOnValidThread());
85 Send(new AwViewMsg_SetInitialPageScale(web_contents()->GetRoutingID(),
86 page_scale_factor));
89 void AwRenderViewHostExt::SetBackgroundColor(SkColor c) {
90 if (background_color_ == c)
91 return;
92 background_color_ = c;
93 if (web_contents()->GetRenderViewHost()) {
94 Send(new AwViewMsg_SetBackgroundColor(web_contents()->GetRoutingID(),
95 background_color_));
99 void AwRenderViewHostExt::SetJsOnlineProperty(bool network_up) {
100 Send(new AwViewMsg_SetJsOnlineProperty(network_up));
103 void AwRenderViewHostExt::SmoothScroll(int target_x,
104 int target_y,
105 long duration_ms) {
106 Send(new AwViewMsg_SmoothScroll(web_contents()->GetRoutingID(), target_x,
107 target_y, duration_ms));
110 void AwRenderViewHostExt::RenderViewCreated(
111 content::RenderViewHost* render_view_host) {
112 Send(new AwViewMsg_SetBackgroundColor(web_contents()->GetRoutingID(),
113 background_color_));
116 void AwRenderViewHostExt::RenderProcessGone(base::TerminationStatus status) {
117 DCHECK(CalledOnValidThread());
118 for (std::map<int, DocumentHasImagesResult>::iterator pending_req =
119 pending_document_has_images_requests_.begin();
120 pending_req != pending_document_has_images_requests_.end();
121 ++pending_req) {
122 pending_req->second.Run(false);
126 void AwRenderViewHostExt::DidNavigateAnyFrame(
127 content::RenderFrameHost* render_frame_host,
128 const content::LoadCommittedDetails& details,
129 const content::FrameNavigateParams& params) {
130 DCHECK(CalledOnValidThread());
132 AwBrowserContext::FromWebContents(web_contents())
133 ->AddVisitedURLs(params.redirects);
136 bool AwRenderViewHostExt::OnMessageReceived(const IPC::Message& message) {
137 bool handled = true;
138 IPC_BEGIN_MESSAGE_MAP(AwRenderViewHostExt, message)
139 IPC_MESSAGE_HANDLER(AwViewHostMsg_DocumentHasImagesResponse,
140 OnDocumentHasImagesResponse)
141 IPC_MESSAGE_HANDLER(AwViewHostMsg_UpdateHitTestData,
142 OnUpdateHitTestData)
143 IPC_MESSAGE_HANDLER(AwViewHostMsg_PageScaleFactorChanged,
144 OnPageScaleFactorChanged)
145 IPC_MESSAGE_HANDLER(AwViewHostMsg_OnContentsSizeChanged,
146 OnContentsSizeChanged)
147 IPC_MESSAGE_UNHANDLED(handled = false)
148 IPC_END_MESSAGE_MAP()
150 return handled ? true : WebContentsObserver::OnMessageReceived(message);
153 void AwRenderViewHostExt::OnDocumentHasImagesResponse(int msg_id,
154 bool has_images) {
155 DCHECK(CalledOnValidThread());
156 std::map<int, DocumentHasImagesResult>::iterator pending_req =
157 pending_document_has_images_requests_.find(msg_id);
158 if (pending_req == pending_document_has_images_requests_.end()) {
159 DLOG(WARNING) << "unexpected DocumentHasImages Response: " << msg_id;
160 } else {
161 pending_req->second.Run(has_images);
162 pending_document_has_images_requests_.erase(pending_req);
166 void AwRenderViewHostExt::OnUpdateHitTestData(
167 const AwHitTestData& hit_test_data) {
168 DCHECK(CalledOnValidThread());
169 last_hit_test_data_ = hit_test_data;
170 has_new_hit_test_data_ = true;
173 void AwRenderViewHostExt::OnPageScaleFactorChanged(float page_scale_factor) {
174 client_->OnWebLayoutPageScaleFactorChanged(page_scale_factor);
177 void AwRenderViewHostExt::OnContentsSizeChanged(
178 const gfx::Size& contents_size) {
179 client_->OnWebLayoutContentsSizeChanged(contents_size);
182 } // namespace android_webview