Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / location_bar_view.h
blobb302ca340e65369b408ff0d741f292b7a780f876
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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_
8 #include <string>
9 #include <vector>
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 "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
24 #include "content/public/browser/notification_observer.h"
25 #include "content/public/browser/notification_registrar.h"
26 #include "ui/gfx/font.h"
27 #include "ui/gfx/rect.h"
28 #include "ui/views/controls/button/button.h"
29 #include "ui/views/drag_controller.h"
31 class ActionBoxButtonView;
32 class CommandUpdater;
33 class ContentSettingBubbleModelDelegate;
34 class ContentSettingImageView;
35 class EVBubbleView;
36 class ExtensionAction;
37 class GURL;
38 class GeneratedCreditCardView;
39 class InstantController;
40 class KeywordHintView;
41 class LocationIconView;
42 class OpenPDFInReaderView;
43 class ManagePasswordsIconView;
44 class PageActionWithBadgeView;
45 class PageActionImageView;
46 class Profile;
47 class ScriptBubbleIconView;
48 class SelectedKeywordView;
49 class SiteChipView;
50 class StarView;
51 class TemplateURLService;
52 class TranslateIconView;
53 class ZoomView;
55 namespace content {
56 struct SSLStatus;
59 namespace views {
60 class BubbleDelegateView;
61 class ImageButton;
62 class ImageView;
63 class Label;
64 class LabelButton;
65 class Widget;
68 /////////////////////////////////////////////////////////////////////////////
70 // LocationBarView class
72 // The LocationBarView class is a View subclass that paints the background
73 // of the URL bar strip and contains its content.
75 /////////////////////////////////////////////////////////////////////////////
76 class LocationBarView : public LocationBar,
77 public LocationBarTesting,
78 public views::View,
79 public views::ButtonListener,
80 public views::DragController,
81 public OmniboxEditController,
82 public DropdownBarHostDelegate,
83 public TemplateURLServiceObserver,
84 public content::NotificationObserver,
85 public SearchModelObserver {
86 public:
87 // The location bar view's class name.
88 static const char kViewClassName[];
90 // DropdownBarHostDelegate:
91 virtual void SetFocusAndSelection(bool select_all) OVERRIDE;
92 virtual void SetAnimationOffset(int offset) OVERRIDE;
94 // Returns the offset used while animating.
95 int animation_offset() const { return animation_offset_; }
97 class Delegate {
98 public:
99 // Should return the current web contents.
100 virtual content::WebContents* GetWebContents() = 0;
102 // Returns the InstantController, or NULL if there isn't one.
103 virtual InstantController* GetInstant() = 0;
105 virtual ToolbarModel* GetToolbarModel() = 0;
106 virtual const ToolbarModel* GetToolbarModel() const = 0;
108 // Creates Widget for the given delegate.
109 virtual views::Widget* CreateViewsBubble(
110 views::BubbleDelegateView* bubble_delegate) = 0;
112 // Creates PageActionImageView. Caller gets an ownership.
113 virtual PageActionImageView* CreatePageActionImageView(
114 LocationBarView* owner,
115 ExtensionAction* action) = 0;
117 // Returns ContentSettingBubbleModelDelegate.
118 virtual ContentSettingBubbleModelDelegate*
119 GetContentSettingBubbleModelDelegate() = 0;
121 // Shows permissions and settings for the given web contents.
122 virtual void ShowWebsiteSettings(content::WebContents* web_contents,
123 const GURL& url,
124 const content::SSLStatus& ssl) = 0;
126 protected:
127 virtual ~Delegate() {}
130 enum ColorKind {
131 BACKGROUND = 0,
132 TEXT,
133 SELECTED_TEXT,
134 DEEMPHASIZED_TEXT,
135 SECURITY_TEXT,
138 LocationBarView(Browser* browser,
139 Profile* profile,
140 CommandUpdater* command_updater,
141 Delegate* delegate,
142 bool is_popup_mode);
144 virtual ~LocationBarView();
146 // Uses GetBuiltInHorizontalPaddingForChildViews() to optionally add
147 // additional padding (via an empty border) to |view|. This should be called
148 // during creation on all child views which are potentially touchable so that
149 // when touch is enabled they will have sufficient padding.
150 static void InitTouchableLocationBarChildView(views::View* view);
152 // Initializes the LocationBarView.
153 void Init();
155 // True if this instance has been initialized by calling Init, which can only
156 // be called when the receiving instance is attached to a view container.
157 bool IsInitialized() const;
159 // Returns the appropriate color for the desired kind, based on the user's
160 // system theme.
161 SkColor GetColor(ToolbarModel::SecurityLevel security_level,
162 ColorKind kind) const;
164 // Returns the delegate.
165 Delegate* delegate() const { return delegate_; }
167 // See comment in browser_window.h for more info.
168 void ZoomChangedForActiveTab(bool can_show_bubble);
170 // The zoom icon. It may not be visible.
171 ZoomView* zoom_view() { return zoom_view_; }
173 // The passwords icon. It may not be visible.
174 ManagePasswordsIconView* manage_passwords_icon_view() {
175 return manage_passwords_icon_view_;
178 // Sets |preview_enabled| for the PageAction View associated with this
179 // |page_action|. If |preview_enabled| is true, the view will display the
180 // PageActions icon even though it has not been activated by the extension.
181 // This is used by the ExtensionInstalledBubble to preview what the icon
182 // will look like for the user upon installation of the extension.
183 void SetPreviewEnabledPageAction(ExtensionAction* page_action,
184 bool preview_enabled);
186 // Retrieves the PageAction View which is associated with |page_action|.
187 views::View* GetPageActionView(ExtensionAction* page_action);
189 // Toggles the star on or off.
190 void SetStarToggled(bool on);
192 // The star. It may not be visible.
193 StarView* star_view() { return star_view_; }
195 // Toggles the translate icon on or off.
196 void SetTranslateIconToggled(bool on);
198 // The translate icon. It may not be visible.
199 TranslateIconView* translate_icon_view() { return translate_icon_view_; }
201 void set_site_chip_view(SiteChipView* site_chip_view) {
202 site_chip_view_ = site_chip_view;
205 // Shows the bookmark prompt.
206 void ShowBookmarkPrompt();
208 // Returns the screen coordinates of the omnibox (where the URL text appears,
209 // not where the icons are shown).
210 gfx::Point GetOmniboxViewOrigin() const;
212 // Shows |text| as an inline autocompletion. This is useful for IMEs, where
213 // we can't show the autocompletion inside the actual OmniboxView. See
214 // comments on |ime_inline_autocomplete_view_|.
215 void SetImeInlineAutocompletion(const base::string16& text);
217 // Invoked from OmniboxViewWin to show gray text autocompletion.
218 void SetGrayTextAutocompletion(const base::string16& text);
220 // Returns the current gray text autocompletion.
221 base::string16 GetGrayTextAutocompletion() const;
223 // Sizing functions
224 virtual gfx::Size GetPreferredSize() OVERRIDE;
226 // Layout and Painting functions
227 virtual void Layout() OVERRIDE;
228 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE;
229 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
231 // Set if we should show a focus rect while the location entry field is
232 // focused. Used when the toolbar is in full keyboard accessibility mode.
233 // Repaints if necessary.
234 virtual void SetShowFocusRect(bool show);
236 // Select all of the text. Needed when the user tabs through controls
237 // in the toolbar in full keyboard accessibility mode.
238 virtual void SelectAll();
240 views::ImageView* GetLocationIconView();
241 const views::ImageView* GetLocationIconView() const;
243 // Return a view suitable for anchoring location-bar-anchored bubbles to.
244 views::View* GetLocationBarAnchor();
245 // Return the point suitable for anchoring location-bar-anchored bubbles at.
246 // The point will be returned in the coordinates of the LocationBarView.
247 gfx::Point GetLocationBarAnchorPoint() const;
249 OmniboxViewViews* omnibox_view() { return omnibox_view_; }
251 views::View* generated_credit_card_view();
253 // OmniboxEditController:
254 virtual void Update(const content::WebContents* contents) OVERRIDE;
255 virtual void OnChanged() OVERRIDE;
256 virtual void OnSetFocus() OVERRIDE;
257 virtual InstantController* GetInstant() OVERRIDE;
258 virtual content::WebContents* GetWebContents() OVERRIDE;
259 virtual ToolbarModel* GetToolbarModel() OVERRIDE;
260 virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
262 // views::View:
263 virtual const char* GetClassName() const OVERRIDE;
264 virtual bool HasFocus() const OVERRIDE;
265 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
266 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
268 // views::ButtonListener:
269 virtual void ButtonPressed(views::Button* sender,
270 const ui::Event& event) OVERRIDE;
272 // views::DragController:
273 virtual void WriteDragDataForView(View* sender,
274 const gfx::Point& press_pt,
275 OSExchangeData* data) OVERRIDE;
276 virtual int GetDragOperationsForView(View* sender,
277 const gfx::Point& p) OVERRIDE;
278 virtual bool CanStartDragForView(View* sender,
279 const gfx::Point& press_pt,
280 const gfx::Point& p) OVERRIDE;
282 // LocationBar:
283 virtual void ShowFirstRunBubble() OVERRIDE;
284 virtual GURL GetDestinationURL() const OVERRIDE;
285 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
286 virtual content::PageTransition GetPageTransition() const OVERRIDE;
287 virtual void AcceptInput() OVERRIDE;
288 virtual void FocusLocation(bool select_all) OVERRIDE;
289 virtual void FocusSearch() OVERRIDE;
290 virtual void UpdateContentSettingsIcons() OVERRIDE;
291 virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
292 virtual void UpdatePageActions() OVERRIDE;
293 virtual void InvalidatePageActions() OVERRIDE;
294 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
295 virtual void UpdateGeneratedCreditCardView() OVERRIDE;
296 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
297 virtual void Revert() OVERRIDE;
298 virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
299 virtual OmniboxView* GetOmniboxView() OVERRIDE;
300 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
302 // LocationBarTesting:
303 virtual int PageActionCount() OVERRIDE;
304 virtual int PageActionVisibleCount() OVERRIDE;
305 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
306 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
307 virtual void TestPageActionPressed(size_t index) OVERRIDE;
308 virtual bool GetBookmarkStarVisibility() OVERRIDE;
310 // TemplateURLServiceObserver:
311 virtual void OnTemplateURLServiceChanged() OVERRIDE;
313 // content::NotificationObserver:
314 virtual void Observe(int type,
315 const content::NotificationSource& source,
316 const content::NotificationDetails& details) OVERRIDE;
318 // SearchModelObserver:
319 virtual void ModelChanged(const SearchModel::State& old_state,
320 const SearchModel::State& new_state) OVERRIDE;
322 // Returns the height of the control without the top and bottom
323 // edges(i.e. the height of the edit control inside). If
324 // |use_preferred_size| is true this will be the preferred height,
325 // otherwise it will be the current height.
326 int GetInternalHeight(bool use_preferred_size);
328 // Returns the position and width that the popup should be, and also the left
329 // edge that the results should align themselves to (which will leave some
330 // border on the left of the popup).
331 void GetOmniboxPopupPositioningInfo(gfx::Point* top_left_screen_coord,
332 int* popup_width,
333 int* left_margin,
334 int* right_margin);
336 // Space between items in the location bar, as well as between items and the
337 // edges.
338 static int GetItemPadding();
340 // Thickness of the edges of the omnibox background images, in normal mode.
341 static const int kNormalEdgeThickness;
342 // The same, but for popup mode.
343 static const int kPopupEdgeThickness;
344 // Amount of padding built into the standard omnibox icons.
345 static const int kIconInternalPadding;
346 // Space between the edge and a bubble.
347 static const int kBubblePadding;
349 protected:
350 virtual void OnFocus() OVERRIDE;
352 private:
353 typedef std::vector<ContentSettingImageView*> ContentSettingViews;
355 friend class PageActionImageView;
356 friend class PageActionWithBadgeView;
357 typedef std::vector<ExtensionAction*> PageActions;
358 typedef std::vector<PageActionWithBadgeView*> PageActionViews;
360 // Returns the number of pixels of built-in padding to the left and right for
361 // child views. This is nonzero when touch UI is enabled so as to space out
362 // child views for easier targeting. See InitTouchableLocationBarChildView().
363 static int GetBuiltInHorizontalPaddingForChildViews();
365 // Returns the thickness of any visible left and right edge, in pixels.
366 int GetHorizontalEdgeThickness() const;
368 // The same, but for the top and bottom edges.
369 int vertical_edge_thickness() const {
370 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
373 // Updates the visibility state of the Content Blocked icons to reflect what
374 // is actually blocked on the current page. Returns true if the visibility
375 // of at least one of the views in |content_setting_views_| changed.
376 bool RefreshContentSettingViews();
378 // Deletes all page action views that we have created.
379 void DeletePageActionViews();
381 // Updates the views for the Page Actions, to reflect state changes for
382 // PageActions. Returns true if the visibility of a PageActionWithBadgeView
383 // changed, or PageActionWithBadgeView were created/destroyed.
384 bool RefreshPageActionViews();
386 // Returns the number of scripts currently running on the page.
387 size_t ScriptBubbleScriptsRunning();
389 // Updates the Script Bubble Icon, to reflect the number of content scripts
390 // running on the page. Returns true if the visibility of the bubble changed.
391 bool RefreshScriptBubble();
393 // Updates the view for the zoom icon based on the current tab's zoom. Returns
394 // true if the visibility of the view changed.
395 bool RefreshZoomView();
397 // Updates the Translate icon based on the current tab's Translate status.
398 void RefreshTranslateIcon();
400 // Sets the visibility of view to new_vis.
401 void ToggleVisibility(bool new_vis, views::View* view);
403 // Updates |manage_passwords_icon_view_|. Returns true if visibility changed.
404 bool RefreshManagePasswordsIconView();
406 // Shows the manage passwords bubble if there is a savable password.
407 void ShowManagePasswordsBubbleIfNeeded();
409 // Returns true if the suggest text is valid.
410 bool HasValidSuggestText() const;
412 // Helper to show the first run info bubble.
413 void ShowFirstRunBubbleInternal();
415 // Draws backgrounds and borders for page actions. Must be called
416 // after layout, so the |page_action_views_| have their bounds.
417 void PaintPageActionBackgrounds(gfx::Canvas* canvas);
419 // Handles a request to change the value of this text field from software
420 // using an accessibility API (typically automation software, screen readers
421 // don't normally use this). Sets the value and clears the selection.
422 void AccessibilitySetValue(const base::string16& new_value);
424 // The Browser this LocationBarView is in. Note that at least
425 // chromeos::SimpleWebViewDialog uses a LocationBarView outside any browser
426 // window, so this may be NULL.
427 Browser* browser_;
429 OmniboxViewViews* omnibox_view_;
431 // Our delegate.
432 Delegate* delegate_;
434 // Object used to paint the border.
435 scoped_ptr<views::Painter> border_painter_;
437 // An icon to the left of the edit field.
438 LocationIconView* location_icon_view_;
440 // A bubble displayed for EV HTTPS sites.
441 EVBubbleView* ev_bubble_view_;
443 // A view to show inline autocompletion when an IME is active. In this case,
444 // we shouldn't change the text or selection inside the OmniboxView itself,
445 // since this will conflict with the IME's control over the text. So instead
446 // we show any autocompletion in a separate field after the OmniboxView.
447 views::Label* ime_inline_autocomplete_view_;
449 // The following views are used to provide hints and remind the user as to
450 // what is going in the edit. They are all added a children of the
451 // LocationBarView. At most one is visible at a time. Preference is
452 // given to the keyword_view_, then hint_view_.
453 // These autocollapse when the edit needs the room.
455 // Shown if the user has selected a keyword.
456 SelectedKeywordView* selected_keyword_view_;
458 // View responsible for showing suggested text. This is NULL when there is no
459 // suggested text.
460 views::Label* suggested_text_view_;
462 // Shown if the selected url has a corresponding keyword.
463 KeywordHintView* keyword_hint_view_;
465 // The voice search icon.
466 views::ImageButton* mic_search_view_;
468 // The content setting views.
469 ContentSettingViews content_setting_views_;
471 // The zoom icon.
472 ZoomView* zoom_view_;
474 // A bubble that shows after successfully generating a new credit card number.
475 GeneratedCreditCardView* generated_credit_card_view_;
477 // The icon to open a PDF in Reader.
478 OpenPDFInReaderView* open_pdf_in_reader_view_;
480 // The manage passwords icon.
481 ManagePasswordsIconView* manage_passwords_icon_view_;
483 // The current page actions.
484 PageActions page_actions_;
486 // The page action icon views.
487 PageActionViews page_action_views_;
489 // The script bubble.
490 ScriptBubbleIconView* script_bubble_icon_view_;
492 // The Site Chip.
493 SiteChipView* site_chip_view_;
495 // The icon for Translate.
496 TranslateIconView* translate_icon_view_;
498 // The star.
499 StarView* star_view_;
501 // The search/go button.
502 views::LabelButton* search_button_;
504 // Whether we're in popup mode. This value also controls whether the location
505 // bar is read-only.
506 const bool is_popup_mode_;
508 // True if we should show a focus rect while the location entry field is
509 // focused. Used when the toolbar is in full keyboard accessibility mode.
510 bool show_focus_rect_;
512 // This is in case we're destroyed before the model loads. We need to make
513 // Add/RemoveObserver calls.
514 TemplateURLService* template_url_service_;
516 // Tracks this preference to determine whether bookmark editing is allowed.
517 BooleanPrefMember edit_bookmarks_enabled_;
519 // While animating, the host clips the widget and draws only the bottom
520 // part of it. The view needs to know the pixel offset at which we are drawing
521 // the widget so that we can draw the curved edges that attach to the toolbar
522 // in the right location.
523 int animation_offset_;
525 // Used to register for notifications received by NotificationObserver.
526 content::NotificationRegistrar registrar_;
528 // Used to bind callback functions to this object.
529 base::WeakPtrFactory<LocationBarView> weak_ptr_factory_;
531 DISALLOW_COPY_AND_ASSIGN(LocationBarView);
534 #endif // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_LOCATION_BAR_VIEW_H_