Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / panels / panel.h
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_PANELS_PANEL_H_
6 #define CHROME_BROWSER_UI_PANELS_PANEL_H_
7
8 #include <string>
9
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/command_updater.h"
15 #include "chrome/browser/command_updater_delegate.h"
16 #include "chrome/browser/ui/panels/panel_constants.h"
17 #include "components/sessions/session_id.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "ui/base/base_window.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/rect.h"
24
25 class GURL;
26 class NativePanel;
27 class PanelCollection;
28 class PanelHost;
29 class PanelManager;
30 class Profile;
31 class StackedPanelCollection;
32
33 namespace content {
34 class WebContents;
35 struct NativeWebKeyboardEvent;
36 }
37
38 namespace extensions {
39 class Extension;
40 class ExtensionRegistry;
41 class WindowController;
42 }
43
44 // A platform independent implementation of ui::BaseWindow for Panels.
45 // This class gets the first crack at all the ui::BaseWindow calls for Panels
46 // and does one or more of the following:
47 // - Do nothing.  The function is not relevant to Panels.
48 // - Do Panel specific platform independent processing and then invoke the
49 //   function on the platform specific member. For example, restrict panel
50 //   size to certain limits.
51 // - Invoke an appropriate PanelManager function to do stuff that might affect
52 //   other Panels. For example deleting a panel would rearrange other panels.
53 class Panel : public ui::BaseWindow,
54               public CommandUpdaterDelegate,
55               public content::NotificationObserver,
56               public extensions::ExtensionRegistryObserver {
57  public:
58   enum ExpansionState {
59     // The panel is fully expanded with both title-bar and the client-area.
60     EXPANDED,
61     // The panel is shown with the title-bar only.
62     TITLE_ONLY,
63     // The panel is shown with 3-pixel line.
64     MINIMIZED
65   };
66
67   // Controls how the attention should be drawn.
68   enum AttentionMode {
69     // Uses the panel attention. The panel's titlebar would be painted
70     // differently to attract the user's attention. This is the default mode.
71     USE_PANEL_ATTENTION = 0x01,
72     // Uses the system attention. On Windows or Linux (depending on Window
73     // Manager), the app icon on taskbar will be flashed. On MacOS, the dock
74     // icon will jump once.
75     USE_SYSTEM_ATTENTION = 0x02
76   };
77
78   ~Panel() override;
79
80   // Returns the PanelManager associated with this panel.
81   PanelManager* manager() const;
82
83   const std::string& app_name() const { return app_name_; }
84   const gfx::Image& app_icon() const { return app_icon_; }
85   const SessionID& session_id() const { return session_id_; }
86   extensions::WindowController* extension_window_controller() const {
87     return extension_window_controller_.get();
88   }
89   const std::string extension_id() const;
90
91   CommandUpdater* command_updater();
92   Profile* profile() const;
93
94   const extensions::Extension* GetExtension() const;
95
96   // Returns web contents of the panel, if any. There may be none if web
97   // contents have not been added to the panel yet.
98   content::WebContents* GetWebContents() const;
99
100   void SetExpansionState(ExpansionState new_expansion_state);
101
102   bool IsDrawingAttention() const;
103
104   // This function will only get called by PanelManager when full screen mode
105   // changes i.e it gets called when an app goes into full screen mode or when
106   // an app exits full screen mode. Panel should respond by making sure
107   // a) it does not go on top when some app enters full screen mode.
108   // b) it remains on top when an app exits full screen mode.
109   void FullScreenModeChanged(bool is_full_screen);
110
111   int TitleOnlyHeight() const;
112
113   // Returns true if the panel can show minimize or restore button in its
114   // titlebar, depending on its state.
115   bool CanShowMinimizeButton() const;
116   bool CanShowRestoreButton() const;
117
118   // ui::BaseWindow overrides.
119   bool IsActive() const override;
120   bool IsMaximized() const override;
121   bool IsMinimized() const override;
122   bool IsFullscreen() const override;
123   gfx::NativeWindow GetNativeWindow() const override;
124   gfx::Rect GetRestoredBounds() const override;
125   ui::WindowShowState GetRestoredState() const override;
126   gfx::Rect GetBounds() const override;
127   void Show() override;
128   void Hide() override;
129   void ShowInactive() override;
130   void Close() override;
131   void Activate() override;
132   void Deactivate() override;
133   void Maximize() override;
134   void Minimize() override;
135   void Restore() override;
136   void SetBounds(const gfx::Rect& bounds) override;
137   void FlashFrame(bool flash) override;
138   bool IsAlwaysOnTop() const override;
139   void SetAlwaysOnTop(bool on_top) override;
140
141   // Overridden from CommandUpdaterDelegate:
142   void ExecuteCommandWithDisposition(int id, WindowOpenDisposition disposition)
143       override;
144
145   // content::NotificationObserver overrides.
146   void Observe(int type,
147                const content::NotificationSource& source,
148                const content::NotificationDetails& details) override;
149
150   //  extensions::ExtensionRegistryObserver.
151   void OnExtensionUnloaded(
152       content::BrowserContext* browser_context,
153       const extensions::Extension* extension,
154       extensions::UnloadedExtensionInfo::Reason reason) override;
155
156   // Construct a native panel implementation.
157   static NativePanel* CreateNativePanel(Panel* panel,
158                                         const gfx::Rect& bounds,
159                                         bool always_on_top);
160
161   NativePanel* native_panel() const { return native_panel_; }
162
163   // Invoked when the native panel has detected a mouse click on the
164   // panel's titlebar, minimize or restore buttons. Behavior of the
165   // click may be modified as indicated by |modifier|.
166   void OnTitlebarClicked(panel::ClickModifier modifier);
167   void OnMinimizeButtonClicked(panel::ClickModifier modifier);
168   void OnRestoreButtonClicked(panel::ClickModifier modifier);
169
170   // Used on platforms where the panel cannot determine its window size
171   // until the window has been created. (e.g. GTK)
172   void OnWindowSizeAvailable();
173
174   // Asynchronous completion of panel close request.
175   void OnNativePanelClosed();
176
177   // May be NULL if:
178   // * panel is newly created and has not been positioned yet.
179   // * panel is being closed asynchronously.
180   // Please use it with caution.
181   PanelCollection* collection() const { return collection_; }
182
183   // Sets the current panel collection that contains this panel.
184   void set_collection(PanelCollection* new_collection) {
185     collection_ = new_collection;
186   }
187
188   StackedPanelCollection* stack() const;
189
190   ExpansionState expansion_state() const { return expansion_state_; }
191   const gfx::Size& min_size() const { return min_size_; }
192   const gfx::Size& max_size() const { return max_size_; }
193   bool auto_resizable() const { return auto_resizable_; }
194
195   bool in_preview_mode() const { return in_preview_mode_; }
196
197   panel::Resizability CanResizeByMouse() const;
198
199   AttentionMode attention_mode() const { return attention_mode_; }
200   void set_attention_mode(AttentionMode attention_mode) {
201     attention_mode_ = attention_mode;
202   }
203
204   // The full size is the size of the panel when it is detached or expanded
205   // in the docked collection and squeezing mode is not on.
206   gfx::Size full_size() const { return full_size_; }
207   void set_full_size(const gfx::Size& size) { full_size_ = size; }
208
209   // Panel must be initialized to be "fully created" and ready for use.
210   // Only called by PanelManager.
211   bool initialized() const { return initialized_; }
212   void Initialize(const GURL& url, const gfx::Rect& bounds, bool always_on_top);
213
214   // This is different from BaseWindow::SetBounds():
215   // * SetPanelBounds() is only called by PanelManager to manage its position.
216   // * SetBounds() is called by the API to try to change the bounds, which may
217   //   only change the size for Panel.
218   void SetPanelBounds(const gfx::Rect& bounds);
219
220   // Updates the panel bounds instantly without any animation.
221   void SetPanelBoundsInstantly(const gfx::Rect& bounds);
222
223   // Ensures that the panel's size does not exceed the work area by updating
224   // maximum and full size of the panel. This is called each time when display
225   // settings are changed. Note that bounds are not updated here and the call
226   // of setting bounds or refreshing layout should be called after this.
227   void LimitSizeToWorkArea(const gfx::Rect& work_area);
228
229   // Sets whether the panel will auto resize according to its content.
230   void SetAutoResizable(bool resizable);
231
232   // Configures the web contents for auto resize, including configurations
233   // on the renderer and detecting renderer changes.
234   void EnableWebContentsAutoResize(content::WebContents* web_contents);
235
236   // Invoked when the preferred window size of the given panel might need to
237   // get changed due to the contents being auto-resized.
238   void OnContentsAutoResized(const gfx::Size& new_content_size);
239
240   // Resizes the panel and sets the origin. Invoked when the panel is resized
241   // via the mouse.
242   void OnWindowResizedByMouse(const gfx::Rect& new_bounds);
243
244   // Sets minimum and maximum size for the panel.
245   void SetSizeRange(const gfx::Size& min_size, const gfx::Size& max_size);
246
247   // Updates the maximum size of the panel so that it's never smaller than the
248   // panel's desired size. Note that even if the user resizes the panel smaller
249   // later, the increased maximum size will still be in effect. Since it's not
250   // possible currently to switch the panel back to autosizing from
251   // user-resizable, it should not be a problem.
252   void IncreaseMaxSize(const gfx::Size& desired_panel_size);
253
254   // Handles keyboard events coming back from the renderer.
255   void HandleKeyboardEvent(const content::NativeWebKeyboardEvent& event);
256
257   // Sets whether the panel is shown in preview mode. When the panel is
258   // being dragged, it is in preview mode.
259   void SetPreviewMode(bool in_preview_mode);
260
261   // Sets whether the minimize or restore button, if any, are visible.
262   void UpdateMinimizeRestoreButtonVisibility();
263
264   // Changes the preferred size to acceptable based on min_size() and max_size()
265   gfx::Size ClampSize(const gfx::Size& size) const;
266
267   // Called when the panel's active state changes.
268   // |active| is true if panel became active.
269   void OnActiveStateChanged(bool active);
270
271   // Called when the panel starts/ends the user resizing.
272   void OnPanelStartUserResizing();
273   void OnPanelEndUserResizing();
274
275   // Gives beforeunload handlers the chance to cancel the close.
276   bool ShouldCloseWindow();
277
278   // Invoked when the window containing us is closing. Performs the necessary
279   // cleanup.
280   void OnWindowClosing();
281
282   // Executes a command if it's enabled.
283   // Returns true if the command is executed.
284   bool ExecuteCommandIfEnabled(int id);
285
286   // Gets the title of the window from the web contents.
287   base::string16 GetWindowTitle() const;
288
289   // Gets the Favicon of the web contents.
290   gfx::Image GetCurrentPageIcon() const;
291
292   // Updates the title bar to display the current title and icon.
293   void UpdateTitleBar();
294
295   // Updates UI to reflect change in loading state.
296   void LoadingStateChanged(bool is_loading);
297
298   // Updates UI to reflect that the web cotents receives the focus.
299   void WebContentsFocused(content::WebContents* contents);
300
301   // Moves the panel by delta instantly.
302   void MoveByInstantly(const gfx::Vector2d& delta_origin);
303
304   // Applies |corner_style| to the panel window.
305   void SetWindowCornerStyle(panel::CornerStyle corner_style);
306
307   // Performs the system minimize for the panel, i.e. becoming iconic.
308   void MinimizeBySystem();
309
310   bool IsMinimizedBySystem() const;
311
312   // Returns true if the panel is shown in the active desktop. The user could
313   // create or use multiple desktops or workspaces.
314   bool IsShownOnActiveDesktop() const;
315
316   // Turns on/off the shadow effect around the window shape.
317   void ShowShadow(bool show);
318
319  protected:
320   // Panel can only be created using PanelManager::CreatePanel() or subclass.
321   // |app_name| is the default title for Panels when the page content does not
322   // provide a title. For extensions, this is usually the application name
323   // generated from the extension id.
324   Panel(Profile* profile, const std::string& app_name,
325         const gfx::Size& min_size, const gfx::Size& max_size);
326
327  private:
328   friend class PanelManager;
329   friend class PanelBrowserTest;
330
331   enum MaxSizePolicy {
332     // Default maximum size is proportional to the work area.
333     DEFAULT_MAX_SIZE,
334     // Custom maximum size is used when the panel is resized by the user.
335     CUSTOM_MAX_SIZE
336   };
337
338   void OnImageLoaded(const gfx::Image& image);
339
340   // Initialize state for all supported commands.
341   void InitCommandState();
342
343   // Configures the renderer for auto resize (if auto resize is enabled).
344   void ConfigureAutoResize(content::WebContents* web_contents);
345
346   // Load the app's image, firing a load state change when loaded.
347   void UpdateAppIcon();
348
349   // Prepares a title string for display (removes embedded newlines, etc).
350   static void FormatTitleForDisplay(base::string16* title);
351
352   // The application name that is also the name of the window when the
353   // page content does not provide a title.
354   // This name should be set when the panel is created.
355   const std::string app_name_;
356
357   Profile* profile_;
358
359   // Current collection of panels to which this panel belongs. This determines
360   // the panel's screen layout.
361   PanelCollection* collection_;  // Owned by PanelManager.
362
363   bool initialized_;
364
365   // Stores the full size of the panel so we can restore it after it's
366   // been minimized or squeezed due to lack of space in the collection.
367   gfx::Size full_size_;
368
369   // This is the minimum size that the panel can shrink to.
370   gfx::Size min_size_;
371
372   // This is the size beyond which the panel is not going to grow to accomodate
373   // the growing content and WebKit would add the scrollbars in such case.
374   gfx::Size max_size_;
375
376   MaxSizePolicy max_size_policy_;
377
378   // True if this panel auto resizes based on content.
379   bool auto_resizable_;
380
381   // True if this panel is in preview mode. When in preview mode, panel bounds
382   // should not be affected by layout refresh. This is currently used by drag
383   // controller to add a panel to the collection without causing its bounds to
384   // change.
385   bool in_preview_mode_;
386
387   // Platform specifc implementation for panels.  It'd be one of
388   // PanelGtk/PanelView/PanelCocoa.
389   NativePanel* native_panel_;  // Weak, owns us.
390
391   AttentionMode attention_mode_;
392
393   ExpansionState expansion_state_;
394
395   // The CommandUpdater manages the window commands.
396   CommandUpdater command_updater_;
397
398   content::NotificationRegistrar registrar_;
399   extensions::ExtensionRegistry* extension_registry_;
400   const SessionID session_id_;
401   scoped_ptr<extensions::WindowController> extension_window_controller_;
402   scoped_ptr<PanelHost> panel_host_;
403
404   // Icon showed in the task bar.
405   gfx::Image app_icon_;
406
407   base::WeakPtrFactory<Panel> image_loader_ptr_factory_;
408
409   DISALLOW_COPY_AND_ASSIGN(Panel);
410 };
411
412 #endif  // CHROME_BROWSER_UI_PANELS_PANEL_H_