Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / extensions / browser / guest_view / mime_handler_view / mime_handler_view_guest.cc
blob2f4466236e9f673befc1f418598b18a76fd64fcf
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::ZoomPropagatesFromEmbedderToGuest() const {
154 return false;
157 bool MimeHandlerViewGuest::Find(int request_id,
158 const base::string16& search_text,
159 const blink::WebFindOptions& options) {
160 if (is_full_page_plugin()) {
161 web_contents()->Find(request_id, search_text, options);
162 return true;
164 return false;
167 bool MimeHandlerViewGuest::StopFinding(content::StopFindAction action) {
168 if (is_full_page_plugin()) {
169 web_contents()->StopFinding(action);
170 return true;
172 return false;
175 content::WebContents* MimeHandlerViewGuest::OpenURLFromTab(
176 content::WebContents* source,
177 const content::OpenURLParams& params) {
178 return embedder_web_contents()->GetDelegate()->OpenURLFromTab(
179 embedder_web_contents(), params);
182 bool MimeHandlerViewGuest::HandleContextMenu(
183 const content::ContextMenuParams& params) {
184 if (delegate_)
185 return delegate_->HandleContextMenu(web_contents(), params);
187 return false;
190 bool MimeHandlerViewGuest::PreHandleGestureEvent(
191 content::WebContents* source,
192 const blink::WebGestureEvent& event) {
193 if (event.type == blink::WebGestureEvent::GesturePinchBegin ||
194 event.type == blink::WebGestureEvent::GesturePinchUpdate ||
195 event.type == blink::WebGestureEvent::GesturePinchEnd) {
196 // If we're an embedded plugin we drop pinch-gestures to avoid zooming the
197 // guest.
198 return !is_full_page_plugin();
200 return false;
203 content::JavaScriptDialogManager*
204 MimeHandlerViewGuest::GetJavaScriptDialogManager(
205 WebContents* source) {
206 return owner_web_contents()->GetDelegate()->GetJavaScriptDialogManager(
207 web_contents());
210 void MimeHandlerViewGuest::FindReply(content::WebContents* web_contents,
211 int request_id,
212 int number_of_matches,
213 const gfx::Rect& selection_rect,
214 int active_match_ordinal,
215 bool final_update) {
216 if (!attached() || !embedder_web_contents()->GetDelegate())
217 return;
219 embedder_web_contents()->GetDelegate()->FindReply(embedder_web_contents(),
220 request_id,
221 number_of_matches,
222 selection_rect,
223 active_match_ordinal,
224 final_update);
227 bool MimeHandlerViewGuest::SaveFrame(const GURL& url,
228 const content::Referrer& referrer) {
229 if (!attached())
230 return false;
232 embedder_web_contents()->SaveFrame(stream_->stream_info()->original_url,
233 referrer);
234 return true;
237 void MimeHandlerViewGuest::DocumentOnLoadCompletedInMainFrame() {
238 embedder_web_contents()->Send(
239 new ExtensionsGuestViewMsg_MimeHandlerViewGuestOnLoadCompleted(
240 element_instance_id()));
243 base::WeakPtr<StreamContainer> MimeHandlerViewGuest::GetStream() const {
244 if (!stream_)
245 return base::WeakPtr<StreamContainer>();
246 return stream_->GetWeakPtr();
249 } // namespace extensions