Home | History | Annotate | Download | only in renderer
      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.
      4 
      5 #include "android_webview/renderer/aw_render_view_ext.h"
      6 
      7 #include <string>
      8 
      9 #include "android_webview/common/aw_hit_test_data.h"
     10 #include "android_webview/common/render_view_messages.h"
     11 #include "base/bind.h"
     12 #include "base/strings/string_piece.h"
     13 #include "base/strings/utf_string_conversions.h"
     14 #include "base/time/time.h"
     15 #include "content/public/common/url_constants.h"
     16 #include "content/public/renderer/android_content_detection_prefixes.h"
     17 #include "content/public/renderer/document_state.h"
     18 #include "content/public/renderer/render_view.h"
     19 #include "skia/ext/refptr.h"
     20 #include "third_party/WebKit/public/platform/WebSize.h"
     21 #include "third_party/WebKit/public/platform/WebURL.h"
     22 #include "third_party/WebKit/public/platform/WebVector.h"
     23 #include "third_party/WebKit/public/web/WebDataSource.h"
     24 #include "third_party/WebKit/public/web/WebDocument.h"
     25 #include "third_party/WebKit/public/web/WebElement.h"
     26 #include "third_party/WebKit/public/web/WebFrame.h"
     27 #include "third_party/WebKit/public/web/WebHitTestResult.h"
     28 #include "third_party/WebKit/public/web/WebImageCache.h"
     29 #include "third_party/WebKit/public/web/WebNode.h"
     30 #include "third_party/WebKit/public/web/WebNodeList.h"
     31 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
     32 #include "third_party/WebKit/public/web/WebView.h"
     33 #include "url/url_canon.h"
     34 #include "url/url_util.h"
     35 
     36 namespace android_webview {
     37 
     38 namespace {
     39 
     40 bool AllowMixedContent(const WebKit::WebURL& url) {
     41   // We treat non-standard schemes as "secure" in the WebView to allow them to
     42   // be used for request interception.
     43   // TODO(benm): Tighten this restriction by requiring embedders to register
     44   // their custom schemes? See b/9420953.
     45   GURL gurl(url);
     46   return !gurl.IsStandard();
     47 }
     48 
     49 GURL GetAbsoluteUrl(const WebKit::WebNode& node, const string16& url_fragment) {
     50   return GURL(node.document().completeURL(url_fragment));
     51 }
     52 
     53 string16 GetHref(const WebKit::WebElement& element) {
     54   // Get the actual 'href' attribute, which might relative if valid or can
     55   // possibly contain garbage otherwise, so not using absoluteLinkURL here.
     56   return element.getAttribute("href");
     57 }
     58 
     59 GURL GetAbsoluteSrcUrl(const WebKit::WebElement& element) {
     60   if (element.isNull())
     61     return GURL();
     62   return GetAbsoluteUrl(element, element.getAttribute("src"));
     63 }
     64 
     65 WebKit::WebNode GetImgChild(const WebKit::WebNode& node) {
     66   // This implementation is incomplete (for example if is an area tag) but
     67   // matches the original WebViewClassic implementation.
     68 
     69   WebKit::WebNodeList list = node.getElementsByTagName("img");
     70   if (list.length() > 0)
     71     return list.item(0);
     72   return WebKit::WebNode();
     73 }
     74 
     75 bool RemovePrefixAndAssignIfMatches(const base::StringPiece& prefix,
     76                                     const GURL& url,
     77                                     std::string* dest) {
     78   const base::StringPiece spec(url.possibly_invalid_spec());
     79 
     80   if (spec.starts_with(prefix)) {
     81     url_canon::RawCanonOutputW<1024> output;
     82     url_util::DecodeURLEscapeSequences(spec.data() + prefix.length(),
     83         spec.length() - prefix.length(), &output);
     84     std::string decoded_url = UTF16ToUTF8(
     85         string16(output.data(), output.length()));
     86     dest->assign(decoded_url.begin(), decoded_url.end());
     87     return true;
     88   }
     89   return false;
     90 }
     91 
     92 void DistinguishAndAssignSrcLinkType(const GURL& url, AwHitTestData* data) {
     93   if (RemovePrefixAndAssignIfMatches(
     94       content::kAddressPrefix,
     95       url,
     96       &data->extra_data_for_type)) {
     97     data->type = AwHitTestData::GEO_TYPE;
     98   } else if (RemovePrefixAndAssignIfMatches(
     99       content::kPhoneNumberPrefix,
    100       url,
    101       &data->extra_data_for_type)) {
    102     data->type = AwHitTestData::PHONE_TYPE;
    103   } else if (RemovePrefixAndAssignIfMatches(
    104       content::kEmailPrefix,
    105       url,
    106       &data->extra_data_for_type)) {
    107     data->type = AwHitTestData::EMAIL_TYPE;
    108   } else {
    109     data->type = AwHitTestData::SRC_LINK_TYPE;
    110     data->extra_data_for_type = url.possibly_invalid_spec();
    111   }
    112 }
    113 
    114 void PopulateHitTestData(const GURL& absolute_link_url,
    115                          const GURL& absolute_image_url,
    116                          bool is_editable,
    117                          AwHitTestData* data) {
    118   // Note: Using GURL::is_empty instead of GURL:is_valid due to the
    119   // WebViewClassic allowing any kind of protocol which GURL::is_valid
    120   // disallows. Similar reasons for using GURL::possibly_invalid_spec instead of
    121   // GURL::spec.
    122   if (!absolute_image_url.is_empty())
    123     data->img_src = absolute_image_url;
    124 
    125   const bool is_javascript_scheme =
    126       absolute_link_url.SchemeIs(chrome::kJavaScriptScheme);
    127   const bool has_link_url = !absolute_link_url.is_empty();
    128   const bool has_image_url = !absolute_image_url.is_empty();
    129 
    130   if (has_link_url && !has_image_url && !is_javascript_scheme) {
    131     DistinguishAndAssignSrcLinkType(absolute_link_url, data);
    132   } else if (has_link_url && has_image_url && !is_javascript_scheme) {
    133     data->type = AwHitTestData::SRC_IMAGE_LINK_TYPE;
    134     data->extra_data_for_type = data->img_src.possibly_invalid_spec();
    135   } else if (!has_link_url && has_image_url) {
    136     data->type = AwHitTestData::IMAGE_TYPE;
    137     data->extra_data_for_type = data->img_src.possibly_invalid_spec();
    138   } else if (is_editable) {
    139     data->type = AwHitTestData::EDIT_TEXT_TYPE;
    140     DCHECK(data->extra_data_for_type.length() == 0);
    141   }
    142 }
    143 
    144 }  // namespace
    145 
    146 AwRenderViewExt::AwRenderViewExt(content::RenderView* render_view)
    147     : content::RenderViewObserver(render_view), page_scale_factor_(0.0f) {
    148   render_view->GetWebView()->setPermissionClient(this);
    149 }
    150 
    151 AwRenderViewExt::~AwRenderViewExt() {
    152 }
    153 
    154 // static
    155 void AwRenderViewExt::RenderViewCreated(content::RenderView* render_view) {
    156   new AwRenderViewExt(render_view);  // |render_view| takes ownership.
    157 }
    158 
    159 bool AwRenderViewExt::OnMessageReceived(const IPC::Message& message) {
    160   bool handled = true;
    161   IPC_BEGIN_MESSAGE_MAP(AwRenderViewExt, message)
    162     IPC_MESSAGE_HANDLER(AwViewMsg_DocumentHasImages, OnDocumentHasImagesRequest)
    163     IPC_MESSAGE_HANDLER(AwViewMsg_DoHitTest, OnDoHitTest)
    164     IPC_MESSAGE_HANDLER(AwViewMsg_SetTextZoomLevel, OnSetTextZoomLevel)
    165     IPC_MESSAGE_HANDLER(AwViewMsg_ResetScrollAndScaleState,
    166                         OnResetScrollAndScaleState)
    167     IPC_MESSAGE_HANDLER(AwViewMsg_SetInitialPageScale, OnSetInitialPageScale)
    168     IPC_MESSAGE_HANDLER(AwViewMsg_SetFixedLayoutSize,
    169                         OnSetFixedLayoutSize)
    170     IPC_MESSAGE_HANDLER(AwViewMsg_SetBackgroundColor, OnSetBackgroundColor)
    171     IPC_MESSAGE_UNHANDLED(handled = false)
    172   IPC_END_MESSAGE_MAP()
    173   return handled;
    174 }
    175 
    176 void AwRenderViewExt::OnDocumentHasImagesRequest(int id) {
    177   bool hasImages = false;
    178   if (render_view()) {
    179     WebKit::WebView* webview = render_view()->GetWebView();
    180     if (webview) {
    181       WebKit::WebVector<WebKit::WebElement> images;
    182       webview->mainFrame()->document().images(images);
    183       hasImages = !images.isEmpty();
    184     }
    185   }
    186   Send(new AwViewHostMsg_DocumentHasImagesResponse(routing_id(), id,
    187                                                    hasImages));
    188 }
    189 
    190 bool AwRenderViewExt::allowDisplayingInsecureContent(
    191       WebKit::WebFrame* frame,
    192       bool enabled_per_settings,
    193       const WebKit::WebSecurityOrigin& origin,
    194       const WebKit::WebURL& url) {
    195   return enabled_per_settings ? true : AllowMixedContent(url);
    196 }
    197 
    198 bool AwRenderViewExt::allowRunningInsecureContent(
    199       WebKit::WebFrame* frame,
    200       bool enabled_per_settings,
    201       const WebKit::WebSecurityOrigin& origin,
    202       const WebKit::WebURL& url) {
    203   return enabled_per_settings ? true : AllowMixedContent(url);
    204 }
    205 
    206 void AwRenderViewExt::DidCommitProvisionalLoad(WebKit::WebFrame* frame,
    207                                                bool is_new_navigation) {
    208   content::DocumentState* document_state =
    209       content::DocumentState::FromDataSource(frame->dataSource());
    210   if (document_state->can_load_local_resources()) {
    211     WebKit::WebSecurityOrigin origin = frame->document().securityOrigin();
    212     origin.grantLoadLocalResources();
    213   }
    214 }
    215 
    216 void AwRenderViewExt::DidCommitCompositorFrame() {
    217   UpdatePageScaleFactor();
    218 }
    219 
    220 void AwRenderViewExt::DidUpdateLayout() {
    221   if (check_contents_size_timer_.IsRunning())
    222     return;
    223 
    224   check_contents_size_timer_.Start(FROM_HERE,
    225                                    base::TimeDelta::FromMilliseconds(0), this,
    226                                    &AwRenderViewExt::CheckContentsSize);
    227 }
    228 
    229 void AwRenderViewExt::UpdatePageScaleFactor() {
    230   if (page_scale_factor_ != render_view()->GetWebView()->pageScaleFactor()) {
    231     page_scale_factor_ = render_view()->GetWebView()->pageScaleFactor();
    232     Send(new AwViewHostMsg_PageScaleFactorChanged(routing_id(),
    233                                                   page_scale_factor_));
    234   }
    235 }
    236 
    237 void AwRenderViewExt::CheckContentsSize() {
    238   if (!render_view()->GetWebView())
    239     return;
    240 
    241   gfx::Size contents_size;
    242 
    243   WebKit::WebFrame* main_frame = render_view()->GetWebView()->mainFrame();
    244   if (main_frame)
    245     contents_size = main_frame->contentsSize();
    246 
    247   // Fall back to contentsPreferredMinimumSize if the mainFrame is reporting a
    248   // 0x0 size (this happens during initial load).
    249   if (contents_size.IsEmpty()) {
    250     contents_size = render_view()->GetWebView()->contentsPreferredMinimumSize();
    251   }
    252 
    253   if (contents_size == last_sent_contents_size_)
    254     return;
    255 
    256   last_sent_contents_size_ = contents_size;
    257   Send(new AwViewHostMsg_OnContentsSizeChanged(routing_id(), contents_size));
    258 }
    259 
    260 void AwRenderViewExt::Navigate(const GURL& url) {
    261   // Navigate is called only on NEW navigations, so WebImageCache won't be freed
    262   // when the user just clicks on links, but only when a navigation is started,
    263   // for instance via loadUrl. A better approach would be clearing the cache on
    264   // cross-site boundaries, however this would require too many changes both on
    265   // the browser side (in RenderViewHostManger), to the IPCmessages and to the
    266   // RenderViewObserver. Thus, clearing decoding image cache on Navigate, seems
    267   // a more acceptable compromise.
    268   WebKit::WebImageCache::clear();
    269 }
    270 
    271 void AwRenderViewExt::FocusedNodeChanged(const WebKit::WebNode& node) {
    272   if (node.isNull() || !node.isElementNode() || !render_view())
    273     return;
    274 
    275   // Note: element is not const due to innerText() is not const.
    276   WebKit::WebElement element = node.toConst<WebKit::WebElement>();
    277   AwHitTestData data;
    278 
    279   data.href = GetHref(element);
    280   data.anchor_text = element.innerText();
    281 
    282   GURL absolute_link_url;
    283   if (node.isLink())
    284     absolute_link_url = GetAbsoluteUrl(node, data.href);
    285 
    286   GURL absolute_image_url;
    287   const WebKit::WebNode child_img = GetImgChild(node);
    288   if (!child_img.isNull() && child_img.isElementNode()) {
    289     absolute_image_url =
    290         GetAbsoluteSrcUrl(child_img.toConst<WebKit::WebElement>());
    291   }
    292 
    293   PopulateHitTestData(absolute_link_url,
    294                       absolute_image_url,
    295                       render_view()->IsEditableNode(node),
    296                       &data);
    297   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
    298 }
    299 
    300 void AwRenderViewExt::OnDoHitTest(int view_x, int view_y) {
    301   if (!render_view() || !render_view()->GetWebView())
    302     return;
    303 
    304   const WebKit::WebHitTestResult result =
    305       render_view()->GetWebView()->hitTestResultAt(
    306           WebKit::WebPoint(view_x, view_y));
    307   AwHitTestData data;
    308 
    309   if (!result.urlElement().isNull()) {
    310     data.anchor_text = result.urlElement().innerText();
    311     data.href = GetHref(result.urlElement());
    312   }
    313 
    314   PopulateHitTestData(result.absoluteLinkURL(),
    315                       result.absoluteImageURL(),
    316                       result.isContentEditable(),
    317                       &data);
    318   Send(new AwViewHostMsg_UpdateHitTestData(routing_id(), data));
    319 }
    320 
    321 void AwRenderViewExt::OnSetTextZoomLevel(double zoom_level) {
    322   if (!render_view() || !render_view()->GetWebView())
    323     return;
    324   // Hide selection and autofill popups.
    325   render_view()->GetWebView()->hidePopups();
    326   render_view()->GetWebView()->setZoomLevel(true, zoom_level);
    327 }
    328 
    329 void AwRenderViewExt::OnResetScrollAndScaleState() {
    330   if (!render_view() || !render_view()->GetWebView())
    331     return;
    332   render_view()->GetWebView()->resetScrollAndScaleState();
    333 }
    334 
    335 void AwRenderViewExt::OnSetInitialPageScale(double page_scale_factor) {
    336   if (!render_view() || !render_view()->GetWebView())
    337     return;
    338   render_view()->GetWebView()->setInitialPageScaleOverride(
    339       page_scale_factor);
    340 }
    341 
    342 void AwRenderViewExt::OnSetFixedLayoutSize(const gfx::Size& size) {
    343   if (!render_view() || !render_view()->GetWebView())
    344     return;
    345   DCHECK(render_view()->GetWebView()->isFixedLayoutModeEnabled());
    346   render_view()->GetWebView()->setFixedLayoutSize(size);
    347 }
    348 
    349 void AwRenderViewExt::OnSetBackgroundColor(SkColor c) {
    350   if (!render_view() || !render_view()->GetWebView())
    351     return;
    352   render_view()->GetWebView()->setBaseBackgroundColor(c);
    353 }
    354 
    355 }  // namespace android_webview
    356