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/renderer/aw_render_view_ext.h"
9 #include "android_webview/common/aw_hit_test_data.h"
10 #include "android_webview/common/render_view_messages.h"
11 #include "base/string_piece.h"
12 #include "content/public/common/url_constants.h"
13 #include "content/public/renderer/android_content_detection_prefixes.h"
14 #include "content/public/renderer/document_state.h"
15 #include "content/public/renderer/render_view.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebHitTestResult.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
28 namespace android_webview
{
32 bool RemovePrefixAndAssignIfMatches(const base::StringPiece
& prefix
,
35 const base::StringPiece
spec(url
.spec());
37 if (spec
.starts_with(prefix
)) {
38 dest
->assign(spec
.begin() + prefix
.length(), spec
.end());
46 AwRenderViewExt::AwRenderViewExt(content::RenderView
* render_view
)
47 : content::RenderViewObserver(render_view
) {
48 render_view
->GetWebView()->setPermissionClient(this);
51 AwRenderViewExt::~AwRenderViewExt() {}
54 void AwRenderViewExt::RenderViewCreated(content::RenderView
* render_view
) {
55 new AwRenderViewExt(render_view
); // |render_view| takes ownership.
58 bool AwRenderViewExt::OnMessageReceived(const IPC::Message
& message
) {
60 IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt
, message
)
61 IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages
, OnDocumentHasImagesRequest
)
62 IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest
, OnDoHitTest
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
68 void AwRenderViewExt::OnDocumentHasImagesRequest(int id
) {
69 bool hasImages
= false;
71 WebKit::WebView
* webview
= render_view()->GetWebView();
73 WebKit::WebVector
<WebKit::WebElement
> images
;
74 webview
->mainFrame()->document().images(images
);
75 hasImages
= !images
.isEmpty();
78 Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id
,
82 bool AwRenderViewExt::allowImage(WebKit::WebFrame
* frame
,
83 bool enabled_per_settings
,
84 const WebKit::WebURL
& image_url
) {
85 // Implementing setBlockNetworkImages, so allow local scheme images to be
87 if (enabled_per_settings
)
90 // For compatibility, only blacklist network schemes instead of whitelisting.
91 const GURL
url(image_url
);
92 return !(url
.SchemeIs(chrome::kHttpScheme
) ||
93 url
.SchemeIs(chrome::kHttpsScheme
) ||
94 url
.SchemeIs(chrome::kFtpScheme
));
97 void AwRenderViewExt::DidCommitProvisionalLoad(WebKit::WebFrame
* frame
,
98 bool is_new_navigation
) {
99 content::DocumentState
* document_state
=
100 content::DocumentState::FromDataSource(frame
->dataSource());
101 if (document_state
->can_load_local_resources()) {
102 WebKit::WebSecurityOrigin origin
= frame
->document().securityOrigin();
103 origin
.grantLoadLocalResources();
107 void AwRenderViewExt::FocusedNodeChanged(const WebKit::WebNode
& node
) {
108 if (!node
.isNull()) {
109 if (node
.isTextNode() && node
.isContentEditable()) {
111 data
.type
= AwHitTestData::EDIT_TEXT_TYPE
;
112 Send(new AwViewHostMsg_UpdateHitTestData(
113 routing_id(), data
));
115 // TODO(boliu): Implement this path.
116 NOTIMPLEMENTED() << "Tab focused links not implemented";
121 void AwRenderViewExt::OnDoHitTest(int view_x
, int view_y
) {
122 if (!render_view() || !render_view()->GetWebView())
125 const WebKit::WebHitTestResult result
=
126 render_view()->GetWebView()->hitTestResultAt(
127 WebKit::WebPoint(view_x
, view_y
));
130 // Populate fixed AwHitTestData fields.
131 if (result
.absoluteImageURL().isValid())
132 data
.img_src
= result
.absoluteImageURL();
133 if (!result
.urlElement().isNull()) {
134 data
.anchor_text
= result
.urlElement().innerText();
136 // href is the actual 'href' attribute, which might relative if valid or can
137 // possibly contain garbage otherwise, so not using absoluteLinkURL here.
138 data
.href
= result
.urlElement().getAttribute("href");
141 GURL
url(result
.absoluteLinkURL());
142 bool is_javascript_scheme
= url
.SchemeIs(chrome::kJavaScriptScheme
);
144 // Set AwHitTestData type and extra_data_for_type.
145 if (result
.absoluteLinkURL().isValid() &&
146 !result
.absoluteImageURL().isValid() &&
147 !is_javascript_scheme
) {
148 if (RemovePrefixAndAssignIfMatches(
149 content::kAddressPrefix
,
151 &data
.extra_data_for_type
)) {
152 data
.type
= AwHitTestData::GEO_TYPE
;
153 } else if (RemovePrefixAndAssignIfMatches(
154 content::kPhoneNumberPrefix
,
156 &data
.extra_data_for_type
)) {
157 data
.type
= AwHitTestData::PHONE_TYPE
;
158 } else if (RemovePrefixAndAssignIfMatches(
159 content::kEmailPrefix
,
161 &data
.extra_data_for_type
)) {
162 data
.type
= AwHitTestData::EMAIL_TYPE
;
164 data
.type
= AwHitTestData::SRC_LINK_TYPE
;
165 data
.extra_data_for_type
= url
.spec();
167 } else if (result
.absoluteLinkURL().isValid() &&
168 result
.absoluteImageURL().isValid() &&
169 !is_javascript_scheme
) {
170 data
.type
= AwHitTestData::SRC_IMAGE_LINK_TYPE
;
171 data
.extra_data_for_type
= data
.img_src
.spec();
172 } else if (!result
.absoluteLinkURL().isValid() &&
173 result
.absoluteImageURL().isValid()) {
174 data
.type
= AwHitTestData::IMAGE_TYPE
;
175 data
.extra_data_for_type
= data
.img_src
.spec();
176 } else if (result
.isContentEditable()) {
177 data
.type
= AwHitTestData::EDIT_TEXT_TYPE
;
178 DCHECK(data
.extra_data_for_type
.length() == 0);
181 Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data
));
184 } // namespace android_webview