Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / extension_view_views.cc
blob106bd8a5727d294be997e47f3a9df8cd3182d403
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"
20 #if defined(USE_AURA)
21 #include "ui/base/cursor/cursor.h"
22 #endif
24 ExtensionViewViews::ExtensionViewViews(extensions::ExtensionHost* host,
25 Browser* browser)
26 : views::WebView(browser ? browser->profile() : nullptr),
27 host_(host),
28 browser_(browser),
29 container_(nullptr) {
30 SetWebContents(host_->web_contents());
33 ExtensionViewViews::~ExtensionViewViews() {
34 if (parent())
35 parent()->RemoveChildView(this);
38 Browser* ExtensionViewViews::GetBrowser() {
39 return browser_;
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
48 // unless we tell it.
49 content::RenderWidgetHostView* host_view =
50 host_->render_view_host()->GetView();
51 if (host_view) {
52 if (is_visible)
53 host_view->Show();
54 else
55 host_view->Hide();
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.
69 if (!visible()) {
70 pending_preferred_size_ = new_size;
71 return;
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) {
91 if (browser_) {
92 // Handle lower priority browser shortcuts such as Ctrl-f.
93 browser_->HandleKeyboardEvent(source, event);
94 return;
97 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
98 GetFocusManager());
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()) {
106 SetVisible(true);
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();
123 if (container_)
124 container_->OnExtensionSizeChanged(this);
127 void ExtensionViewViews::OnWebContentsAttached() {
128 host_->CreateRenderViewSoon();
129 SetVisible(false);
132 namespace extensions {
134 // static
135 scoped_ptr<ExtensionView> ExtensionViewHost::CreateExtensionView(
136 ExtensionViewHost* host,
137 Browser* browser) {
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
140 // hierarchy.
141 view->set_owned_by_client();
142 return view.Pass();
145 } // namespace extensions