Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2010 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 #include "chrome/browser/extensions/extension_host.h"
      8 #include "chrome/browser/renderer_host/render_widget_host_view_mac.h"
      9 #include "content/browser/renderer_host/render_view_host.h"
     10 
     11 // The minimum/maximum dimensions of the popup.
     12 const CGFloat ExtensionViewMac::kMinWidth = 25.0;
     13 const CGFloat ExtensionViewMac::kMinHeight = 25.0;
     14 const CGFloat ExtensionViewMac::kMaxWidth = 800.0;
     15 const CGFloat ExtensionViewMac::kMaxHeight = 600.0;
     16 
     17 ExtensionViewMac::ExtensionViewMac(ExtensionHost* extension_host,
     18                                    Browser* browser)
     19     : is_toolstrip_(true),
     20       browser_(browser),
     21       extension_host_(extension_host),
     22       render_widget_host_view_(NULL) {
     23   DCHECK(extension_host_);
     24 }
     25 
     26 ExtensionViewMac::~ExtensionViewMac() {
     27   if (render_widget_host_view_)
     28     [render_widget_host_view_->native_view() release];
     29 }
     30 
     31 void ExtensionViewMac::Init() {
     32   CreateWidgetHostView();
     33 }
     34 
     35 gfx::NativeView ExtensionViewMac::native_view() {
     36   DCHECK(render_widget_host_view_);
     37   return render_widget_host_view_->native_view();
     38 }
     39 
     40 RenderViewHost* ExtensionViewMac::render_view_host() const {
     41   return extension_host_->render_view_host();
     42 }
     43 
     44 void ExtensionViewMac::SetBackground(const SkBitmap& background) {
     45   DCHECK(render_widget_host_view_);
     46   if (render_view_host()->IsRenderViewLive()) {
     47     render_widget_host_view_->SetBackground(background);
     48   } else {
     49     pending_background_ = background;
     50   }
     51 }
     52 
     53 void ExtensionViewMac::UpdatePreferredSize(const gfx::Size& new_size) {
     54   // TODO(thakis, erikkay): Windows does some tricks to resize the extension
     55   // view not before it's visible. Do something similar here.
     56 
     57   // No need to use CA here, our caller calls us repeatedly to animate the
     58   // resizing.
     59   NSView* view = native_view();
     60   NSRect frame = [view frame];
     61   frame.size.width = new_size.width();
     62   frame.size.height = new_size.height();
     63 
     64   // |new_size| is in pixels. Convert to view units.
     65   frame.size = [view convertSize:frame.size fromView:nil];
     66 
     67   // On first display of some extensions, this function is called with zero
     68   // width after the correct size has been set. Bail if zero is seen, assuming
     69   // that an extension's view doesn't want any dimensions to ever be zero.
     70   // TODO(andybons): Verify this assumption and look into WebCore's
     71   // |contentesPreferredWidth| to see why this is occurring.
     72   if (NSIsEmptyRect(frame))
     73     return;
     74 
     75   DCHECK([view isKindOfClass:[RenderWidgetHostViewCocoa class]]);
     76   RenderWidgetHostViewCocoa* hostView = (RenderWidgetHostViewCocoa*)view;
     77 
     78   // RenderWidgetHostViewCocoa overrides setFrame but not setFrameSize.
     79   // We need to defer the update back to the RenderWidgetHost so we don't
     80   // get the flickering effect on 10.5 of http://crbug.com/31970
     81   [hostView setFrameWithDeferredUpdate:frame];
     82   [hostView setNeedsDisplay:YES];
     83 }
     84 
     85 void ExtensionViewMac::RenderViewCreated() {
     86   // Do not allow webkit to draw scroll bars on views smaller than
     87   // the largest size view allowed.  The view will be resized to make
     88   // scroll bars unnecessary.  Scroll bars change the height of the
     89   // view, so not drawing them is necessary to avoid infinite resizing.
     90   gfx::Size largest_popup_size(
     91       CGSizeMake(ExtensionViewMac::kMaxWidth, ExtensionViewMac::kMaxHeight));
     92   extension_host_->DisableScrollbarsForSmallWindows(largest_popup_size);
     93 
     94   if (!pending_background_.empty() && render_view_host()->view()) {
     95     render_widget_host_view_->SetBackground(pending_background_);
     96     pending_background_.reset();
     97   }
     98 }
     99 
    100 void ExtensionViewMac::WindowFrameChanged() {
    101   if (render_widget_host_view_)
    102     render_widget_host_view_->WindowFrameChanged();
    103 }
    104 
    105 void ExtensionViewMac::CreateWidgetHostView() {
    106   DCHECK(!render_widget_host_view_);
    107   render_widget_host_view_ = new RenderWidgetHostViewMac(render_view_host());
    108 
    109   // The RenderWidgetHostViewMac is owned by its native view, which is created
    110   // in an autoreleased state. retain it, so that it doesn't immediately
    111   // disappear.
    112   [render_widget_host_view_->native_view() retain];
    113 
    114   extension_host_->CreateRenderViewSoon(render_widget_host_view_);
    115 }
    116