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 "components/guest_view/common/guest_view_constants.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/host_zoom_map.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/stream_handle.h"
13 #include "content/public/browser/stream_info.h"
14 #include "content/public/common/service_registry.h"
15 #include "content/public/common/url_constants.h"
16 #include "extensions/browser/api/extensions_api_client.h"
17 #include "extensions/browser/api/mime_handler_private/mime_handler_private.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_stream_manager.h"
20 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_constants.h"
21 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest_delegate.h"
22 #include "extensions/browser/process_manager.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension_messages.h"
25 #include "extensions/common/guest_view/extensions_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
;
32 using guest_view::GuestViewBase
;
34 namespace extensions
{
36 StreamContainer::StreamContainer(scoped_ptr
<content::StreamInfo
> stream
,
39 const GURL
& handler_url
,
40 const std::string
& extension_id
)
41 : stream_(stream
.Pass()),
44 handler_url_(handler_url
),
45 extension_id_(extension_id
),
50 StreamContainer::~StreamContainer() {
53 void StreamContainer::Abort(const base::Closure
& callback
) {
54 if (!stream_
->handle
) {
58 stream_
->handle
->AddCloseListener(callback
);
59 stream_
->handle
.reset();
62 base::WeakPtr
<StreamContainer
> StreamContainer::GetWeakPtr() {
63 return weak_factory_
.GetWeakPtr();
67 const char MimeHandlerViewGuest::Type
[] = "mimehandler";
70 GuestViewBase
* MimeHandlerViewGuest::Create(WebContents
* owner_web_contents
) {
71 return new MimeHandlerViewGuest(owner_web_contents
);
74 MimeHandlerViewGuest::MimeHandlerViewGuest(WebContents
* owner_web_contents
)
75 : GuestView
<MimeHandlerViewGuest
>(owner_web_contents
),
76 delegate_(ExtensionsAPIClient::Get()->CreateMimeHandlerViewGuestDelegate(
79 MimeHandlerViewGuest::~MimeHandlerViewGuest() {
82 const char* MimeHandlerViewGuest::GetAPINamespace() const {
83 return "mimeHandlerViewGuestInternal";
86 int MimeHandlerViewGuest::GetTaskPrefix() const {
87 return IDS_EXTENSION_TASK_MANAGER_MIMEHANDLERVIEW_TAG_PREFIX
;
90 void MimeHandlerViewGuest::CreateWebContents(
91 const base::DictionaryValue
& create_params
,
92 const WebContentsCreatedCallback
& callback
) {
93 create_params
.GetString(mime_handler_view::kViewId
, &view_id_
);
94 if (view_id_
.empty()) {
95 callback
.Run(nullptr);
99 MimeHandlerStreamManager::Get(browser_context())->ReleaseStream(view_id_
);
101 callback
.Run(nullptr);
104 const Extension
* mime_handler_extension
=
105 // TODO(lazyboy): Do we need handle the case where the extension is
106 // terminated (ExtensionRegistry::TERMINATED)?
107 ExtensionRegistry::Get(browser_context())
108 ->enabled_extensions()
109 .GetByID(stream_
->extension_id());
110 if (!mime_handler_extension
) {
111 LOG(ERROR
) << "Extension for mime_type not found, mime_type = "
112 << stream_
->stream_info()->mime_type
;
113 callback
.Run(nullptr);
117 // Use the mime handler extension's SiteInstance to create the guest so it
118 // goes under the same process as the extension.
119 ProcessManager
* process_manager
= ProcessManager::Get(browser_context());
120 scoped_refptr
<content::SiteInstance
> guest_site_instance
=
121 process_manager
->GetSiteInstanceForURL(stream_
->handler_url());
123 // Clear the zoom level for the mime handler extension. The extension is
124 // responsible for managing its own zoom. This is necessary for OOP PDF, as
125 // otherwise the UI is zoomed and the calculations to determine the PDF size
126 // mix zoomed and unzoomed units.
127 content::HostZoomMap::Get(guest_site_instance
.get())
128 ->SetZoomLevelForHostAndScheme(kExtensionScheme
, stream_
->extension_id(),
131 WebContents::CreateParams
params(browser_context(),
132 guest_site_instance
.get());
133 params
.guest_delegate
= this;
134 callback
.Run(WebContents::Create(params
));
137 void MimeHandlerViewGuest::DidAttachToEmbedder() {
138 web_contents()->GetController().LoadURL(
139 stream_
->handler_url(), content::Referrer(),
140 ui::PAGE_TRANSITION_AUTO_TOPLEVEL
, std::string());
141 web_contents()->GetMainFrame()->GetServiceRegistry()->AddService(
142 base::Bind(&MimeHandlerServiceImpl::Create
, stream_
->GetWeakPtr()));
145 void MimeHandlerViewGuest::DidInitialize(
146 const base::DictionaryValue
& create_params
) {
147 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
150 bool MimeHandlerViewGuest::ShouldHandleFindRequestsForEmbedder() const {
151 return is_full_page_plugin();
154 bool MimeHandlerViewGuest::ZoomPropagatesFromEmbedderToGuest() const {
158 WebContents
* MimeHandlerViewGuest::OpenURLFromTab(
160 const content::OpenURLParams
& params
) {
161 return embedder_web_contents()->GetDelegate()->OpenURLFromTab(
162 embedder_web_contents(), params
);
165 bool MimeHandlerViewGuest::HandleContextMenu(
166 const content::ContextMenuParams
& params
) {
168 return delegate_
->HandleContextMenu(web_contents(), params
);
173 bool MimeHandlerViewGuest::PreHandleGestureEvent(
175 const blink::WebGestureEvent
& event
) {
176 if (event
.type
== blink::WebGestureEvent::GesturePinchBegin
||
177 event
.type
== blink::WebGestureEvent::GesturePinchUpdate
||
178 event
.type
== blink::WebGestureEvent::GesturePinchEnd
) {
179 // If we're an embedded plugin we drop pinch-gestures to avoid zooming the
181 return !is_full_page_plugin();
186 content::JavaScriptDialogManager
*
187 MimeHandlerViewGuest::GetJavaScriptDialogManager(
188 WebContents
* source
) {
189 return owner_web_contents()->GetDelegate()->GetJavaScriptDialogManager(
193 bool MimeHandlerViewGuest::SaveFrame(const GURL
& url
,
194 const content::Referrer
& referrer
) {
198 embedder_web_contents()->SaveFrame(stream_
->stream_info()->original_url
,
203 void MimeHandlerViewGuest::DocumentOnLoadCompletedInMainFrame() {
204 embedder_web_contents()->Send(
205 new ExtensionsGuestViewMsg_MimeHandlerViewGuestOnLoadCompleted(
206 element_instance_id()));
209 base::WeakPtr
<StreamContainer
> MimeHandlerViewGuest::GetStream() const {
211 return base::WeakPtr
<StreamContainer
>();
212 return stream_
->GetWeakPtr();
215 } // namespace extensions