Home | History | Annotate | Download | only in renderer_host
      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/browser/renderer_host/aw_render_view_host_ext.h"
      6 
      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"
     20 
     21 namespace android_webview {
     22 
     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_);
     30 }
     31 
     32 AwRenderViewHostExt::~AwRenderViewHostExt() {}
     33 
     34 void AwRenderViewHostExt::DocumentHasImages(DocumentHasImagesResult result) {
     35   DCHECK(CalledOnValidThread());
     36   if (!web_contents()->GetRenderViewHost()) {
     37     result.Run(false);
     38     return;
     39   }
     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));
     45 }
     46 
     47 void AwRenderViewHostExt::ClearCache() {
     48   DCHECK(CalledOnValidThread());
     49   Send(new AwViewMsg_ClearCache);
     50 }
     51 
     52 bool AwRenderViewHostExt::HasNewHitTestData() const {
     53   return has_new_hit_test_data_;
     54 }
     55 
     56 void AwRenderViewHostExt::MarkHitTestDataRead() {
     57   has_new_hit_test_data_ = false;
     58 }
     59 
     60 void AwRenderViewHostExt::RequestNewHitTestDataAt(int view_x, int view_y) {
     61   DCHECK(CalledOnValidThread());
     62   Send(new AwViewMsg_DoHitTest(web_contents()->GetRoutingID(),
     63                                view_x,
     64                                view_y));
     65 }
     66 
     67 const AwHitTestData& AwRenderViewHostExt::GetLastHitTestData() const {
     68   DCHECK(CalledOnValidThread());
     69   return last_hit_test_data_;
     70 }
     71 
     72 void AwRenderViewHostExt::SetTextZoomFactor(float factor) {
     73   DCHECK(CalledOnValidThread());
     74   Send(new AwViewMsg_SetTextZoomFactor(web_contents()->GetRoutingID(), factor));
     75 }
     76 
     77 void AwRenderViewHostExt::ResetScrollAndScaleState() {
     78   DCHECK(CalledOnValidThread());
     79   Send(new AwViewMsg_ResetScrollAndScaleState(web_contents()->GetRoutingID()));
     80 }
     81 
     82 void AwRenderViewHostExt::SetInitialPageScale(double page_scale_factor) {
     83   DCHECK(CalledOnValidThread());
     84   Send(new AwViewMsg_SetInitialPageScale(web_contents()->GetRoutingID(),
     85                                          page_scale_factor));
     86 }
     87 
     88 void AwRenderViewHostExt::SetBackgroundColor(SkColor c) {
     89   if (background_color_ == c)
     90     return;
     91   background_color_ = c;
     92   if (web_contents()->GetRenderViewHost()) {
     93     Send(new AwViewMsg_SetBackgroundColor(web_contents()->GetRoutingID(),
     94                                           background_color_));
     95   }
     96 }
     97 
     98 void AwRenderViewHostExt::SetJsOnlineProperty(bool network_up) {
     99   Send(new AwViewMsg_SetJsOnlineProperty(network_up));
    100 }
    101 
    102 void AwRenderViewHostExt::RenderViewCreated(
    103     content::RenderViewHost* render_view_host) {
    104   Send(new AwViewMsg_SetBackgroundColor(web_contents()->GetRoutingID(),
    105                                         background_color_));
    106 }
    107 
    108 void AwRenderViewHostExt::RenderProcessGone(base::TerminationStatus status) {
    109   DCHECK(CalledOnValidThread());
    110   for (std::map<int, DocumentHasImagesResult>::iterator pending_req =
    111            pending_document_has_images_requests_.begin();
    112        pending_req != pending_document_has_images_requests_.end();
    113       ++pending_req) {
    114     pending_req->second.Run(false);
    115   }
    116 }
    117 
    118 void AwRenderViewHostExt::DidNavigateAnyFrame(
    119     const content::LoadCommittedDetails& details,
    120     const content::FrameNavigateParams& params) {
    121   DCHECK(CalledOnValidThread());
    122 
    123   AwBrowserContext::FromWebContents(web_contents())
    124       ->AddVisitedURLs(params.redirects);
    125 }
    126 
    127 bool AwRenderViewHostExt::OnMessageReceived(const IPC::Message& message) {
    128   bool handled = true;
    129   IPC_BEGIN_MESSAGE_MAP(AwRenderViewHostExt, message)
    130     IPC_MESSAGE_HANDLER(AwViewHostMsg_DocumentHasImagesResponse,
    131                         OnDocumentHasImagesResponse)
    132     IPC_MESSAGE_HANDLER(AwViewHostMsg_UpdateHitTestData,
    133                         OnUpdateHitTestData)
    134     IPC_MESSAGE_HANDLER(AwViewHostMsg_PageScaleFactorChanged,
    135                         OnPageScaleFactorChanged)
    136     IPC_MESSAGE_HANDLER(AwViewHostMsg_OnContentsSizeChanged,
    137                         OnContentsSizeChanged)
    138     IPC_MESSAGE_UNHANDLED(handled = false)
    139   IPC_END_MESSAGE_MAP()
    140 
    141   return handled ? true : WebContentsObserver::OnMessageReceived(message);
    142 }
    143 
    144 void AwRenderViewHostExt::OnDocumentHasImagesResponse(int msg_id,
    145                                                       bool has_images) {
    146   DCHECK(CalledOnValidThread());
    147   std::map<int, DocumentHasImagesResult>::iterator pending_req =
    148       pending_document_has_images_requests_.find(msg_id);
    149   if (pending_req == pending_document_has_images_requests_.end()) {
    150     DLOG(WARNING) << "unexpected DocumentHasImages Response: " << msg_id;
    151   } else {
    152     pending_req->second.Run(has_images);
    153     pending_document_has_images_requests_.erase(pending_req);
    154   }
    155 }
    156 
    157 void AwRenderViewHostExt::OnUpdateHitTestData(
    158     const AwHitTestData& hit_test_data) {
    159   DCHECK(CalledOnValidThread());
    160   last_hit_test_data_ = hit_test_data;
    161   has_new_hit_test_data_ = true;
    162 }
    163 
    164 void AwRenderViewHostExt::OnPageScaleFactorChanged(float page_scale_factor) {
    165   client_->OnWebLayoutPageScaleFactorChanged(page_scale_factor);
    166 }
    167 
    168 void AwRenderViewHostExt::OnContentsSizeChanged(
    169     const gfx::Size& contents_size) {
    170   client_->OnWebLayoutContentsSizeChanged(contents_size);
    171 }
    172 
    173 }  // namespace android_webview
    174