Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / extensions / extension_view_mac.mm
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.
4
5 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h"
6
7 #import <Cocoa/Cocoa.h>
8
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"
17
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;
23
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];
31 }
32
33 ExtensionViewMac::~ExtensionViewMac() {
34 }
35
36 void ExtensionViewMac::WindowFrameChanged() {
37   if (render_view_host()->GetView())
38     render_view_host()->GetView()->WindowFrameChanged();
39 }
40
41 void ExtensionViewMac::Init() {
42   CreateWidgetHostView();
43 }
44
45 Browser* ExtensionViewMac::GetBrowser() {
46   return browser_;
47 }
48
49 gfx::NativeView ExtensionViewMac::GetNativeView() {
50   return extension_host_->host_contents()->GetNativeView();
51 }
52
53 void ExtensionViewMac::ResizeDueToAutoResize(const gfx::Size& new_size) {
54   if (container_)
55     container_->OnExtensionSizeChanged(this, new_size);
56 }
57
58 void ExtensionViewMac::RenderViewCreated() {
59   extensions::ViewType host_type = extension_host_->extension_host_type();
60   if (host_type == extensions::VIEW_TYPE_EXTENSION_POPUP) {
61     gfx::Size min_size(ExtensionViewMac::kMinWidth,
62                        ExtensionViewMac::kMinHeight);
63     gfx::Size max_size(ExtensionViewMac::kMaxWidth,
64                        ExtensionViewMac::kMaxHeight);
65     render_view_host()->EnableAutoResize(min_size, max_size);
66   }
67 }
68
69 void ExtensionViewMac::HandleKeyboardEvent(
70     content::WebContents* source,
71     const content::NativeWebKeyboardEvent& event) {
72   if (event.skip_in_browser ||
73       event.type == content::NativeWebKeyboardEvent::Char ||
74       extension_host_->extension_host_type() !=
75           extensions::VIEW_TYPE_EXTENSION_POPUP)
76     return;
77
78   ChromeEventProcessingWindow* event_window =
79       base::mac::ObjCCastStrict<ChromeEventProcessingWindow>(
80           [GetNativeView() window]);
81   [event_window redispatchKeyEvent:event.os_event];
82 }
83
84 void ExtensionViewMac::DidStopLoading() {
85   ShowIfCompletelyLoaded();
86 }
87
88 content::RenderViewHost* ExtensionViewMac::render_view_host() const {
89   return extension_host_->render_view_host();
90 }
91
92 void ExtensionViewMac::CreateWidgetHostView() {
93   extension_host_->CreateRenderViewSoon();
94 }
95
96 void ExtensionViewMac::ShowIfCompletelyLoaded() {
97   // We wait to show the ExtensionView until it has loaded, and the view has
98   // actually been created. These can happen in different orders.
99   if (extension_host_->did_stop_loading()) {
100     [GetNativeView() setHidden:NO];
101     if (container_)
102       container_->OnExtensionViewDidShow(this);
103   }
104 }
105
106 namespace extensions {
107
108 // static
109 scoped_ptr<ExtensionView> ExtensionViewHost::CreateExtensionView(
110     ExtensionViewHost* host,
111     Browser* browser) {
112   scoped_ptr<ExtensionViewMac> view(new ExtensionViewMac(host, browser));
113   return view.PassAs<ExtensionView>();
114 }
115
116 }  // namespace extensions