Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / guest_view / guest_view_internal_api.cc
blob6a60f566b7d8d791dbbaf0d087b1c19255816211
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/api/guest_view/guest_view_internal_api.h"
7 #include "components/guest_view/browser/guest_view_base.h"
8 #include "components/guest_view/browser/guest_view_manager.h"
9 #include "components/guest_view/browser/guest_view_manager_delegate.h"
10 #include "components/guest_view/common/guest_view_constants.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "extensions/browser/api/extensions_api_client.h"
14 #include "extensions/common/api/guest_view_internal.h"
15 #include "extensions/common/permissions/permissions_data.h"
17 using guest_view::GuestViewBase;
18 using guest_view::GuestViewManager;
19 using guest_view::GuestViewManagerDelegate;
21 namespace guest_view_internal = extensions::api::guest_view_internal;
23 namespace extensions {
25 GuestViewInternalCreateGuestFunction::
26 GuestViewInternalCreateGuestFunction() {
29 bool GuestViewInternalCreateGuestFunction::RunAsync() {
30 std::string view_type;
31 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &view_type));
33 base::DictionaryValue* create_params;
34 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &create_params));
36 // Since we are creating a new guest, we will create a GuestViewManager
37 // if we don't already have one.
38 GuestViewManager* guest_view_manager =
39 GuestViewManager::FromBrowserContext(browser_context());
40 if (!guest_view_manager) {
41 guest_view_manager = GuestViewManager::CreateWithDelegate(
42 browser_context(),
43 ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate(context_));
46 GuestViewManager::WebContentsCreatedCallback callback =
47 base::Bind(&GuestViewInternalCreateGuestFunction::CreateGuestCallback,
48 this);
50 content::WebContents* sender_web_contents = GetSenderWebContents();
51 if (!sender_web_contents) {
52 error_ = "Guest views can only be embedded in web content";
53 return false;
56 // Add flag to |create_params| to indicate that the element size is specified
57 // in logical units.
58 create_params->SetBoolean(guest_view::kElementSizeIsLogical, true);
60 guest_view_manager->CreateGuest(view_type,
61 sender_web_contents,
62 *create_params,
63 callback);
64 return true;
67 void GuestViewInternalCreateGuestFunction::CreateGuestCallback(
68 content::WebContents* guest_web_contents) {
69 int guest_instance_id = 0;
70 int content_window_id = MSG_ROUTING_NONE;
71 if (guest_web_contents) {
72 GuestViewBase* guest = GuestViewBase::FromWebContents(guest_web_contents);
73 guest_instance_id = guest->guest_instance_id();
74 content_window_id = guest->proxy_routing_id();
76 scoped_ptr<base::DictionaryValue> return_params(new base::DictionaryValue());
77 return_params->SetInteger(guest_view::kID, guest_instance_id);
78 return_params->SetInteger(guest_view::kContentWindowID, content_window_id);
79 SetResult(return_params.release());
80 SendResponse(true);
83 GuestViewInternalDestroyGuestFunction::
84 GuestViewInternalDestroyGuestFunction() {
87 GuestViewInternalDestroyGuestFunction::
88 ~GuestViewInternalDestroyGuestFunction() {
91 bool GuestViewInternalDestroyGuestFunction::RunAsync() {
92 scoped_ptr<guest_view_internal::DestroyGuest::Params> params(
93 guest_view_internal::DestroyGuest::Params::Create(*args_));
94 EXTENSION_FUNCTION_VALIDATE(params.get());
95 GuestViewBase* guest = GuestViewBase::From(
96 render_frame_host()->GetProcess()->GetID(), params->instance_id);
97 if (!guest)
98 return false;
99 guest->Destroy();
100 SendResponse(true);
101 return true;
104 GuestViewInternalSetSizeFunction::GuestViewInternalSetSizeFunction() {
107 GuestViewInternalSetSizeFunction::~GuestViewInternalSetSizeFunction() {
110 bool GuestViewInternalSetSizeFunction::RunAsync() {
111 scoped_ptr<guest_view_internal::SetSize::Params> params(
112 guest_view_internal::SetSize::Params::Create(*args_));
113 EXTENSION_FUNCTION_VALIDATE(params.get());
114 GuestViewBase* guest = GuestViewBase::From(
115 render_frame_host()->GetProcess()->GetID(), params->instance_id);
116 if (!guest)
117 return false;
119 guest_view::SetSizeParams set_size_params;
120 if (params->params.enable_auto_size) {
121 set_size_params.enable_auto_size.reset(
122 params->params.enable_auto_size.release());
124 if (params->params.min) {
125 set_size_params.min_size.reset(
126 new gfx::Size(params->params.min->width, params->params.min->height));
128 if (params->params.max) {
129 set_size_params.max_size.reset(
130 new gfx::Size(params->params.max->width, params->params.max->height));
132 if (params->params.normal) {
133 set_size_params.normal_size.reset(new gfx::Size(
134 params->params.normal->width, params->params.normal->height));
137 guest->SetSize(set_size_params);
138 SendResponse(true);
139 return true;
142 } // namespace extensions