Home | History | Annotate | Download | only in extensions
      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 #ifndef CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_POPUP_H_
      6 #define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_POPUP_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/compiler_specific.h"
     10 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
     11 #include "chrome/browser/ui/views/extensions/extension_view_views.h"
     12 #include "content/public/browser/notification_observer.h"
     13 #include "content/public/browser/notification_registrar.h"
     14 #include "ui/views/bubble/bubble_delegate.h"
     15 #include "ui/wm/public/activation_change_observer.h"
     16 #include "url/gurl.h"
     17 
     18 
     19 class Browser;
     20 namespace views {
     21 class Widget;
     22 }
     23 
     24 namespace content {
     25 class DevToolsAgentHost;
     26 }
     27 
     28 namespace extensions {
     29 class ExtensionViewHost;
     30 }
     31 
     32 class ExtensionPopup : public views::BubbleDelegateView,
     33                        public aura::client::ActivationChangeObserver,
     34                        public ExtensionViewViews::Container,
     35                        public content::NotificationObserver,
     36                        public TabStripModelObserver {
     37  public:
     38   enum ShowAction {
     39     SHOW,
     40     SHOW_AND_INSPECT,
     41   };
     42 
     43   virtual ~ExtensionPopup();
     44 
     45   // Create and show a popup with |url| positioned adjacent to |anchor_view|.
     46   // |browser| is the browser to which the pop-up will be attached.  NULL is a
     47   // valid parameter for pop-ups not associated with a browser.
     48   // The positioning of the pop-up is determined by |arrow| according to the
     49   // following logic:  The popup is anchored so that the corner indicated by the
     50   // value of |arrow| remains fixed during popup resizes.  If |arrow| is
     51   // BOTTOM_*, then the popup 'pops up', otherwise the popup 'drops down'.
     52   // The actual display of the popup is delayed until the page contents
     53   // finish loading in order to minimize UI flashing and resizing.
     54   static ExtensionPopup* ShowPopup(const GURL& url,
     55                                    Browser* browser,
     56                                    views::View* anchor_view,
     57                                    views::BubbleBorder::Arrow arrow,
     58                                    ShowAction show_action);
     59 
     60   extensions::ExtensionViewHost* host() const { return host_.get(); }
     61 
     62   // content::NotificationObserver overrides.
     63   virtual void Observe(int type,
     64                        const content::NotificationSource& source,
     65                        const content::NotificationDetails& details) OVERRIDE;
     66 
     67   // ExtensionViewViews::Container overrides.
     68   virtual void OnExtensionSizeChanged(ExtensionViewViews* view) OVERRIDE;
     69 
     70   // views::View overrides.
     71   virtual gfx::Size GetPreferredSize() const OVERRIDE;
     72   virtual void ViewHierarchyChanged(
     73       const ViewHierarchyChangedDetails& details) OVERRIDE;
     74 
     75   // views::WidgetObserver overrides.
     76   virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
     77   virtual void OnWidgetActivationChanged(views::Widget* widget,
     78                                          bool active) OVERRIDE;
     79 
     80   // aura::client::ActivationChangeObserver overrides.
     81   virtual void OnWindowActivated(aura::Window* gained_active,
     82                                  aura::Window* lost_active) OVERRIDE;
     83 
     84   // TabStripModelObserver overrides.
     85   virtual void ActiveTabChanged(content::WebContents* old_contents,
     86                                 content::WebContents* new_contents,
     87                                 int index,
     88                                 int reason) OVERRIDE;
     89 
     90   // The min/max height of popups.
     91   static const int kMinWidth;
     92   static const int kMinHeight;
     93   static const int kMaxWidth;
     94   static const int kMaxHeight;
     95 
     96  private:
     97   ExtensionPopup(extensions::ExtensionViewHost* host,
     98                  views::View* anchor_view,
     99                  views::BubbleBorder::Arrow arrow,
    100                  ShowAction show_action);
    101 
    102   // Show the bubble, focus on its content, and register listeners.
    103   void ShowBubble();
    104 
    105   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
    106 
    107   // The contained host for the view.
    108   scoped_ptr<extensions::ExtensionViewHost> host_;
    109 
    110   // Flag used to indicate if the pop-up should open a devtools window once
    111   // it is shown inspecting it.
    112   bool inspect_with_devtools_;
    113 
    114   content::NotificationRegistrar registrar_;
    115 
    116   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
    117 
    118   bool widget_initialized_;
    119 
    120   DISALLOW_COPY_AND_ASSIGN(ExtensionPopup);
    121 };
    122 
    123 #endif  // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_POPUP_H_
    124