Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / gtk / location_bar_view_gtk.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_GTK_LOCATION_BAR_VIEW_GTK_H_
6 #define CHROME_BROWSER_UI_GTK_LOCATION_BAR_VIEW_GTK_H_
7
8 #include <gtk/gtk.h>
9
10 #include <map>
11 #include <string>
12
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/compiler_specific.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/prefs/pref_member.h"
20 #include "chrome/browser/extensions/extension_action.h"
21 #include "chrome/browser/extensions/extension_action_icon_factory.h"
22 #include "chrome/browser/extensions/extension_context_menu_model.h"
23 #include "chrome/browser/ui/gtk/bubble/bubble_gtk.h"
24 #include "chrome/browser/ui/gtk/menu_gtk.h"
25 #include "chrome/browser/ui/omnibox/location_bar.h"
26 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
27 #include "chrome/browser/ui/view_ids.h"
28 #include "chrome/common/content_settings_types.h"
29 #include "content/public/browser/notification_observer.h"
30 #include "content/public/browser/notification_registrar.h"
31 #include "content/public/common/page_transition_types.h"
32 #include "ui/base/gtk/gtk_signal.h"
33 #include "ui/base/gtk/owned_widget_gtk.h"
34 #include "ui/base/window_open_disposition.h"
35 #include "ui/gfx/animation/animation_delegate.h"
36 #include "ui/gfx/animation/slide_animation.h"
37 #include "url/gurl.h"
38
39 class Browser;
40 class CommandUpdater;
41 class ContentSettingImageModel;
42 class ContentSettingBubbleGtk;
43 class ExtensionAction;
44 class GtkThemeService;
45 class OmniboxViewGtk;
46
47 namespace content {
48 class WebContents;
49 }
50
51 namespace gfx {
52 class Image;
53 }
54
55 namespace ui {
56 class Accelerator;
57 }
58
59 class LocationBarViewGtk : public OmniboxEditController,
60                            public LocationBar,
61                            public LocationBarTesting,
62                            public content::NotificationObserver {
63  public:
64   explicit LocationBarViewGtk(Browser* browser);
65   virtual ~LocationBarViewGtk();
66
67   void Init(bool popup_window_mode);
68
69   // Returns the widget the caller should host.  You must call Init() first.
70   GtkWidget* widget() { return hbox_.get(); }
71
72   // Returns the widget the page info bubble should point to.
73   GtkWidget* location_icon_widget() const { return location_icon_image_; }
74
75   // Returns the widget the extension installed bubble should point to.
76   GtkWidget* location_entry_widget() const { return entry_box_; }
77
78   Browser* browser() const { return browser_; }
79
80   // Sets |preview_enabled| for the PageActionViewGtk associated with this
81   // |page_action|. If |preview_enabled| is true, the view will display the
82   // page action's icon even though it has not been activated by the extension.
83   // This is used by the ExtensionInstalledBubbleGtk to preview what the icon
84   // will look like for the user upon installation of the extension.
85   void SetPreviewEnabledPageAction(ExtensionAction *page_action,
86                                    bool preview_enabled);
87
88   // Retrieves the GtkWidget which is associated with PageActionView
89   // corresponding to |page_action|.
90   GtkWidget* GetPageActionWidget(ExtensionAction* page_action);
91
92   // Show the bookmark bubble.
93   void ShowStarBubble(const GURL& url, bool newly_boomkarked);
94
95   // Happens when the zoom changes for the active tab. |can_show_bubble| will be
96   // true if it was a user action and a bubble could be shown.
97   void ZoomChangedForActiveTab(bool can_show_bubble);
98
99   // Returns the zoom widget. Used by the zoom bubble for an anchor.
100   GtkWidget* zoom_widget() { return zoom_.get(); }
101
102   // Returns the manage passwords widget. Used by the manage passwords bubble
103   // for an anchor.
104   GtkWidget* manage_passwords_icon_widget() {
105     return manage_passwords_icon_.get();
106   }
107
108   // Set the starred state of the bookmark star.
109   void SetStarred(bool starred);
110
111   // OmniboxEditController:
112   virtual void Update(const content::WebContents* contents) OVERRIDE;
113   virtual void OnChanged() OVERRIDE;
114   virtual void OnSetFocus() OVERRIDE;
115   virtual InstantController* GetInstant() OVERRIDE;
116   virtual content::WebContents* GetWebContents() OVERRIDE;
117   virtual ToolbarModel* GetToolbarModel() OVERRIDE;
118   virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
119
120   // LocationBar:
121   virtual void ShowFirstRunBubble() OVERRIDE;
122   virtual GURL GetDestinationURL() const OVERRIDE;
123   virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
124   virtual content::PageTransition GetPageTransition() const OVERRIDE;
125   virtual void AcceptInput() OVERRIDE;
126   virtual void FocusLocation(bool select_all) OVERRIDE;
127   virtual void FocusSearch() OVERRIDE;
128   virtual void UpdateContentSettingsIcons() OVERRIDE;
129   virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
130   virtual void UpdatePageActions() OVERRIDE;
131   virtual void InvalidatePageActions() OVERRIDE;
132   virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
133   virtual void UpdateGeneratedCreditCardView() OVERRIDE;
134   virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
135   virtual void Revert() OVERRIDE;
136   virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
137   virtual OmniboxView* GetOmniboxView() OVERRIDE;
138   virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
139
140   // LocationBarTesting:
141   virtual int PageActionCount() OVERRIDE;
142   virtual int PageActionVisibleCount() OVERRIDE;
143   virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
144   virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
145   virtual void TestPageActionPressed(size_t index) OVERRIDE;
146   virtual bool GetBookmarkStarVisibility() OVERRIDE;
147
148   // content::NotificationObserver:
149   virtual void Observe(int type,
150                        const content::NotificationSource& source,
151                        const content::NotificationDetails& details) OVERRIDE;
152
153   // Edit background color.
154   static const GdkColor kBackgroundColor;
155
156   // Superclass for content settings icons shown at the left side of the
157   // location bar.
158   class PageToolViewGtk : public gfx::AnimationDelegate {
159    public:
160     PageToolViewGtk();
161     virtual ~PageToolViewGtk();
162
163     GtkWidget* widget() { return alignment_.get(); }
164
165     bool IsVisible();
166
167     virtual void Update(content::WebContents* web_contents) = 0;
168
169     // Overridden from gfx::AnimationDelegate:
170     virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
171     virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
172     virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE;
173
174    protected:
175     // Theme constants for solid background elements.
176     virtual GdkColor GetButtonBorderColor() const = 0;
177     virtual GdkColor GetGradientTopColor() const = 0;
178     virtual GdkColor GetGradientBottomColor() const = 0;
179
180     // Delegate for ButtonPressed message.
181     virtual void OnClick(GtkWidget* sender) = 0;
182
183     // Start the process of showing the label.
184     void StartAnimating();
185
186     // Slide the label shut.
187     void CloseAnimation();
188
189     CHROMEGTK_CALLBACK_1(PageToolViewGtk, gboolean, OnButtonPressed, GdkEvent*);
190     CHROMEGTK_CALLBACK_1(PageToolViewGtk, gboolean, OnExpose, GdkEventExpose*);
191
192     // The widgets for this view.
193     ui::OwnedWidgetGtk alignment_;
194     ui::OwnedWidgetGtk event_box_;
195     GtkWidget* hbox_;
196     ui::OwnedWidgetGtk image_;
197
198     // Explanatory text (e.g. "popup blocked").
199     ui::OwnedWidgetGtk label_;
200
201     // When we show explanatory text, we slide it in/out.
202     gfx::SlideAnimation animation_;
203
204     // The label's default requisition (cached so we can animate accordingly).
205     GtkRequisition label_req_;
206
207     base::WeakPtrFactory<PageToolViewGtk> weak_factory_;
208
209    private:
210     DISALLOW_COPY_AND_ASSIGN(PageToolViewGtk);
211   };
212
213  private:
214   class PageActionViewGtk :
215        public ExtensionActionIconFactory::Observer,
216        public content::NotificationObserver,
217        public ExtensionContextMenuModel::PopupDelegate {
218    public:
219     PageActionViewGtk(LocationBarViewGtk* owner, ExtensionAction* page_action);
220     virtual ~PageActionViewGtk();
221
222     GtkWidget* widget() { return event_box_.get(); }
223
224     ExtensionAction* page_action() { return page_action_; }
225
226     void set_preview_enabled(bool preview_enabled) {
227       preview_enabled_ = preview_enabled;
228     }
229
230     bool IsVisible();
231
232     // Called to notify the PageAction that it should determine whether to be
233     // visible or hidden. |contents| is the WebContents that is active, |url|
234     // is the current page URL.
235     void UpdateVisibility(content::WebContents* contents, const GURL& url);
236
237     // Overriden from ExtensionActionIconFactory::Observer.
238     virtual void OnIconUpdated() OVERRIDE;
239
240     // Simulate left mouse click on the page action button.
241     void TestActivatePageAction();
242
243     // Implement the content::NotificationObserver interface.
244     virtual void Observe(int type,
245                          const content::NotificationSource& source,
246                          const content::NotificationDetails& details) OVERRIDE;
247
248     // Overridden from ExtensionContextMenuModel::PopupDelegate:
249     virtual void InspectPopup(ExtensionAction* action) OVERRIDE;
250
251    private:
252     // Connect the accelerator for the page action popup.
253     void ConnectPageActionAccelerator();
254
255     // Disconnect the accelerator for the page action popup.
256     void DisconnectPageActionAccelerator();
257
258     CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnButtonPressed,
259                          GdkEventButton*);
260     CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnExposeEvent,
261                          GdkEventExpose*);
262     CHROMEGTK_CALLBACK_0(PageActionViewGtk, void, OnRealize);
263
264     // The accelerator handler for when the shortcuts to open the popup is
265     // struck.
266     static gboolean OnGtkAccelerator(GtkAccelGroup* accel_group,
267                                      GObject* acceleratable,
268                                      guint keyval,
269                                      GdkModifierType modifier,
270                                      void* user_data);
271
272     // The location bar view that owns us.
273     LocationBarViewGtk* owner_;
274
275     // The PageAction that this view represents. The PageAction is not owned by
276     // us, it resides in the extension of this particular profile.
277     ExtensionAction* page_action_;
278
279     // The object that will be used to get the extension action icon for us.
280     // It may load the icon asynchronously (in which case the initial icon
281     // returned by the factory will be transparent), so we have to observe it
282     // for updates to the icon.
283     scoped_ptr<ExtensionActionIconFactory> icon_factory_;
284
285     // The widgets for this page action.
286     ui::OwnedWidgetGtk event_box_;
287     ui::OwnedWidgetGtk image_;
288
289     // The tab id we are currently showing the icon for.
290     int current_tab_id_;
291
292     // The URL we are currently showing the icon for.
293     GURL current_url_;
294
295     // The native browser window of the location bar that owns us.
296     gfx::NativeWindow window_;
297
298     // The Notification registrar.
299     content::NotificationRegistrar registrar_;
300
301     // The accelerator group used to handle accelerators, owned by this object.
302     GtkAccelGroup* accel_group_;
303
304     // The keybinding accelerator registered to show the page action popup.
305     scoped_ptr<ui::Accelerator> page_action_keybinding_;
306
307     // This is used for post-install visual feedback. The page_action icon
308     // is briefly shown even if it hasn't been enabled by its extension.
309     bool preview_enabled_;
310
311     // The context menu view and model for this extension action.
312     scoped_ptr<MenuGtk> context_menu_;
313     scoped_refptr<ExtensionContextMenuModel> context_menu_model_;
314
315     DISALLOW_COPY_AND_ASSIGN(PageActionViewGtk);
316   };
317   friend class PageActionViewGtk;
318
319   // Creates, initializes, and packs the location icon, EV certificate name,
320   // and optional border.
321   void BuildSiteTypeArea();
322
323   // Enable or disable the location icon/EV certificate as a drag source for
324   // the URL.
325   void SetSiteTypeDragSource();
326
327   GtkWidget* site_type_area() { return site_type_alignment_; }
328
329   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, HandleExpose,
330                        GdkEventExpose*);
331   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, OnIconReleased,
332                        GdkEventButton*);
333   CHROMEGTK_CALLBACK_4(LocationBarViewGtk, void, OnIconDragData,
334                        GdkDragContext*, GtkSelectionData*, guint, guint);
335   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnIconDragBegin,
336                        GdkDragContext*);
337   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnIconDragEnd,
338                        GdkDragContext*);
339   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnHboxSizeAllocate,
340                        GtkAllocation*);
341   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnEntryBoxSizeAllocate,
342                        GtkAllocation*);
343   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, OnZoomButtonPress,
344                        GdkEventButton*);
345   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean,
346                        OnManagePasswordsIconButtonPress, GdkEventButton*);
347   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, void, OnStarButtonSizeAllocate,
348                        GtkAllocation*);
349   CHROMEGTK_CALLBACK_1(LocationBarViewGtk, gboolean, OnStarButtonPress,
350                        GdkEventButton*);
351
352   // Updates the site type area: changes the icon and shows/hides the EV
353   // certificate information.
354   void UpdateSiteTypeArea();
355
356   // Updates the maximum size of the EV certificate label.
357   void UpdateEVCertificateLabelSize();
358
359   // Set the keyword text for the Search BLAH: keyword box.
360   void SetKeywordLabel(const base::string16& keyword);
361
362   // Set the keyword text for the "Press tab to search BLAH" hint box.
363   void SetKeywordHintLabel(const base::string16& keyword);
364
365   void ShowFirstRunBubbleInternal();
366
367   // Shows the zoom bubble.
368   void ShowZoomBubble();
369
370   // Shows the manage password bubble.
371   void ShowManagePasswordsBubble();
372
373   // Show or hide |tab_to_search_box_| and |tab_to_search_hint_| according to
374   // the value of |show_selected_keyword_|, |show_keyword_hint_|, and the
375   // available horizontal space in the location bar.
376   void AdjustChildrenVisibility();
377
378   // Helpers to build create the various buttons that show up in the location
379   // bar.
380   GtkWidget* CreateIconButton(
381       GtkWidget** image,
382       int image_id,
383       ViewID debug_id,
384       int tooltip_id,
385       gboolean (click_callback)(GtkWidget*, GdkEventButton*, gpointer));
386   void CreateZoomButton();
387   void CreateManagePasswordsIconButton();
388   void CreateStarButton();
389
390   // Helpers to update state of the various buttons that show up in the
391   // location bar.
392   void UpdateZoomIcon();
393   void UpdateManagePasswordsIcon();
394   void UpdateStarIcon();
395
396   // Shows the managepassword bubble in case there is a password to be saved.
397   void ShowManagePasswordsBubbleIfNeeded();
398
399   // Returns true if we should only show the URL and none of the extras like
400   // the star button or page actions.
401   bool ShouldOnlyShowLocation();
402
403   // The outermost widget we want to be hosted.
404   ui::OwnedWidgetGtk hbox_;
405
406   // Zoom button.
407   ui::OwnedWidgetGtk zoom_;
408   GtkWidget* zoom_image_;
409
410   // Manage passwords button.
411   ui::OwnedWidgetGtk manage_passwords_icon_;
412   GtkWidget* manage_passwords_icon_image_;
413
414   // Star button.
415   ui::OwnedWidgetGtk star_;
416   GtkWidget* star_image_;
417   bool starred_;
418   bool star_sized_;  // True after a size-allocate signal to the star widget.
419
420   // Action to execute after the star icon has been sized, can refer to a NULL
421   // function to indicate no such action should be taken.
422   base::Closure on_star_sized_;
423
424   // An icon to the left of the address bar.
425   GtkWidget* site_type_alignment_;
426   GtkWidget* site_type_event_box_;
427   GtkWidget* location_icon_image_;
428   GtkWidget* drag_icon_;
429   bool enable_location_drag_;
430   // TODO(pkasting): Split this label off and move the rest of the items to the
431   // left of the address bar.
432   GtkWidget* security_info_label_;
433
434   // Content setting icons.
435   ui::OwnedWidgetGtk content_setting_hbox_;
436   ScopedVector<PageToolViewGtk> content_setting_views_;
437
438   // Extension page actions.
439   std::vector<ExtensionAction*> page_actions_;
440
441   // Extension page action icons.
442   ui::OwnedWidgetGtk page_action_hbox_;
443   ScopedVector<PageActionViewGtk> page_action_views_;
444
445   // The widget that contains our tab hints and the location bar.
446   GtkWidget* entry_box_;
447
448   // Area on the left shown when in tab to search mode.
449   GtkWidget* tab_to_search_alignment_;
450   GtkWidget* tab_to_search_box_;
451   GtkWidget* tab_to_search_magnifier_;
452   GtkWidget* tab_to_search_full_label_;
453   GtkWidget* tab_to_search_partial_label_;
454
455   // Hint to user that they can tab-to-search by hitting tab.
456   GtkWidget* tab_to_search_hint_;
457   GtkWidget* tab_to_search_hint_leading_label_;
458   GtkWidget* tab_to_search_hint_icon_;
459   GtkWidget* tab_to_search_hint_trailing_label_;
460
461   scoped_ptr<OmniboxViewGtk> omnibox_view_;
462
463   // Alignment used to wrap |omnibox_view_|.
464   GtkWidget* omnibox_view_alignment_;
465
466   Browser* browser_;
467
468   // When true, the location bar view is read only and also is has a slightly
469   // different presentation (font size / color). This is used for popups.
470   bool popup_window_mode_;
471
472   // Provides colors and rendering mode.
473   GtkThemeService* theme_service_;
474
475   content::NotificationRegistrar registrar_;
476
477   // Width of the main |hbox_|. Used to properly elide the EV certificate.
478   int hbox_width_;
479
480   // Width of the hbox that holds |tab_to_search_box_|, |omnibox_view_| and
481   // |tab_to_search_hint_|.
482   int entry_box_width_;
483
484   // Indicate if |tab_to_search_box_| should be shown.
485   bool show_selected_keyword_;
486
487   // Indicate if |tab_to_search_hint_| should be shown.
488   bool show_keyword_hint_;
489
490   // The last search keyword that was shown via the |tab_to_search_box_|.
491   base::string16 last_keyword_;
492
493   // Used to change the visibility of the star decoration.
494   BooleanPrefMember edit_bookmarks_enabled_;
495
496   // Used to remember the URL and title text when drag&drop has begun.
497   GURL drag_url_;
498   base::string16 drag_title_;
499
500   // Used to schedule a task for the first run bubble.
501   base::WeakPtrFactory<LocationBarViewGtk> weak_ptr_factory_;
502
503   DISALLOW_COPY_AND_ASSIGN(LocationBarViewGtk);
504 };
505
506 #endif  // CHROME_BROWSER_UI_GTK_LOCATION_BAR_VIEW_GTK_H_