Add chrome.usb.getConfiguration and expose extra descriptors.
[chromium-blink-merge.git] / extensions / browser / guest_view / mime_handler_view / mime_handler_view_guest.cc
blobbba4c871701e352a72c36952c719ecd87fde516c
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/render_process_host.h"
10 #include "content/public/common/url_constants.h"
11 #include "extensions/browser/api/extensions_api_client.h"
12 #include "extensions/browser/guest_view/guest_view_constants.h"
13 #include "extensions/browser/guest_view/guest_view_manager.h"
14 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_constants.h"
15 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest_delegate.h"
16 #include "extensions/common/feature_switch.h"
17 #include "extensions/strings/grit/extensions_strings.h"
18 #include "net/base/url_util.h"
20 using content::WebContents;
22 namespace extensions {
24 // static
25 const char MimeHandlerViewGuest::Type[] = "mimehandler";
27 // static
28 GuestViewBase* MimeHandlerViewGuest::Create(
29 content::BrowserContext* browser_context,
30 int guest_instance_id) {
31 if (!extensions::FeatureSwitch::mime_handler_view()->IsEnabled())
32 return NULL;
34 return new MimeHandlerViewGuest(browser_context, guest_instance_id);
37 MimeHandlerViewGuest::MimeHandlerViewGuest(
38 content::BrowserContext* browser_context,
39 int guest_instance_id)
40 : GuestView<MimeHandlerViewGuest>(browser_context, guest_instance_id),
41 delegate_(ExtensionsAPIClient::Get()->CreateMimeHandlerViewGuestDelegate(
42 this)) {
45 MimeHandlerViewGuest::~MimeHandlerViewGuest() {
48 const char* MimeHandlerViewGuest::GetAPINamespace() const {
49 return "mimeHandlerViewGuestInternal";
52 int MimeHandlerViewGuest::GetTaskPrefix() const {
53 return IDS_EXTENSION_TASK_MANAGER_MIMEHANDLERVIEW_TAG_PREFIX;
56 void MimeHandlerViewGuest::CreateWebContents(
57 const std::string& embedder_extension_id,
58 int embedder_render_process_id,
59 const base::DictionaryValue& create_params,
60 const WebContentsCreatedCallback& callback) {
61 std::string orig_mime_type;
62 bool success =
63 create_params.GetString(mime_handler_view::kMimeType, &orig_mime_type);
64 DCHECK(success && !orig_mime_type.empty());
65 std::string guest_site_str;
66 // Note that we put a prefix "mime-" before the mime type so that this
67 // can never collide with an extension ID.
68 guest_site_str = base::StringPrintf(
69 "%s://mime-%s", content::kGuestScheme, orig_mime_type.c_str());
70 GURL guest_site(guest_site_str);
72 // If we already have a mime handler view for the same mime type, we should
73 // use the same SiteInstance so they go under same process.
74 GuestViewManager* guest_view_manager =
75 GuestViewManager::FromBrowserContext(browser_context());
76 content::SiteInstance* guest_site_instance =
77 guest_view_manager->GetGuestSiteInstance(guest_site);
78 if (!guest_site_instance) {
79 // Create the SiteInstance in a new BrowsingInstance, which will ensure
80 // that guests from different render process are not allowed to send
81 // messages to each other.
82 guest_site_instance =
83 content::SiteInstance::CreateForURL(browser_context(), guest_site);
85 WebContents::CreateParams params(browser_context(), guest_site_instance);
86 params.guest_delegate = this;
87 callback.Run(WebContents::Create(params));
90 void MimeHandlerViewGuest::DidAttachToEmbedder() {
91 std::string src;
92 bool success = attach_params()->GetString(mime_handler_view::kSrc, &src);
93 DCHECK(success && !src.empty());
94 web_contents()->GetController().LoadURL(
95 GURL(src),
96 content::Referrer(),
97 content::PAGE_TRANSITION_AUTO_TOPLEVEL,
98 std::string());
101 void MimeHandlerViewGuest::DidInitialize() {
102 if (delegate_)
103 delegate_->AttachHelpers();
106 void MimeHandlerViewGuest::HandleKeyboardEvent(
107 WebContents* source,
108 const content::NativeWebKeyboardEvent& event) {
109 if (!attached())
110 return;
112 // Send the keyboard events back to the embedder to reprocess them.
113 // TODO(fsamuel): This introduces the possibility of out-of-order keyboard
114 // events because the guest may be arbitrarily delayed when responding to
115 // keyboard events. In that time, the embedder may have received and processed
116 // additional key events. This needs to be fixed as soon as possible.
117 // See http://crbug.com/229882.
118 embedder_web_contents()->GetDelegate()->HandleKeyboardEvent(web_contents(),
119 event);
122 } // namespace extensions