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
|
// 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.h"
#include "content/browser/browser_plugin/browser_plugin_guest.h"
#include "content/browser/browser_plugin/browser_plugin_guest_manager.h"
#include "content/browser/browser_plugin/browser_plugin_host_factory.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/browser_plugin/browser_plugin_constants.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/gpu/gpu_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "net/base/escape.h"
namespace content {
// static
BrowserPluginHostFactory* BrowserPluginEmbedder::factory_ = NULL;
BrowserPluginEmbedder::BrowserPluginEmbedder(WebContentsImpl* web_contents)
: WebContentsObserver(web_contents),
next_get_render_view_request_id_(0) {
}
BrowserPluginEmbedder::~BrowserPluginEmbedder() {
CleanUp();
}
// static
BrowserPluginEmbedder* BrowserPluginEmbedder::Create(
WebContentsImpl* web_contents) {
if (factory_)
return factory_->CreateBrowserPluginEmbedder(web_contents);
return new BrowserPluginEmbedder(web_contents);
}
void BrowserPluginEmbedder::GetRenderViewHostAtPosition(
int x, int y, const WebContents::GetRenderViewHostCallback& callback) {
// Store the callback so we can call it later when we have the response.
pending_get_render_view_callbacks_.insert(
std::make_pair(next_get_render_view_request_id_, callback));
Send(new BrowserPluginMsg_PluginAtPositionRequest(
routing_id(),
next_get_render_view_request_id_,
gfx::Point(x, y)));
++next_get_render_view_request_id_;
}
void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) {
CleanUp();
}
bool BrowserPluginEmbedder::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(BrowserPluginEmbedder, message)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_AllocateInstanceID,
OnAllocateInstanceID)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_Attach, OnAttach)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_CreateGuest, OnCreateGuest)
IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_PluginAtPositionResponse,
OnPluginAtPositionResponse)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void BrowserPluginEmbedder::CleanUp() {
// CleanUp gets called when BrowserPluginEmbedder's WebContents goes away
// or the associated RenderViewHost is destroyed or swapped out. Therefore we
// don't need to care about the pending callbacks anymore.
pending_get_render_view_callbacks_.clear();
}
BrowserPluginGuestManager*
BrowserPluginEmbedder::GetBrowserPluginGuestManager() {
BrowserPluginGuestManager* guest_manager = static_cast<WebContentsImpl*>(
web_contents())->GetBrowserPluginGuestManager();
if (!guest_manager) {
guest_manager = BrowserPluginGuestManager::Create();
web_contents()->GetBrowserContext()->SetUserData(
browser_plugin::kBrowserPluginGuestManagerKeyName, guest_manager);
}
return guest_manager;
}
void BrowserPluginEmbedder::OnAllocateInstanceID(int request_id) {
int instance_id = GetBrowserPluginGuestManager()->get_next_instance_id();
Send(new BrowserPluginMsg_AllocateInstanceID_ACK(
routing_id(), request_id, instance_id));
}
void BrowserPluginEmbedder::OnAttach(
int instance_id,
const BrowserPluginHostMsg_CreateGuest_Params& params) {
BrowserPluginGuest* guest =
GetBrowserPluginGuestManager()->GetGuestByInstanceID(
instance_id, web_contents()->GetRenderProcessHost()->GetID());
if (guest)
guest->Attach(static_cast<WebContentsImpl*>(web_contents()), params);
}
void BrowserPluginEmbedder::OnCreateGuest(
int instance_id,
const BrowserPluginHostMsg_CreateGuest_Params& params) {
BrowserPluginGuest* guest = GetBrowserPluginGuestManager()->CreateGuest(
web_contents()->GetSiteInstance(), instance_id, params);
if (guest)
guest->Initialize(static_cast<WebContentsImpl*>(web_contents()), params);
}
void BrowserPluginEmbedder::OnPluginAtPositionResponse(
int instance_id, int request_id, const gfx::Point& position) {
const std::map<int, WebContents::GetRenderViewHostCallback>::iterator
callback_iter = pending_get_render_view_callbacks_.find(request_id);
if (callback_iter == pending_get_render_view_callbacks_.end())
return;
RenderViewHost* render_view_host;
BrowserPluginGuest* guest =
GetBrowserPluginGuestManager()->GetGuestByInstanceID(
instance_id, web_contents()->GetRenderProcessHost()->GetID());
if (guest)
render_view_host = guest->GetWebContents()->GetRenderViewHost();
else // No plugin, use embedder's RenderViewHost.
render_view_host = web_contents()->GetRenderViewHost();
callback_iter->second.Run(render_view_host, position.x(), position.y());
pending_get_render_view_callbacks_.erase(callback_iter);
}
} // namespace content
|