Update broken references to image assets
[chromium-blink-merge.git] / extensions / browser / guest_view / mime_handler_view / mime_handler_view_guest.cc
blobf8db13696e0309850a605c3e4d25f3ba31deb948
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,
37 int tab_id,
38 bool embedded,
39 const GURL& handler_url,
40 const std::string& extension_id)
41 : stream_(stream.Pass()),
42 embedded_(embedded),
43 tab_id_(tab_id),
44 handler_url_(handler_url),
45 extension_id_(extension_id),
46 weak_factory_(this) {
47 DCHECK(stream_);
50 StreamContainer::~StreamContainer() {
53 void StreamContainer::Abort(const base::Closure& callback) {
54 if (!stream_->handle) {
55 callback.Run();
56 return;
58 stream_->handle->AddCloseListener(callback);
59 stream_->handle.reset();
62 base::WeakPtr<StreamContainer> StreamContainer::GetWeakPtr() {
63 return weak_factory_.GetWeakPtr();
66 // static
67 const char MimeHandlerViewGuest::Type[] = "mimehandler";
69 // static
70 GuestViewBase* MimeHandlerViewGuest::Create(
71 content::WebContents* owner_web_contents) {
72 return new MimeHandlerViewGuest(owner_web_contents);
75 MimeHandlerViewGuest::MimeHandlerViewGuest(
76 content::WebContents* owner_web_contents)
77 : GuestView<MimeHandlerViewGuest>(owner_web_contents),
78 delegate_(ExtensionsAPIClient::Get()->CreateMimeHandlerViewGuestDelegate(
79 this)) {
82 MimeHandlerViewGuest::~MimeHandlerViewGuest() {
85 const char* MimeHandlerViewGuest::GetAPINamespace() const {
86 return "mimeHandlerViewGuestInternal";
89 int MimeHandlerViewGuest::GetTaskPrefix() const {
90 return IDS_EXTENSION_TASK_MANAGER_MIMEHANDLERVIEW_TAG_PREFIX;
93 void MimeHandlerViewGuest::CreateWebContents(
94 const base::DictionaryValue& create_params,
95 const WebContentsCreatedCallback& callback) {
96 create_params.GetString(mime_handler_view::kViewId, &view_id_);
97 if (view_id_.empty()) {
98 callback.Run(nullptr);
99 return;
101 stream_ =
102 MimeHandlerStreamManager::Get(browser_context())->ReleaseStream(view_id_);
103 if (!stream_) {
104 callback.Run(nullptr);
105 return;
107 const Extension* mime_handler_extension =
108 // TODO(lazyboy): Do we need handle the case where the extension is
109 // terminated (ExtensionRegistry::TERMINATED)?
110 ExtensionRegistry::Get(browser_context())
111 ->enabled_extensions()
112 .GetByID(stream_->extension_id());
113 if (!mime_handler_extension) {
114 LOG(ERROR) << "Extension for mime_type not found, mime_type = "
115 << stream_->stream_info()->mime_type;
116 callback.Run(nullptr);
117 return;
120 // Use the mime handler extension's SiteInstance to create the guest so it
121 // goes under the same process as the extension.
122 ProcessManager* process_manager = ProcessManager::Get(browser_context());
123 scoped_refptr<content::SiteInstance> guest_site_instance =
124 process_manager->GetSiteInstanceForURL(stream_->handler_url());
126 // Clear the zoom level for the mime handler extension. The extension is
127 // responsible for managing its own zoom. This is necessary for OOP PDF, as
128 // otherwise the UI is zoomed and the calculations to determine the PDF size
129 // mix zoomed and unzoomed units.
130 content::HostZoomMap::Get(guest_site_instance.get())
131 ->SetZoomLevelForHostAndScheme(kExtensionScheme, stream_->extension_id(),
134 WebContents::CreateParams params(browser_context(),
135 guest_site_instance.get());
136 params.guest_delegate = this;
137 callback.Run(WebContents::Create(params));
140 void MimeHandlerViewGuest::DidAttachToEmbedder() {
141 web_contents()->GetController().LoadURL(
142 stream_->handler_url(), content::Referrer(),
143 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, std::string());
144 web_contents()->GetMainFrame()->GetServiceRegistry()->AddService(
145 base::Bind(&MimeHandlerServiceImpl::Create, stream_->GetWeakPtr()));
148 void MimeHandlerViewGuest::DidInitialize(
149 const base::DictionaryValue& create_params) {
150 ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
153 bool MimeHandlerViewGuest::ShouldHandleFindRequestsForEmbedder() const {
154 return is_full_page_plugin();
157 bool MimeHandlerViewGuest::ZoomPropagatesFromEmbedderToGuest() const {
158 return false;
161 content::WebContents* MimeHandlerViewGuest::OpenURLFromTab(
162 content::WebContents* source,
163 const content::OpenURLParams& params) {
164 return embedder_web_contents()->GetDelegate()->OpenURLFromTab(
165 embedder_web_contents(), params);
168 bool MimeHandlerViewGuest::HandleContextMenu(
169 const content::ContextMenuParams& params) {
170 if (delegate_)
171 return delegate_->HandleContextMenu(web_contents(), params);
173 return false;
176 bool MimeHandlerViewGuest::PreHandleGestureEvent(
177 content::WebContents* source,
178 const blink::WebGestureEvent& event) {
179 if (event.type == blink::WebGestureEvent::GesturePinchBegin ||
180 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
181 event.type == blink::WebGestureEvent::GesturePinchEnd) {
182 // If we're an embedded plugin we drop pinch-gestures to avoid zooming the
183 // guest.
184 return !is_full_page_plugin();
186 return false;
189 content::JavaScriptDialogManager*
190 MimeHandlerViewGuest::GetJavaScriptDialogManager(
191 WebContents* source) {
192 return owner_web_contents()->GetDelegate()->GetJavaScriptDialogManager(
193 web_contents());
196 void MimeHandlerViewGuest::FindReply(content::WebContents* web_contents,
197 int request_id,
198 int number_of_matches,
199 const gfx::Rect& selection_rect,
200 int active_match_ordinal,
201 bool final_update) {
202 if (!attached() || !embedder_web_contents()->GetDelegate())
203 return;
205 embedder_web_contents()->GetDelegate()->FindReply(embedder_web_contents(),
206 request_id,
207 number_of_matches,
208 selection_rect,
209 active_match_ordinal,
210 final_update);
213 bool MimeHandlerViewGuest::SaveFrame(const GURL& url,
214 const content::Referrer& referrer) {
215 if (!attached())
216 return false;
218 embedder_web_contents()->SaveFrame(stream_->stream_info()->original_url,
219 referrer);
220 return true;
223 void MimeHandlerViewGuest::DocumentOnLoadCompletedInMainFrame() {
224 embedder_web_contents()->Send(
225 new ExtensionsGuestViewMsg_MimeHandlerViewGuestOnLoadCompleted(
226 element_instance_id()));
229 base::WeakPtr<StreamContainer> MimeHandlerViewGuest::GetStream() const {
230 if (!stream_)
231 return base::WeakPtr<StreamContainer>();
232 return stream_->GetWeakPtr();
235 } // namespace extensions