1 // Copyright 2014 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.
5 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
7 #include "base/strings/stringprintf.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/host_zoom_map.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/stream_handle.h"
12 #include "content/public/browser/stream_info.h"
13 #include "content/public/common/service_registry.h"
14 #include "content/public/common/url_constants.h"
15 #include "extensions/browser/api/extensions_api_client.h"
16 #include "extensions/browser/api/mime_handler_private/mime_handler_private.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_stream_manager.h"
19 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_constants.h"
20 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest_delegate.h"
21 #include "extensions/browser/process_manager.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/extension_messages.h"
24 #include "extensions/common/guest_view/guest_view_constants.h"
25 #include "extensions/common/guest_view/guest_view_messages.h"
26 #include "extensions/strings/grit/extensions_strings.h"
27 #include "ipc/ipc_message_macros.h"
28 #include "net/base/url_util.h"
29 #include "third_party/WebKit/public/web/WebInputEvent.h"
31 using content::WebContents
;
33 namespace extensions
{
35 StreamContainer::StreamContainer(scoped_ptr
<content::StreamInfo
> stream
,
38 const GURL
& handler_url
,
39 const std::string
& extension_id
)
40 : stream_(stream
.Pass()),
43 handler_url_(handler_url
),
44 extension_id_(extension_id
),
49 StreamContainer::~StreamContainer() {
52 void StreamContainer::Abort(const base::Closure
& callback
) {
53 if (!stream_
->handle
) {
57 stream_
->handle
->AddCloseListener(callback
);
58 stream_
->handle
.reset();
61 base::WeakPtr
<StreamContainer
> StreamContainer::GetWeakPtr() {
62 return weak_factory_
.GetWeakPtr();
66 const char MimeHandlerViewGuest::Type
[] = "mimehandler";
69 GuestViewBase
* MimeHandlerViewGuest::Create(
70 content::WebContents
* owner_web_contents
) {
71 return new MimeHandlerViewGuest(owner_web_contents
);
74 MimeHandlerViewGuest::MimeHandlerViewGuest(
75 content::WebContents
* owner_web_contents
)
76 : GuestView
<MimeHandlerViewGuest
>(owner_web_contents
),
77 delegate_(ExtensionsAPIClient::Get()->CreateMimeHandlerViewGuestDelegate(
81 MimeHandlerViewGuest::~MimeHandlerViewGuest() {
84 WindowController
* MimeHandlerViewGuest::GetExtensionWindowController() const {
88 WebContents
* MimeHandlerViewGuest::GetAssociatedWebContents() const {
89 return web_contents();
92 const char* MimeHandlerViewGuest::GetAPINamespace() const {
93 return "mimeHandlerViewGuestInternal";
96 int MimeHandlerViewGuest::GetTaskPrefix() const {
97 return IDS_EXTENSION_TASK_MANAGER_MIMEHANDLERVIEW_TAG_PREFIX
;
100 void MimeHandlerViewGuest::CreateWebContents(
101 const base::DictionaryValue
& create_params
,
102 const WebContentsCreatedCallback
& callback
) {
103 create_params
.GetString(mime_handler_view::kViewId
, &view_id_
);
104 if (view_id_
.empty()) {
105 callback
.Run(nullptr);
109 MimeHandlerStreamManager::Get(browser_context())->ReleaseStream(view_id_
);
111 callback
.Run(nullptr);
114 const Extension
* mime_handler_extension
=
115 // TODO(lazyboy): Do we need handle the case where the extension is
116 // terminated (ExtensionRegistry::TERMINATED)?
117 ExtensionRegistry::Get(browser_context())
118 ->enabled_extensions()
119 .GetByID(stream_
->extension_id());
120 if (!mime_handler_extension
) {
121 LOG(ERROR
) << "Extension for mime_type not found, mime_type = "
122 << stream_
->stream_info()->mime_type
;
123 callback
.Run(nullptr);
127 // Use the mime handler extension's SiteInstance to create the guest so it
128 // goes under the same process as the extension.
129 ProcessManager
* process_manager
= ProcessManager::Get(browser_context());
130 scoped_refptr
<content::SiteInstance
> guest_site_instance
=
131 process_manager
->GetSiteInstanceForURL(stream_
->handler_url());
133 // Clear the zoom level for the mime handler extension. The extension is
134 // responsible for managing its own zoom. This is necessary for OOP PDF, as
135 // otherwise the UI is zoomed and the calculations to determine the PDF size
136 // mix zoomed and unzoomed units.
137 content::HostZoomMap::Get(guest_site_instance
.get())
138 ->SetZoomLevelForHostAndScheme(kExtensionScheme
, stream_
->extension_id(),
141 WebContents::CreateParams
params(browser_context(),
142 guest_site_instance
.get());
143 params
.guest_delegate
= this;
144 callback
.Run(WebContents::Create(params
));
147 void MimeHandlerViewGuest::DidAttachToEmbedder() {
148 web_contents()->GetController().LoadURL(
149 stream_
->handler_url(), content::Referrer(),
150 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
, std::string());
151 web_contents()->GetMainFrame()->GetServiceRegistry()->AddService(
152 base::Bind(&MimeHandlerServiceImpl::Create
, stream_
->GetWeakPtr()));
155 void MimeHandlerViewGuest::DidInitialize(
156 const base::DictionaryValue
& create_params
) {
157 extension_function_dispatcher_
.reset(
158 new ExtensionFunctionDispatcher(browser_context(), this));
160 delegate_
->AttachHelpers();
163 bool MimeHandlerViewGuest::ZoomPropagatesFromEmbedderToGuest() const {
167 bool MimeHandlerViewGuest::Find(int request_id
,
168 const base::string16
& search_text
,
169 const blink::WebFindOptions
& options
) {
170 if (is_full_page_plugin()) {
171 web_contents()->Find(request_id
, search_text
, options
);
177 bool MimeHandlerViewGuest::StopFinding(content::StopFindAction action
) {
178 if (is_full_page_plugin()) {
179 web_contents()->StopFinding(action
);
185 content::WebContents
* MimeHandlerViewGuest::OpenURLFromTab(
186 content::WebContents
* source
,
187 const content::OpenURLParams
& params
) {
188 return embedder_web_contents()->GetDelegate()->OpenURLFromTab(
189 embedder_web_contents(), params
);
192 bool MimeHandlerViewGuest::HandleContextMenu(
193 const content::ContextMenuParams
& params
) {
195 return delegate_
->HandleContextMenu(web_contents(), params
);
200 bool MimeHandlerViewGuest::PreHandleGestureEvent(
201 content::WebContents
* source
,
202 const blink::WebGestureEvent
& event
) {
203 if (event
.type
== blink::WebGestureEvent::GesturePinchBegin
||
204 event
.type
== blink::WebGestureEvent::GesturePinchUpdate
||
205 event
.type
== blink::WebGestureEvent::GesturePinchEnd
) {
206 // If we're an embedded plugin we drop pinch-gestures to avoid zooming the
208 return !is_full_page_plugin();
213 content::JavaScriptDialogManager
*
214 MimeHandlerViewGuest::GetJavaScriptDialogManager(
215 WebContents
* source
) {
216 return owner_web_contents()->GetDelegate()->GetJavaScriptDialogManager(
220 void MimeHandlerViewGuest::FindReply(content::WebContents
* web_contents
,
222 int number_of_matches
,
223 const gfx::Rect
& selection_rect
,
224 int active_match_ordinal
,
226 if (!attached() || !embedder_web_contents()->GetDelegate())
229 embedder_web_contents()->GetDelegate()->FindReply(embedder_web_contents(),
233 active_match_ordinal
,
237 bool MimeHandlerViewGuest::SaveFrame(const GURL
& url
,
238 const content::Referrer
& referrer
) {
242 embedder_web_contents()->SaveFrame(stream_
->stream_info()->original_url
,
247 void MimeHandlerViewGuest::DocumentOnLoadCompletedInMainFrame() {
248 embedder_web_contents()->Send(
249 new GuestViewMsg_MimeHandlerViewGuestOnLoadCompleted(
250 element_instance_id()));
253 bool MimeHandlerViewGuest::OnMessageReceived(const IPC::Message
& message
) {
255 IPC_BEGIN_MESSAGE_MAP(MimeHandlerViewGuest
, message
)
256 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
257 IPC_MESSAGE_UNHANDLED(handled
= false)
258 IPC_END_MESSAGE_MAP()
262 base::WeakPtr
<StreamContainer
> MimeHandlerViewGuest::GetStream() const {
264 return base::WeakPtr
<StreamContainer
>();
265 return stream_
->GetWeakPtr();
268 void MimeHandlerViewGuest::OnRequest(
269 const ExtensionHostMsg_Request_Params
& params
) {
270 if (extension_function_dispatcher_
) {
271 extension_function_dispatcher_
->Dispatch(
272 params
, web_contents()->GetRenderViewHost());
276 } // namespace extensions