Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / extension_view_mac.mm
blob32c362b60b58b312fc0fa53a6405c2867daaee58
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/cocoa/extensions/extension_view_mac.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/foundation_util.h"
10 #include "chrome/browser/extensions/extension_view_host.h"
11 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "extensions/browser/extension_host.h"
16 #include "extensions/common/view_type.h"
18 // The minimum/maximum dimensions of the popup.
19 const CGFloat ExtensionViewMac::kMinWidth = 25.0;
20 const CGFloat ExtensionViewMac::kMinHeight = 25.0;
21 const CGFloat ExtensionViewMac::kMaxWidth = 800.0;
22 const CGFloat ExtensionViewMac::kMaxHeight = 600.0;
24 ExtensionViewMac::ExtensionViewMac(extensions::ExtensionHost* extension_host,
25                                    Browser* browser)
26     : browser_(browser),
27       extension_host_(extension_host),
28       container_(NULL) {
29   DCHECK(extension_host_);
30   [GetNativeView() setHidden:YES];
33 ExtensionViewMac::~ExtensionViewMac() {
36 void ExtensionViewMac::WindowFrameChanged() {
37   if (render_view_host()->GetView())
38     render_view_host()->GetView()->WindowFrameChanged();
41 void ExtensionViewMac::CreateWidgetHostViewIn(gfx::NativeView superview) {
42   [superview addSubview:GetNativeView()];
43   extension_host_->CreateRenderViewSoon();
46 Browser* ExtensionViewMac::GetBrowser() {
47   return browser_;
50 gfx::NativeView ExtensionViewMac::GetNativeView() {
51   return extension_host_->host_contents()->GetNativeView();
54 void ExtensionViewMac::ResizeDueToAutoResize(content::WebContents* web_contents,
55                                              const gfx::Size& new_size) {
56   if (container_)
57     container_->OnExtensionSizeChanged(this, new_size);
60 void ExtensionViewMac::RenderViewCreated(content::RenderViewHost* host) {
61   extensions::ViewType host_type = extension_host_->extension_host_type();
62   if (host_type == extensions::VIEW_TYPE_EXTENSION_POPUP) {
63     gfx::Size min_size(ExtensionViewMac::kMinWidth,
64                        ExtensionViewMac::kMinHeight);
65     gfx::Size max_size(ExtensionViewMac::kMaxWidth,
66                        ExtensionViewMac::kMaxHeight);
67     render_view_host()->EnableAutoResize(min_size, max_size);
68   }
71 void ExtensionViewMac::HandleKeyboardEvent(
72     content::WebContents* source,
73     const content::NativeWebKeyboardEvent& event) {
74   if (event.skip_in_browser ||
75       event.type == content::NativeWebKeyboardEvent::Char ||
76       extension_host_->extension_host_type() !=
77           extensions::VIEW_TYPE_EXTENSION_POPUP)
78     return;
80   ChromeEventProcessingWindow* event_window =
81       base::mac::ObjCCastStrict<ChromeEventProcessingWindow>(
82           [GetNativeView() window]);
83   [event_window redispatchKeyEvent:event.os_event];
86 void ExtensionViewMac::DidStopLoading() {
87   ShowIfCompletelyLoaded();
90 content::RenderViewHost* ExtensionViewMac::render_view_host() const {
91   return extension_host_->render_view_host();
94 void ExtensionViewMac::ShowIfCompletelyLoaded() {
95   // We wait to show the ExtensionView until it has loaded, and the view has
96   // actually been created. These can happen in different orders.
97   if (extension_host_->has_loaded_once()) {
98     [GetNativeView() setHidden:NO];
99     if (container_)
100       container_->OnExtensionViewDidShow(this);
101   }
104 namespace extensions {
106 // static
107 scoped_ptr<ExtensionView> ExtensionViewHost::CreateExtensionView(
108     ExtensionViewHost* host,
109     Browser* browser) {
110   return make_scoped_ptr(new ExtensionViewMac(host, browser));
113 }  // namespace extensions