extension_dialog.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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_DIALOG_H_
6#define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_DIALOG_H_
7
8#include "base/logging.h"
9#include "base/memory/ref_counted.h"
10#include "content/public/browser/notification_observer.h"
11#include "content/public/browser/notification_registrar.h"
12#include "ui/views/window/dialog_delegate.h"
13
14class BaseWindow;
15class ExtensionDialogObserver;
16class GURL;
17class Profile;
18
19namespace content {
20class WebContents;
21}
22
23namespace extensions {
24class ExtensionHost;
25}
26
27// Modal dialog containing contents provided by an extension.
28// Dialog is automatically centered in the owning window and has fixed size.
29// For example, used by the Chrome OS file browser.
30class ExtensionDialog : public views::DialogDelegate,
31                        public content::NotificationObserver,
32                        public base::RefCounted<ExtensionDialog> {
33 public:
34  // Create and show a dialog with |url| centered over the provided window.
35  // |base_window| is the window to which the pop-up will be attached.
36  // |profile| is the profile that the extension is registered with.
37  // |web_contents| is the tab that spawned the dialog.
38  // |width| and |height| are the size of the dialog in pixels.
39  static ExtensionDialog* Show(const GURL& url,
40                               BaseWindow* base_window,
41                               Profile* profile,
42                               content::WebContents* web_contents,
43                               int width,
44                               int height,
45                               int min_width,
46                               int min_height,
47                               const string16& title,
48                               ExtensionDialogObserver* observer);
49
50  // Notifies the dialog that the observer has been destroyed and should not
51  // be sent notifications.
52  void ObserverDestroyed();
53
54  // Closes the ExtensionDialog.
55  void Close();
56
57  // Focus to the render view if possible.
58  void MaybeFocusRenderView();
59
60  // Sets the window title.
61  void set_title(const string16& title) { window_title_ = title; }
62
63  // Sets minimum contents size in pixels and makes the window resizable.
64  void SetMinimumContentsSize(int width, int height);
65
66  extensions::ExtensionHost* host() const { return extension_host_.get(); }
67
68  // views::DialogDelegate override.
69  virtual int GetDialogButtons() const OVERRIDE;
70  virtual bool CanResize() const OVERRIDE;
71  virtual ui::ModalType GetModalType() const OVERRIDE;
72  virtual bool ShouldShowWindowTitle() const OVERRIDE;
73  virtual string16 GetWindowTitle() const OVERRIDE;
74  virtual void WindowClosing() OVERRIDE;
75  virtual void DeleteDelegate() OVERRIDE;
76  virtual views::Widget* GetWidget() OVERRIDE;
77  virtual const views::Widget* GetWidget() const OVERRIDE;
78  virtual views::View* GetContentsView() OVERRIDE;
79
80  // content::NotificationObserver overrides.
81  virtual void Observe(int type,
82                       const content::NotificationSource& source,
83                       const content::NotificationDetails& details) OVERRIDE;
84
85 protected:
86  virtual ~ExtensionDialog();
87
88 private:
89  friend class base::RefCounted<ExtensionDialog>;
90
91  // Use Show() to create instances.
92  ExtensionDialog(extensions::ExtensionHost* host,
93                  ExtensionDialogObserver* observer);
94
95  static ExtensionDialog* ShowInternal(const GURL& url,
96                                       BaseWindow* base_window,
97                                       extensions::ExtensionHost* host,
98                                       int width,
99                                       int height,
100                                       const string16& title,
101                                       ExtensionDialogObserver* observer);
102
103  static extensions::ExtensionHost* CreateExtensionHost(const GURL& url,
104                                                        Profile* profile);
105
106  void InitWindow(BaseWindow* base_window, int width, int height);
107
108  // Window that holds the extension host view.
109  views::Widget* window_;
110
111  // Window Title
112  string16 window_title_;
113
114  // The contained host for the view.
115  scoped_ptr<extensions::ExtensionHost> extension_host_;
116
117  content::NotificationRegistrar registrar_;
118
119  // The observer of this popup.
120  ExtensionDialogObserver* observer_;
121
122  DISALLOW_COPY_AND_ASSIGN(ExtensionDialog);
123};
124
125#endif  // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_DIALOG_H_
126