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