summaryrefslogtreecommitdiffstats
path: root/content/browser/browser_plugin/browser_plugin_embedder_helper.cc
blob: 2bc9cdfefb1dd80ba06f0a6a13473db8a313f6a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/browser_plugin/browser_plugin_embedder_helper.h"

#include "content/browser/browser_plugin/browser_plugin_embedder.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/common/browser_plugin_messages.h"
#include "content/common/view_messages.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "ui/gfx/size.h"

namespace content {

BrowserPluginEmbedderHelper::BrowserPluginEmbedderHelper(
    BrowserPluginEmbedder* embedder,
    RenderViewHost* render_view_host)
    : RenderViewHostObserver(render_view_host),
      embedder_(embedder) {
}

BrowserPluginEmbedderHelper::~BrowserPluginEmbedderHelper() {
}

bool BrowserPluginEmbedderHelper::Send(IPC::Message* message) {
  return RenderViewHostObserver::Send(message);
}

bool BrowserPluginEmbedderHelper::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(BrowserPluginEmbedderHelper, message)
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateGuest,
                        OnNavigateGuest);
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest)
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_UpdateRect_ACK, OnUpdateRectACK);
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_SetFocus, OnSetFocus);
    IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_HandleInputEvent,
        OnHandleInputEvent(*static_cast<const IPC::SyncMessage*>(&message),
                           &handled))
    IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_PluginDestroyed,
                        OnPluginDestroyed);
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void BrowserPluginEmbedderHelper::OnResizeGuest(
    int instance_id,
    const BrowserPluginHostMsg_ResizeGuest_Params& params) {
  TransportDIB* damage_buffer = NULL;
#if defined(OS_WIN)
  // On Windows we need to duplicate the handle from the remote process.
  HANDLE section;
  DuplicateHandle(render_view_host()->GetProcess()->GetHandle(),
                  params.damage_buffer_id.handle,
                  GetCurrentProcess(),
                  &section,
                  STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ | FILE_MAP_WRITE,
                  FALSE, 0);
  damage_buffer = TransportDIB::Map(section);
#elif defined(OS_MACOSX)
  // On OSX, the browser allocates all DIBs and keeps a file descriptor around
  // for each.
  damage_buffer = render_view_host()->GetProcess()->
      GetTransportDIB(params.damage_buffer_id);
#elif defined(OS_ANDROID)
  damage_buffer = TransportDIB::Map(params.damage_buffer_id);
#elif defined(OS_POSIX)
  damage_buffer = TransportDIB::Map(params.damage_buffer_id.shmkey);
#endif  // defined(OS_POSIX)
  DCHECK(damage_buffer);
  // TODO(fsamuel): Schedule this later so that we don't stall the embedder for
  // too long.
  embedder_->ResizeGuest(instance_id,
                         damage_buffer,
#if defined(OS_WIN)
                         params.damage_buffer_size,
#endif
                         params.width,
                         params.height,
                         params.resize_pending,
                         params.scale_factor);
}

void BrowserPluginEmbedderHelper::OnHandleInputEvent(
    const IPC::SyncMessage& message,
    bool* handled) {
  *handled = true;
  PickleIterator iter(message);

  // TODO(fsamuel): This appears to be a monotonically increasing value.
  int instance_id = -1;
  const char* guest_rect_data = NULL;
  int guest_rect_data_length = -1;
  const char* input_event_data = NULL;
  int input_event_data_length = -1;
  if (!iter.SkipBytes(4) ||
      !message.ReadInt(&iter, &instance_id) ||
      !message.ReadData(&iter, &guest_rect_data, &guest_rect_data_length) ||
      !message.ReadData(&iter, &input_event_data, &input_event_data_length)) {
    *handled = false;
    return;
  }
  const gfx::Rect* guest_rect =
      reinterpret_cast<const gfx::Rect*>(guest_rect_data);
  const WebKit::WebInputEvent* input_event =
      reinterpret_cast<const WebKit::WebInputEvent*>(input_event_data);
  RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
      render_view_host());

  // Convert the window coordinates into screen coordinates.
  gfx::Rect guest_screen_rect(*guest_rect);
  if (rvh->GetView())
    guest_screen_rect.Offset(rvh->GetView()->GetViewBounds().origin());

  IPC::Message* reply_message =
      IPC::SyncMessage::GenerateReply(&message);
  embedder_->HandleInputEvent(instance_id,
                              rvh,
                              guest_screen_rect,
                              *input_event,
                              reply_message);
}

void BrowserPluginEmbedderHelper::OnNavigateGuest(int instance_id,
                                                  int64 frame_id,
                                                  const std::string& src,
                                                  const gfx::Size& size) {
  embedder_->NavigateGuest(render_view_host(), instance_id, frame_id, src,
                           size);
}

void BrowserPluginEmbedderHelper::OnUpdateRectACK(int instance_id,
                                                  int message_id,
                                                  const gfx::Size& size) {
  embedder_->UpdateRectACK(instance_id, message_id, size);
}

void BrowserPluginEmbedderHelper::OnSetFocus(int instance_id, bool focused) {
  embedder_->SetFocus(instance_id, focused);
}

void BrowserPluginEmbedderHelper::OnPluginDestroyed(int instance_id) {
  embedder_->PluginDestroyed(instance_id);
}

}  // namespace content