1 // Copyright 2014 Samsung Electronics. 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 "browser/web_view_browser_message_filter.h"
7 #include "browser/sound_effect.h"
8 #include "common/web_contents_utils.h"
9 #include "common/render_messages_ewk.h"
10 #include "common/hit_test_params.h"
11 #include "content/common/view_messages.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents.h"
18 #include "eweb_view.h"
19 #include "private/ewk_hit_test_private.h"
20 #include "ipc_message_start_ewk.h"
22 using content::BrowserThread;
23 using content::RenderViewHost;
24 using content::WebContents;
25 using namespace web_contents_utils;
28 static const uint32_t kFilteredMessageClasses[] = { EwkMsgStart, ViewMsgStart };
31 class WebViewBrowserMessageFilterPrivate
32 : public content::NotificationObserver {
34 WebViewBrowserMessageFilterPrivate(WebContents* web_contents) {
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
37 content::Source<content::WebContents>(web_contents));
39 web_view_ = WebViewFromWebContents(web_contents);
43 void OnReceivedHitTestData(int render_view,
44 const Hit_Test_Params& params) {
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
49 RenderViewHost* render_view_host =
50 web_view_->web_contents().GetRenderViewHost();
51 DCHECK(render_view_host);
53 if (render_view_host && render_view_host->GetRoutingID() == render_view)
54 web_view_->UpdateHitTestData(params);
57 void OnReceivedHitTestAsyncData(int render_view,
58 const Hit_Test_Params& params,
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 // Only this method is called from renderer process. In particular case
63 // WebContents may be destroyed here, so additional check is needed.
67 RenderViewHost* render_view_host =
68 web_view_->web_contents().GetRenderViewHost();
69 DCHECK(render_view_host);
71 if (render_view_host && render_view_host->GetRoutingID() == render_view) {
72 web_view_->DispatchAsyncHitTestData(params, request_id);
76 void OnPlainTextGetContents(const std::string& content_text, int plain_text_get_callback_id) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
79 web_view_->InvokePlainTextGetCallback(content_text, plain_text_get_callback_id);
82 void OnHandleTapGestureWithContext(bool is_link, bool is_content_editable) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
87 if (is_link && web_view_->GetSettings()->getLinkEffectEnabled())
88 sound_effect::playLinkEffect();
89 web_view_->HandleTapGestureForSelection(is_content_editable);
92 void OnWebAppIconUrlGet(const std::string &icon_url, int callback_id) {
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 web_view_->InvokeWebAppIconUrlGetCallback(icon_url, callback_id);
98 void OnWebAppIconUrlsGet(const std::map<std::string, std::string> &icon_urls, int callback_id) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
101 web_view_->InvokeWebAppIconUrlsGetCallback(icon_urls, callback_id);
104 void OnWebAppCapableGet(bool capable, int callback_id) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
107 web_view_->InvokeWebAppCapableGetCallback(capable, callback_id);
110 void OnDidChangeContentsSize(int width, int height) {
111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
113 web_view_->DidChangeContentsSize(width, height);
116 void OnSelectionTextStyleState(const SelectionStylePrams& params) {
118 web_view_->OnQuerySelectionStyleReply(params);
121 void OnDidChangePageScaleFactor(double scale_factor) {
123 web_view_->DidChangePageScaleFactor(scale_factor);
126 void OnMHTMLContentGet(const std::string& mhtml_content, int callback_id) {
128 web_view_->OnMHTMLContentGet(mhtml_content, callback_id);
131 void OnDidChangeMaxScrollOffset(int maxScrollX, int maxScrollY) {
133 web_view_->GetScrollDetector()->SetMaxScroll(maxScrollX, maxScrollY);
136 void OnDidChangeScrollOffset(int scrollX, int scrollY) {
138 web_view_->GetScrollDetector()->OnChangeScrollOffset(gfx::Vector2d(scrollX, scrollY));
141 void OnFormSubmit(const GURL&url) {
143 web_view_->SmartCallback<EWebViewCallbacks::FormSubmit>().call(url.GetContent().c_str());
146 void OnStartContentIntent(const GURL& content_url, bool is_main_frame) {
147 // [M48_2564] Need to apply the new is_main_frame argument in this function
148 // FIXME: http://web.sec.samsung.net/bugzilla/show_bug.cgi?id=15388
149 DCHECK(!content_url.is_empty());
150 web_view_->ShowContentsDetectedPopup(content_url.spec().c_str());
153 void OnRequestSelectCollectionInformationUpdateACK(
154 int formElementCount, int currentNodeIndex,
155 bool prevState, bool nextState) {
157 web_view_->UpdateFormNavigation(formElementCount,
158 currentNodeIndex, prevState, nextState);
161 void Observe(int type, const content::NotificationSource& source,
162 const content::NotificationDetails& details) override {
163 DCHECK_EQ(content::NOTIFICATION_WEB_CONTENTS_DESTROYED, type);
169 content::NotificationRegistrar registrar_;
173 WebViewBrowserMessageFilter::WebViewBrowserMessageFilter(
174 content::WebContents* web_contents)
175 : BrowserMessageFilter(kFilteredMessageClasses,
176 arraysize(kFilteredMessageClasses)),
177 private_(new WebViewBrowserMessageFilterPrivate(web_contents)) {
180 WebViewBrowserMessageFilter::~WebViewBrowserMessageFilter() {
181 // Due to NotificationRegistrar private_ shall be destroyed in UI thread
182 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, private_);
185 void WebViewBrowserMessageFilter::OverrideThreadForMessage(
186 const IPC::Message& message, BrowserThread::ID* thread) {
187 switch (message.type()) {
188 case EwkViewHostMsg_HitTestAsyncReply::ID:
189 case EwkHostMsg_PlainTextGetContents::ID:
190 case EwkHostMsg_HandleTapGestureWithContext::ID:
191 case EwkHostMsg_WebAppIconUrlGet::ID:
192 case EwkHostMsg_WebAppIconUrlsGet::ID:
193 case EwkHostMsg_WebAppCapableGet::ID:
194 case EwkHostMsg_DidChangeContentsSize::ID:
195 *thread = BrowserThread::UI;
200 bool WebViewBrowserMessageFilter::OnMessageReceived(
201 const IPC::Message& message) {
203 IPC_BEGIN_MESSAGE_MAP(WebViewBrowserMessageFilterPrivate, message)
204 IPC_MESSAGE_FORWARD(EwkViewHostMsg_HitTestReply, private_,
205 WebViewBrowserMessageFilterPrivate::OnReceivedHitTestData)
206 IPC_MESSAGE_FORWARD(EwkViewHostMsg_HitTestAsyncReply, private_,
207 WebViewBrowserMessageFilterPrivate::OnReceivedHitTestAsyncData)
208 IPC_MESSAGE_FORWARD(EwkHostMsg_PlainTextGetContents, private_,
209 WebViewBrowserMessageFilterPrivate::OnPlainTextGetContents)
210 IPC_MESSAGE_FORWARD(EwkHostMsg_HandleTapGestureWithContext, private_,
211 WebViewBrowserMessageFilterPrivate::OnHandleTapGestureWithContext)
212 IPC_MESSAGE_FORWARD(EwkHostMsg_WebAppIconUrlGet, private_,
213 WebViewBrowserMessageFilterPrivate::OnWebAppIconUrlGet)
214 IPC_MESSAGE_FORWARD(EwkHostMsg_WebAppIconUrlsGet, private_,
215 WebViewBrowserMessageFilterPrivate::OnWebAppIconUrlsGet)
216 IPC_MESSAGE_FORWARD(EwkHostMsg_WebAppCapableGet, private_,
217 WebViewBrowserMessageFilterPrivate::OnWebAppCapableGet)
218 IPC_MESSAGE_FORWARD(EwkHostMsg_DidChangeContentsSize, private_,
219 WebViewBrowserMessageFilterPrivate::OnDidChangeContentsSize)
220 IPC_MESSAGE_FORWARD(EwkViewMsg_SelectionTextStyleState, private_,
221 WebViewBrowserMessageFilterPrivate::OnSelectionTextStyleState)
222 IPC_MESSAGE_FORWARD(EwkHostMsg_ReadMHTMLData, private_,
223 WebViewBrowserMessageFilterPrivate::OnMHTMLContentGet)
224 IPC_MESSAGE_FORWARD(EwkHostMsg_DidChangePageScaleFactor, private_,
225 WebViewBrowserMessageFilterPrivate::OnDidChangePageScaleFactor)
226 IPC_MESSAGE_FORWARD(EwkHostMsg_DidChangeMaxScrollOffset, private_,
227 WebViewBrowserMessageFilterPrivate::OnDidChangeMaxScrollOffset)
228 IPC_MESSAGE_FORWARD(EwkHostMsg_DidChangeScrollOffset, private_,
229 WebViewBrowserMessageFilterPrivate::OnDidChangeScrollOffset)
230 IPC_MESSAGE_FORWARD(EwkHostMsg_FormSubmit, private_,
231 WebViewBrowserMessageFilterPrivate::OnFormSubmit)
232 IPC_MESSAGE_FORWARD(ViewHostMsg_StartContentIntent, private_,
233 WebViewBrowserMessageFilterPrivate::OnStartContentIntent)
234 IPC_MESSAGE_FORWARD(EwkHostMsg_RequestSelectCollectionInformationUpdateACK,
235 private_, WebViewBrowserMessageFilterPrivate::OnRequestSelectCollectionInformationUpdateACK)
237 IPC_MESSAGE_UNHANDLED(handled = false)
238 IPC_END_MESSAGE_MAP()