- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / location_bar / location_bar_view.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_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/prefs/pref_member.h"
14 #include "chrome/browser/extensions/extension_context_menu_model.h"
15 #include "chrome/browser/search_engines/template_url_service_observer.h"
16 #include "chrome/browser/ui/omnibox/location_bar.h"
17 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
18 #include "chrome/browser/ui/search/search_model_observer.h"
19 #include "chrome/browser/ui/toolbar/toolbar_model.h"
20 #include "chrome/browser/ui/views/dropdown_bar_host.h"
21 #include "chrome/browser/ui/views/dropdown_bar_host_delegate.h"
22 #include "chrome/browser/ui/views/extensions/extension_popup.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "ui/gfx/font.h"
26 #include "ui/gfx/rect.h"
27 #include "ui/views/controls/button/button.h"
28 #include "ui/views/controls/native/native_view_host.h"
29 #include "ui/views/drag_controller.h"
30
31 #if defined(USE_AURA)
32 #include "ui/compositor/layer_animation_observer.h"
33 #endif
34
35 class ActionBoxButtonView;
36 class CommandUpdater;
37 class ContentSettingBubbleModelDelegate;
38 class ContentSettingImageView;
39 class EVBubbleView;
40 class ExtensionAction;
41 class GURL;
42 class GeneratedCreditCardView;
43 class InstantController;
44 class KeywordHintView;
45 class LocationIconView;
46 class OpenPDFInReaderView;
47 class PageActionWithBadgeView;
48 class PageActionImageView;
49 class Profile;
50 class ScriptBubbleIconView;
51 class SelectedKeywordView;
52 class StarView;
53 class TemplateURLService;
54 class TranslateIconView;
55 class ZoomView;
56
57 namespace views {
58 class BubbleDelegateView;
59 class ImageButton;
60 class Label;
61 class Widget;
62 }
63
64 /////////////////////////////////////////////////////////////////////////////
65 //
66 // LocationBarView class
67 //
68 //   The LocationBarView class is a View subclass that paints the background
69 //   of the URL bar strip and contains its content.
70 //
71 /////////////////////////////////////////////////////////////////////////////
72 class LocationBarView : public LocationBar,
73                         public LocationBarTesting,
74                         public views::View,
75                         public views::ButtonListener,
76                         public views::DragController,
77                         public OmniboxEditController,
78                         public DropdownBarHostDelegate,
79                         public TemplateURLServiceObserver,
80                         public content::NotificationObserver,
81                         public SearchModelObserver {
82  public:
83   // The location bar view's class name.
84   static const char kViewClassName[];
85
86   // DropdownBarHostDelegate:
87   virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
88   virtual void SetAnimationOffset(int offset) OVERRIDE;
89
90   // Returns the offset used while animating.
91   int animation_offset() const { return animation_offset_; }
92
93   class Delegate {
94    public:
95     // Should return the current web contents.
96     virtual content::WebContents* GetWebContents() = 0;
97
98     // Returns the InstantController, or NULL if there isn't one.
99     virtual InstantController* GetInstant() = 0;
100
101     virtual ToolbarModel* GetToolbarModel() = 0;
102     virtual const ToolbarModel* GetToolbarModel() const = 0;
103
104     // Creates Widget for the given delegate.
105     virtual views::Widget* CreateViewsBubble(
106         views::BubbleDelegateView* bubble_delegate) = 0;
107
108     // Creates PageActionImageView. Caller gets an ownership.
109     virtual PageActionImageView* CreatePageActionImageView(
110         LocationBarView* owner,
111         ExtensionAction* action) = 0;
112
113     // Returns ContentSettingBubbleModelDelegate.
114     virtual ContentSettingBubbleModelDelegate*
115         GetContentSettingBubbleModelDelegate() = 0;
116
117     // Shows permissions and settings for the given web contents.
118     virtual void ShowWebsiteSettings(content::WebContents* web_contents,
119                                      const GURL& url,
120                                      const content::SSLStatus& ssl) = 0;
121
122    protected:
123     virtual ~Delegate() {}
124   };
125
126   enum ColorKind {
127     BACKGROUND = 0,
128     TEXT,
129     SELECTED_TEXT,
130     DEEMPHASIZED_TEXT,
131     SECURITY_TEXT,
132   };
133
134   LocationBarView(Browser* browser,
135                   Profile* profile,
136                   CommandUpdater* command_updater,
137                   Delegate* delegate,
138                   bool is_popup_mode);
139
140   virtual ~LocationBarView();
141
142   // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add
143   // additional padding (via an empty border) to |view|. This should be called
144   // during creation on all child views which are potentially touchable so that
145   // when touch is enabled they will have sufficient padding.
146   static void InitTouchableLocationBarChildView(views::View* view);
147
148   // Initializes the LocationBarView.
149   void Init();
150
151   // True if this instance has been initialized by calling Init, which can only
152   // be called when the receiving instance is attached to a view container.
153   bool IsInitialized() const;
154
155   // Returns the appropriate color for the desired kind, based on the user's
156   // system theme.
157   SkColor GetColor(ToolbarModel::SecurityLevel security_level,
158                    ColorKind kind) const;
159
160   // Returns corresponding profile.
161   Profile* profile() const { return profile_; }
162
163   // Returns the delegate.
164   Delegate* delegate() const { return delegate_; }
165
166   // See comment in browser_window.h for more info.
167   void ZoomChangedForActiveTab(bool can_show_bubble);
168
169   // The zoom icon view. It may not be visible.
170   ZoomView* zoom_view() { return zoom_view_; }
171
172   // Sets |preview_enabled| for the PageAction View associated with this
173   // |page_action|. If |preview_enabled| is true, the view will display the
174   // PageActions icon even though it has not been activated by the extension.
175   // This is used by the ExtensionInstalledBubble to preview what the icon
176   // will look like for the user upon installation of the extension.
177   void SetPreviewEnabledPageAction(ExtensionAction* page_action,
178                                    bool preview_enabled);
179
180   // Retrieves the PageAction View which is associated with |page_action|.
181   views::View* GetPageActionView(ExtensionAction* page_action);
182
183   // Toggles the star on or off.
184   void SetStarToggled(bool on);
185
186   // Returns the star view. It may not be visible.
187   StarView* star_view() { return star_view_; }
188
189   TranslateIconView* translate_icon_view() {
190     return translate_icon_view_;
191   }
192
193   // Shows the bookmark prompt.
194   void ShowBookmarkPrompt();
195
196   // Returns the screen coordinates of the location entry (where the URL text
197   // appears, not where the icons are shown).
198   gfx::Point GetLocationEntryOrigin() const;
199
200   // Shows |text| as an inline autocompletion.  This is useful for IMEs, where
201   // we can't show the autocompletion inside the actual OmniboxView.  See
202   // comments on |ime_inline_autocomplete_view_|.
203   void SetImeInlineAutocompletion(const string16& text);
204
205   // Invoked from OmniboxViewWin to show gray text autocompletion.
206   void SetGrayTextAutocompletion(const string16& text);
207
208   // Returns the current gray text autocompletion.
209   string16 GetGrayTextAutocompletion() const;
210
211   // Sets whether the location entry can accept focus.
212   void SetLocationEntryFocusable(bool focusable);
213
214   // Returns true if the location entry is focusable and visible in
215   // the root view.
216   bool IsLocationEntryFocusableInRootView() const;
217
218   // Sizing functions
219   virtual gfx::Size GetPreferredSize() OVERRIDE;
220
221   // Layout and Painting functions
222   virtual void Layout() OVERRIDE;
223   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
224
225   // No focus border for the location bar, the caret is enough.
226   virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE { }
227
228   // Set if we should show a focus rect while the location entry field is
229   // focused. Used when the toolbar is in full keyboard accessibility mode.
230   // Repaints if necessary.
231   virtual void SetShowFocusRect(bool show);
232
233   // Select all of the text. Needed when the user tabs through controls
234   // in the toolbar in full keyboard accessibility mode.
235   virtual void SelectAll();
236
237 #if defined(OS_WIN) && !defined(USE_AURA)
238   // Event Handlers
239   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
240   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
241   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
242   virtual void OnMouseCaptureLost() OVERRIDE;
243 #endif
244
245   LocationIconView* location_icon_view() { return location_icon_view_; }
246   const LocationIconView* location_icon_view() const {
247     return location_icon_view_;
248   }
249
250   views::View* location_entry_view() const { return location_entry_view_; }
251
252   views::View* generated_credit_card_view();
253
254   // OmniboxEditController:
255   virtual void Update(const content::WebContents* contents) OVERRIDE;
256   virtual void OnChanged() OVERRIDE;
257   virtual void OnSetFocus() OVERRIDE;
258   virtual InstantController* GetInstant() OVERRIDE;
259   virtual content::WebContents* GetWebContents() OVERRIDE;
260   virtual ToolbarModel* GetToolbarModel() OVERRIDE;
261   virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
262
263   // views::View:
264   virtual const char* GetClassName() const OVERRIDE;
265   virtual bool SkipDefaultKeyEventProcessing(
266       const ui::KeyEvent& event) OVERRIDE;
267   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
268   virtual bool HasFocus() const OVERRIDE;
269   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
270
271   // views::ButtonListener:
272   virtual void ButtonPressed(views::Button* sender,
273                              const ui::Event& event) OVERRIDE;
274
275   // views::DragController:
276   virtual void WriteDragDataForView(View* sender,
277                                     const gfx::Point& press_pt,
278                                     OSExchangeData* data) OVERRIDE;
279   virtual int GetDragOperationsForView(View* sender,
280                                        const gfx::Point& p) OVERRIDE;
281   virtual bool CanStartDragForView(View* sender,
282                                    const gfx::Point& press_pt,
283                                    const gfx::Point& p) OVERRIDE;
284
285   // LocationBar:
286   virtual void ShowFirstRunBubble() OVERRIDE;
287   virtual GURL GetDestinationURL() const OVERRIDE;
288   virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
289   virtual content::PageTransition GetPageTransition() const OVERRIDE;
290   virtual void AcceptInput() OVERRIDE;
291   virtual void FocusLocation(bool select_all) OVERRIDE;
292   virtual void FocusSearch() OVERRIDE;
293   virtual void UpdateContentSettingsIcons() OVERRIDE;
294   virtual void UpdatePageActions() OVERRIDE;
295   virtual void InvalidatePageActions() OVERRIDE;
296   virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
297   virtual void UpdateGeneratedCreditCardView() OVERRIDE;
298   virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
299   virtual void Revert() OVERRIDE;
300   virtual const OmniboxView* GetLocationEntry() const OVERRIDE;
301   virtual OmniboxView* GetLocationEntry() OVERRIDE;
302   virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
303
304   // LocationBarTesting:
305   virtual int PageActionCount() OVERRIDE;
306   virtual int PageActionVisibleCount() OVERRIDE;
307   virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
308   virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
309   virtual void TestPageActionPressed(size_t index) OVERRIDE;
310   virtual bool GetBookmarkStarVisibility() OVERRIDE;
311
312   // TemplateURLServiceObserver:
313   virtual void OnTemplateURLServiceChanged() OVERRIDE;
314
315   // content::NotificationObserver:
316   virtual void Observe(int type,
317                        const content::NotificationSource& source,
318                        const content::NotificationDetails& details) OVERRIDE;
319
320   // SearchModelObserver:
321   virtual void ModelChanged(const SearchModel::State& old_state,
322                             const SearchModel::State& new_state) OVERRIDE;
323
324   // Returns the height of the control without the top and bottom
325   // edges(i.e.  the height of the edit control inside).  If
326   // |use_preferred_size| is true this will be the preferred height,
327   // otherwise it will be the current height.
328   int GetInternalHeight(bool use_preferred_size);
329
330   // Returns the position and width that the popup should be, and also the left
331   // edge that the results should align themselves to (which will leave some
332   // border on the left of the popup).
333   void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
334                                       int* popup_width,
335                                       int* left_margin,
336                                       int* right_margin);
337
338   // Space between items in the location bar, as well as between items and the
339   // edges.
340   static int GetItemPadding();
341
342   // Thickness of the edges of the omnibox background images, in normal mode.
343   static const int kNormalEdgeThickness;
344   // The same, but for popup mode.
345   static const int kPopupEdgeThickness;
346   // Amount of padding built into the standard omnibox icons.
347   static const int kIconInternalPadding;
348   // Space between the edge and a bubble.
349   static const int kBubblePadding;
350
351  protected:
352   virtual void OnFocus() OVERRIDE;
353
354  private:
355   typedef std::vector<ContentSettingImageView*> ContentSettingViews;
356
357   friend class PageActionImageView;
358   friend class PageActionWithBadgeView;
359   typedef std::vector<PageActionWithBadgeView*> PageActionViews;
360
361   // Returns the number of pixels of built-in padding to the left and right for
362   // child views. This is nonzero when touch UI is enabled so as to space out
363   // child views for easier targeting. See InitTouchableLocationBarChildView().
364   static int GetBuiltInHorizontalPaddingForChildViews();
365
366   // Returns the thickness of any visible left and right edge, in pixels.
367   int GetHorizontalEdgeThickness() const;
368
369   // The same, but for the top and bottom edges.
370   int vertical_edge_thickness() const {
371     return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
372   }
373
374   // Updates the visibility state of the Content Blocked icons to reflect what
375   // is actually blocked on the current page. Calling this function should
376   // always eventually be followed by calling Layout() and then
377   // UpdateContentSettingViewsPostLayout(), to ensure the icons can completely
378   // update their states.
379   void UpdateContentSettingViewsPreLayout();
380
381   // Updates after the correct screen coordinates have been set for icons.
382   // Allows content setting icons to perform any updating which can't complete
383   // until after the icons have been correctly laid out.  This should be called
384   // after UpdateContentSettingViewsPreLayout() and a subsequent Layout().
385   void UpdateContentSettingViewsPostLayout();
386
387   // Deletes all page action views that we have created.
388   void DeletePageActionViews();
389
390   // Updates the views for the Page Actions, to reflect state changes for
391   // PageActions.
392   void RefreshPageActionViews();
393
394   // Returns the number of scripts currently running on the page.
395   size_t ScriptBubbleScriptsRunning();
396
397   // Updates the Script Bubble Icon, to reflect the number of content scripts
398   // running on the page.
399   void RefreshScriptBubble();
400
401   // Updates the view for the zoom icon based on the current tab's zoom.
402   void RefreshZoomView();
403
404   // Updates the Translate icon based on the current tab's Translate status.
405   void RefreshTranslateIcon();
406
407   // Sets the visibility of view to new_vis.
408   void ToggleVisibility(bool new_vis, views::View* view);
409
410 #if !defined(USE_AURA)
411   // Helper for the Mouse event handlers that does all the real work.
412   void OnMouseEvent(const ui::MouseEvent& event, UINT msg);
413 #endif
414
415   // Returns true if the suggest text is valid.
416   bool HasValidSuggestText() const;
417
418   // Helper to show the first run info bubble.
419   void ShowFirstRunBubbleInternal();
420
421   // Draws backgrounds and borders for page actions.  Must be called
422   // after layout, so the |page_action_views_| have their bounds.
423   void PaintPageActionBackgrounds(gfx::Canvas* canvas);
424
425   // Handles a request to change the value of this text field from software
426   // using an accessibility API (typically automation software, screen readers
427   // don't normally use this). Sets the value and clears the selection.
428   void AccessibilitySetValue(const string16& new_value);
429
430   // The Browser this LocationBarView is in.  Note that at least
431   // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
432   // window, so this may be NULL.
433   Browser* browser_;
434
435   // The Autocomplete Edit field.
436   scoped_ptr<OmniboxView> location_entry_;
437
438   // The profile which corresponds to this View.
439   Profile* profile_;
440
441   // Our delegate.
442   Delegate* delegate_;
443
444   // An object used to paint the normal-mode background.
445   scoped_ptr<views::Painter> background_border_painter_;
446   scoped_ptr<views::Painter> background_filling_painter_;
447
448   // An icon to the left of the edit field.
449   LocationIconView* location_icon_view_;
450
451   // A bubble displayed for EV HTTPS sites.
452   EVBubbleView* ev_bubble_view_;
453
454   // Location_entry view
455   views::View* location_entry_view_;
456
457   // A view to show inline autocompletion when an IME is active.  In this case,
458   // we shouldn't change the text or selection inside the OmniboxView itself,
459   // since this will conflict with the IME's control over the text.  So instead
460   // we show any autocompletion in a separate field after the OmniboxView.
461   views::Label* ime_inline_autocomplete_view_;
462
463   // The following views are used to provide hints and remind the user as to
464   // what is going in the edit. They are all added a children of the
465   // LocationBarView. At most one is visible at a time. Preference is
466   // given to the keyword_view_, then hint_view_.
467   // These autocollapse when the edit needs the room.
468
469   // Shown if the user has selected a keyword.
470   SelectedKeywordView* selected_keyword_view_;
471
472   // View responsible for showing suggested text. This is NULL when there is no
473   // suggested text.
474   views::Label* suggested_text_view_;
475
476   // Shown if the selected url has a corresponding keyword.
477   KeywordHintView* keyword_hint_view_;
478
479   // The voice search icon.
480   views::ImageButton* mic_search_view_;
481
482   // The content setting views.
483   ContentSettingViews content_setting_views_;
484
485   // The zoom icon.
486   ZoomView* zoom_view_;
487
488   // A bubble that shows after successfully generating a new credit card number.
489   GeneratedCreditCardView* generated_credit_card_view_;
490
491   // The icon to open a PDF in Reader.
492   OpenPDFInReaderView* open_pdf_in_reader_view_;
493
494   // The current page actions.
495   std::vector<ExtensionAction*> page_actions_;
496
497   // The page action icon views.
498   PageActionViews page_action_views_;
499
500   // The script bubble.
501   ScriptBubbleIconView* script_bubble_icon_view_;
502
503   // The star.
504   StarView* star_view_;
505
506   // The icon for Translate.
507   TranslateIconView* translate_icon_view_;
508
509   // Whether we're in popup mode. This value also controls whether the location
510   // bar is read-only.
511   const bool is_popup_mode_;
512
513   // True if we should show a focus rect while the location entry field is
514   // focused. Used when the toolbar is in full keyboard accessibility mode.
515   bool show_focus_rect_;
516
517   // This is in case we're destroyed before the model loads. We need to make
518   // Add/RemoveObserver calls.
519   TemplateURLService* template_url_service_;
520
521   // Tracks this preference to determine whether bookmark editing is allowed.
522   BooleanPrefMember edit_bookmarks_enabled_;
523
524   // While animating, the host clips the widget and draws only the bottom
525   // part of it. The view needs to know the pixel offset at which we are drawing
526   // the widget so that we can draw the curved edges that attach to the toolbar
527   // in the right location.
528   int animation_offset_;
529
530   // Used to register for notifications received by NotificationObserver.
531   content::NotificationRegistrar registrar_;
532
533   // Used to bind callback functions to this object.
534   base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
535
536   DISALLOW_COPY_AND_ASSIGN(LocationBarView);
537 };
538
539 #endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_