1 // Copyright (c) 2012 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 "chrome/browser/ui/views/extensions/extension_view_views.h"
7 #include "chrome/browser/extensions/extension_view_host.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/views/extensions/extension_popup.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host_view.h"
13 #include "content/public/browser/web_contents.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/common/view_type.h"
16 #include "ui/events/event.h"
17 #include "ui/views/controls/native/native_view_host.h"
18 #include "ui/views/widget/widget.h"
21 #include "ui/base/cursor/cursor.h"
24 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost
* host
,
26 : views::WebView(browser
? browser
->profile() : nullptr),
30 SetWebContents(host_
->web_contents());
33 ExtensionViewViews::~ExtensionViewViews() {
35 parent()->RemoveChildView(this);
38 Browser
* ExtensionViewViews::GetBrowser() {
42 void ExtensionViewViews::SetVisible(bool is_visible
) {
43 if (is_visible
!= visible()) {
44 views::WebView::SetVisible(is_visible
);
46 // Also tell RenderWidgetHostView the new visibility. Despite its name, it
47 // is not part of the View hierarchy and does not know about the change
49 content::RenderWidgetHostView
* host_view
=
50 host_
->render_view_host()->GetView();
60 gfx::NativeView
ExtensionViewViews::GetNativeView() {
61 return holder()->native_view();
64 void ExtensionViewViews::ResizeDueToAutoResize(
65 content::WebContents
* web_contents
,
66 const gfx::Size
& new_size
) {
67 // Don't actually do anything with this information until we have been shown.
68 // Size changes will not be honored by lower layers while we are hidden.
70 pending_preferred_size_
= new_size
;
74 if (new_size
!= GetPreferredSize())
75 SetPreferredSize(new_size
);
78 void ExtensionViewViews::RenderViewCreated(
79 content::RenderViewHost
* render_view_host
) {
80 extensions::ViewType host_type
= host_
->extension_host_type();
81 if (host_type
== extensions::VIEW_TYPE_EXTENSION_POPUP
) {
82 host_
->render_view_host()->EnableAutoResize(
83 gfx::Size(ExtensionPopup::kMinWidth
, ExtensionPopup::kMinHeight
),
84 gfx::Size(ExtensionPopup::kMaxWidth
, ExtensionPopup::kMaxHeight
));
88 void ExtensionViewViews::HandleKeyboardEvent(
89 content::WebContents
* source
,
90 const content::NativeWebKeyboardEvent
& event
) {
92 // Handle lower priority browser shortcuts such as Ctrl-f.
93 browser_
->HandleKeyboardEvent(source
, event
);
97 unhandled_keyboard_event_handler_
.HandleKeyboardEvent(event
,
101 void ExtensionViewViews::DidStopLoading() {
102 // We wait to show the ExtensionViewViews until it has loaded, and the view
103 // has actually been created. These can happen in different orders.
104 // TODO(devlin): Can they? Isn't the view created during construction?
105 if (!visible() && host_
->has_loaded_once()) {
107 ResizeDueToAutoResize(web_contents(), pending_preferred_size_
);
111 gfx::NativeCursor
ExtensionViewViews::GetCursor(const ui::MouseEvent
& event
) {
112 return gfx::kNullCursor
;
115 gfx::Size
ExtensionViewViews::GetMinimumSize() const {
116 // If the minimum size has never been set, returns the preferred size (same
117 // behavior as views::View).
118 return (minimum_size_
== gfx::Size()) ? GetPreferredSize() : minimum_size_
;
121 void ExtensionViewViews::PreferredSizeChanged() {
122 View::PreferredSizeChanged();
124 container_
->OnExtensionSizeChanged(this);
127 void ExtensionViewViews::OnWebContentsAttached() {
128 host_
->CreateRenderViewSoon();
132 namespace extensions
{
135 scoped_ptr
<ExtensionView
> ExtensionViewHost::CreateExtensionView(
136 ExtensionViewHost
* host
,
138 scoped_ptr
<ExtensionViewViews
> view(new ExtensionViewViews(host
, browser
));
139 // We own |view_|, so don't auto delete when it's removed from the view
141 view
->set_owned_by_client();
145 } // namespace extensions