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