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#ifndef ANDROID_WEBVIEW_BROWSER_RENDER_HOST_RENDER_VIEW_HOST_EXT_H_
6#define ANDROID_WEBVIEW_BROWSER_RENDER_HOST_RENDER_VIEW_HOST_EXT_H_
7
8#include "content/public/browser/web_contents_observer.h"
9
10#include "android_webview/common/aw_hit_test_data.h"
11#include "base/callback_forward.h"
12#include "base/threading/non_thread_safe.h"
13#include "third_party/skia/include/core/SkColor.h"
14#include "ui/gfx/size.h"
15
16class GURL;
17
18namespace content {
19struct FrameNavigateParams;
20struct LoadCommittedDetails;
21}  // namespace content
22
23namespace android_webview {
24
25class AwRenderViewHostExtClient {
26 public:
27  // Called when the RenderView page scale changes.
28  virtual void OnWebLayoutPageScaleFactorChanged(float page_scale_factor) = 0;
29  virtual void OnWebLayoutContentsSizeChanged(
30      const gfx::Size& contents_size) = 0;
31
32 protected:
33  virtual ~AwRenderViewHostExtClient() {}
34};
35
36// Provides RenderViewHost wrapper functionality for sending WebView-specific
37// IPC messages to the renderer and from there to WebKit.
38class AwRenderViewHostExt : public content::WebContentsObserver,
39                            public base::NonThreadSafe {
40 public:
41
42  // To send receive messages to a RenderView we take the WebContents instance,
43  // as it internally handles RenderViewHost instances changing underneath us.
44  AwRenderViewHostExt(
45      AwRenderViewHostExtClient* client, content::WebContents* contents);
46  virtual ~AwRenderViewHostExt();
47
48  // |result| will be invoked with the outcome of the request.
49  typedef base::Callback<void(bool)> DocumentHasImagesResult;
50  void DocumentHasImages(DocumentHasImagesResult result);
51
52  // Clear all WebCore memory cache (not only for this view).
53  void ClearCache();
54
55  // Do a hit test at the view port coordinates and asynchronously update
56  // |last_hit_test_data_|. |view_x| and |view_y| are in density independent
57  // pixels used by WebKit::WebView.
58  void RequestNewHitTestDataAt(int view_x, int view_y);
59
60  // Optimization to avoid unnecessary Java object creation on hit test.
61  bool HasNewHitTestData() const;
62  void MarkHitTestDataRead();
63
64  // Return |last_hit_test_data_|. Note that this is unavoidably racy;
65  // the corresponding public WebView API is as well.
66  const AwHitTestData& GetLastHitTestData() const;
67
68  // Sets the zoom level for text only. Used in layout modes other than
69  // Text Autosizing.
70  void SetTextZoomLevel(double level);
71
72  void SetFixedLayoutSize(const gfx::Size& size);
73
74  void ResetScrollAndScaleState();
75
76  // Sets the initial page scale. This overrides initial scale set by
77  // the meta viewport tag.
78  void SetInitialPageScale(double page_scale_factor);
79  void SetBackgroundColor(SkColor c);
80  void SetJsOnlineProperty(bool network_up);
81
82 private:
83  // content::WebContentsObserver implementation.
84  virtual void RenderViewCreated(content::RenderViewHost* view_host) OVERRIDE;
85  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
86  virtual void DidNavigateAnyFrame(
87      const content::LoadCommittedDetails& details,
88      const content::FrameNavigateParams& params) OVERRIDE;
89  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
90
91  void OnDocumentHasImagesResponse(int msg_id, bool has_images);
92  void OnUpdateHitTestData(const AwHitTestData& hit_test_data);
93  void OnPageScaleFactorChanged(float page_scale_factor);
94  void OnContentsSizeChanged(const gfx::Size& contents_size);
95
96  bool IsRenderViewReady() const;
97
98  AwRenderViewHostExtClient* client_;
99
100  SkColor background_color_;
101
102  std::map<int, DocumentHasImagesResult> pending_document_has_images_requests_;
103
104  // Master copy of hit test data on the browser side. This is updated
105  // as a result of DoHitTest called explicitly or when the FocusedNodeChanged
106  // is called in AwRenderViewExt.
107  AwHitTestData last_hit_test_data_;
108
109  bool has_new_hit_test_data_;
110
111  DISALLOW_COPY_AND_ASSIGN(AwRenderViewHostExt);
112};
113
114}  // namespace android_webview
115
116#endif  // ANDROID_WEBVIEW_BROWSER_RENDER_HOST_RENDER_VIEW_HOST_EXT_H_
117