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_GTK_EXTENSIONS_EXTENSION_POPUP_GTK_H_
      6 #define CHROME_BROWSER_UI_GTK_EXTENSIONS_EXTENSION_POPUP_GTK_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "chrome/browser/ui/gtk/bubble/bubble_gtk.h"
     13 #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "ui/gfx/rect.h"
     17 
     18 class Browser;
     19 class GURL;
     20 
     21 namespace extensions {
     22 class ExtensionHost;
     23 }
     24 
     25 namespace content {
     26 class DevToolsAgentHost;
     27 }
     28 
     29 class ExtensionPopupGtk : public content::NotificationObserver,
     30                           public BubbleDelegateGtk,
     31                           public ExtensionViewGtk::Container {
     32  public:
     33   enum ShowAction {
     34     SHOW,
     35     SHOW_AND_INSPECT
     36   };
     37 
     38   static void Show(const GURL& url,
     39                    Browser* browser,
     40                    GtkWidget* anchor,
     41                    ShowAction show_action);
     42 
     43   // content::NotificationObserver implementation.
     44   virtual void Observe(int type,
     45                        const content::NotificationSource& source,
     46                        const content::NotificationDetails& details) OVERRIDE;
     47 
     48   // BubbleDelegateGtk implementation.
     49   virtual void BubbleClosing(BubbleGtk* bubble,
     50                              bool closed_by_escape) OVERRIDE;
     51 
     52   // ExtensionViewGtk::Container implementation.
     53   virtual void OnExtensionSizeChanged(
     54       ExtensionViewGtk* view,
     55       const gfx::Size& new_size) OVERRIDE;
     56 
     57   // Destroys the popup widget. This will in turn destroy us since we delete
     58   // ourselves when the bubble closes. Returns true if we successfully
     59   // closed the bubble.
     60   bool DestroyPopup();
     61 
     62   // Get the currently showing extension popup, or NULL.
     63   static ExtensionPopupGtk* get_current_extension_popup() {
     64     return current_extension_popup_;
     65   }
     66 
     67   bool being_inspected() const {
     68     return being_inspected_;
     69   }
     70 
     71   // Declared here for testing.
     72   static const int kMinWidth;
     73   static const int kMinHeight;
     74   static const int kMaxWidth;
     75   static const int kMaxHeight;
     76 
     77  private:
     78   ExtensionPopupGtk(Browser* browser,
     79                     extensions::ExtensionHost* host,
     80                     GtkWidget* anchor,
     81                     ShowAction show_action);
     82   virtual ~ExtensionPopupGtk();
     83 
     84   // Shows the popup widget. Called after loading completes.
     85   void ShowPopup();
     86 
     87   // See DestroyPopup. Does not return success or failure. Necessitated by
     88   // base::Bind and friends, which cannot handle a WeakPtr for a function that
     89   // has a return value.
     90   void DestroyPopupWithoutResult();
     91 
     92   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
     93 
     94   Browser* browser_;
     95 
     96   BubbleGtk* bubble_;
     97 
     98   // We take ownership of the popup ExtensionHost.
     99   scoped_ptr<extensions::ExtensionHost> host_;
    100 
    101   // The widget for anchoring the position of the bubble.
    102   GtkWidget* anchor_;
    103 
    104   content::NotificationRegistrar registrar_;
    105 
    106   static ExtensionPopupGtk* current_extension_popup_;
    107 
    108   // Whether a devtools window is attached to this bubble.
    109   bool being_inspected_;
    110 
    111   base::WeakPtrFactory<ExtensionPopupGtk> weak_factory_;
    112 
    113   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
    114 
    115   // Used for testing. ---------------------------------------------------------
    116   gfx::Rect GetViewBounds();
    117 
    118   friend class BrowserActionTestUtil;
    119 
    120   DISALLOW_COPY_AND_ASSIGN(ExtensionPopupGtk);
    121 };
    122 
    123 #endif  // CHROME_BROWSER_UI_GTK_EXTENSIONS_EXTENSION_POPUP_GTK_H_
    124