Home | History | Annotate | Download | only in location_bar
      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_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_
      6 #define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_
      7 
      8 #include <string>
      9 
     10 #import <Cocoa/Cocoa.h>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/prefs/pref_member.h"
     16 #include "chrome/browser/ui/browser.h"
     17 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
     18 #include "chrome/browser/ui/location_bar/location_bar.h"
     19 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
     20 #include "chrome/browser/ui/search/search_model_observer.h"
     21 #include "components/content_settings/core/common/content_settings_types.h"
     22 
     23 @class AutocompleteTextField;
     24 class CommandUpdater;
     25 class ContentSettingDecoration;
     26 class EVBubbleDecoration;
     27 class GeneratedCreditCardDecoration;
     28 class KeywordHintDecoration;
     29 class LocationBarDecoration;
     30 class LocationIconDecoration;
     31 class ManagePasswordsDecoration;
     32 class MicSearchDecoration;
     33 class OriginChipDecoration;
     34 class PageActionDecoration;
     35 class Profile;
     36 class SearchButtonDecoration;
     37 class SelectedKeywordDecoration;
     38 class StarDecoration;
     39 class TranslateDecoration;
     40 class ZoomDecoration;
     41 class ZoomDecorationTest;
     42 
     43 // A C++ bridge class that represents the location bar UI element to
     44 // the portable code.  Wires up an OmniboxViewMac instance to
     45 // the location bar text field, which handles most of the work.
     46 
     47 class LocationBarViewMac : public LocationBar,
     48                            public LocationBarTesting,
     49                            public OmniboxEditController,
     50                            public SearchModelObserver {
     51  public:
     52   LocationBarViewMac(AutocompleteTextField* field,
     53                      CommandUpdater* command_updater,
     54                      Profile* profile,
     55                      Browser* browser);
     56   virtual ~LocationBarViewMac();
     57 
     58   // Overridden from LocationBar:
     59   virtual void ShowFirstRunBubble() OVERRIDE;
     60   virtual GURL GetDestinationURL() const OVERRIDE;
     61   virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE;
     62   virtual ui::PageTransition GetPageTransition() const OVERRIDE;
     63   virtual void AcceptInput() OVERRIDE;
     64   virtual void FocusLocation(bool select_all) OVERRIDE;
     65   virtual void FocusSearch() OVERRIDE;
     66   virtual void UpdateContentSettingsIcons() OVERRIDE;
     67   virtual void UpdateManagePasswordsIconAndBubble() OVERRIDE;
     68   virtual void UpdatePageActions() OVERRIDE;
     69   virtual void InvalidatePageActions() OVERRIDE;
     70   virtual void UpdateBookmarkStarVisibility() OVERRIDE;
     71   virtual bool ShowPageActionPopup(const extensions::Extension* extension,
     72                                    bool grant_active_tab) OVERRIDE;
     73   virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE;
     74   virtual void UpdateGeneratedCreditCardView() OVERRIDE;
     75   virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE;
     76   virtual void Revert() OVERRIDE;
     77   virtual const OmniboxView* GetOmniboxView() const OVERRIDE;
     78   virtual OmniboxView* GetOmniboxView() OVERRIDE;
     79   virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE;
     80 
     81   // Overridden from LocationBarTesting:
     82   virtual int PageActionCount() OVERRIDE;
     83   virtual int PageActionVisibleCount() OVERRIDE;
     84   virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE;
     85   virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE;
     86   virtual void TestPageActionPressed(size_t index) OVERRIDE;
     87   virtual bool GetBookmarkStarVisibility() OVERRIDE;
     88 
     89   // Set/Get the editable state of the field.
     90   void SetEditable(bool editable);
     91   bool IsEditable();
     92 
     93   // Set the starred state of the bookmark star.
     94   void SetStarred(bool starred);
     95 
     96   // Set whether or not the translate icon is lit.
     97   void SetTranslateIconLit(bool on);
     98 
     99   // Happens when the zoom changes for the active tab. |can_show_bubble| is
    100   // false when the change in zoom for the active tab wasn't an explicit user
    101   // action (e.g. switching tabs, creating a new tab, creating a new browser).
    102   // Additionally, |can_show_bubble| will only be true when the bubble wouldn't
    103   // be obscured by other UI (wrench menu) or redundant (+/- from wrench).
    104   void ZoomChangedForActiveTab(bool can_show_bubble);
    105 
    106   // Checks if the bookmark star should be enabled or not.
    107   bool IsStarEnabled() const;
    108 
    109   // Get the point in window coordinates on the star for the bookmark bubble to
    110   // aim at. Only works if IsStarEnabled returns YES.
    111   NSPoint GetBookmarkBubblePoint() const;
    112 
    113   // Get the point in window coordinates on the star for the Translate bubble to
    114   // aim at.
    115   NSPoint GetTranslateBubblePoint() const;
    116 
    117   // Get the point in window coordinates in the lock icon for the Manage
    118   // Passwords bubble to aim at.
    119   NSPoint GetManagePasswordsBubblePoint() const;
    120 
    121   // Get the point in window coordinates in the security icon at which the page
    122   // info bubble aims.
    123   NSPoint GetPageInfoBubblePoint() const;
    124 
    125   // Get the point in window coordinates in the "generated cc" icon at which the
    126   // corresponding info bubble aims.
    127   NSPoint GetGeneratedCreditCardBubblePoint() const;
    128 
    129   // When any image decorations change, call this to ensure everything is
    130   // redrawn and laid out if necessary.
    131   void OnDecorationsChanged();
    132 
    133   // Layout the various decorations which live in the field.
    134   void Layout();
    135 
    136   // Re-draws |decoration| if it's already being displayed.
    137   void RedrawDecoration(LocationBarDecoration* decoration);
    138 
    139   // Sets preview_enabled_ for the PageActionImageView associated with this
    140   // |page_action|. If |preview_enabled|, the location bar will display the
    141   // PageAction icon even if it has not been activated by the extension.
    142   // This is used by the ExtensionInstalledBubble to preview what the icon
    143   // will look like for the user upon installation of the extension.
    144   void SetPreviewEnabledPageAction(ExtensionAction* page_action,
    145                                    bool preview_enabled);
    146 
    147   // Retrieve the frame for the given |page_action|.
    148   NSRect GetPageActionFrame(ExtensionAction* page_action);
    149 
    150   // Return |page_action|'s info-bubble point in window coordinates.
    151   // This function should always be called with a visible page action.
    152   // If |page_action| is not a page action or not visible, NOTREACHED()
    153   // is called and this function returns |NSZeroPoint|.
    154   NSPoint GetPageActionBubblePoint(ExtensionAction* page_action);
    155 
    156   // OmniboxEditController:
    157   virtual void Update(const content::WebContents* contents) OVERRIDE;
    158   virtual void OnChanged() OVERRIDE;
    159   virtual void OnSetFocus() OVERRIDE;
    160   virtual void ShowURL() OVERRIDE;
    161   virtual void EndOriginChipAnimations(bool cancel_fade) OVERRIDE;
    162   virtual InstantController* GetInstant() OVERRIDE;
    163   virtual content::WebContents* GetWebContents() OVERRIDE;
    164   virtual ToolbarModel* GetToolbarModel() OVERRIDE;
    165   virtual const ToolbarModel* GetToolbarModel() const OVERRIDE;
    166 
    167   NSImage* GetKeywordImage(const base::string16& keyword);
    168 
    169   AutocompleteTextField* GetAutocompleteTextField() { return field_; }
    170 
    171   ManagePasswordsDecoration* manage_passwords_decoration() {
    172     return manage_passwords_decoration_.get();
    173   }
    174 
    175   // SearchModelObserver:
    176   virtual void ModelChanged(const SearchModel::State& old_state,
    177                             const SearchModel::State& new_state) OVERRIDE;
    178 
    179   Browser* browser() const { return browser_; }
    180 
    181  protected:
    182   // OmniboxEditController:
    183   virtual void HideURL() OVERRIDE;
    184 
    185  private:
    186   friend ZoomDecorationTest;
    187 
    188   // Posts |notification| to the default notification center.
    189   void PostNotification(NSString* notification);
    190 
    191   // Return the decoration for |page_action|.
    192   PageActionDecoration* GetPageActionDecoration(ExtensionAction* page_action);
    193 
    194   // Clear the page-action decorations.
    195   void DeletePageActionDecorations();
    196 
    197   void OnEditBookmarksEnabledChanged();
    198 
    199   // Re-generate the page-action decorations from the profile's
    200   // extension service.
    201   void RefreshPageActionDecorations();
    202 
    203   // Updates visibility of the content settings icons based on the current
    204   // tab contents state.
    205   bool RefreshContentSettingsDecorations();
    206 
    207   void ShowFirstRunBubbleInternal();
    208 
    209   // Updates the translate decoration in the omnibox with the current translate
    210   // state.
    211   void UpdateTranslateDecoration();
    212 
    213   // Updates the zoom decoration in the omnibox with the current zoom level.
    214   // Returns whether any updates were made.
    215   bool UpdateZoomDecoration();
    216 
    217   // Updates the voice search decoration. Returns true if the visible state was
    218   // changed.
    219   bool UpdateMicSearchDecorationVisibility();
    220 
    221   scoped_ptr<OmniboxViewMac> omnibox_view_;
    222 
    223   AutocompleteTextField* field_;  // owned by tab controller
    224 
    225   // A decoration that shows an icon to the left of the address.
    226   scoped_ptr<LocationIconDecoration> location_icon_decoration_;
    227 
    228   // A decoration that shows the keyword-search bubble on the left.
    229   scoped_ptr<SelectedKeywordDecoration> selected_keyword_decoration_;
    230 
    231   // A decoration that shows a lock icon and ev-cert label in a bubble
    232   // on the left.
    233   scoped_ptr<EVBubbleDecoration> ev_bubble_decoration_;
    234 
    235   // Bookmark star right of page actions.
    236   scoped_ptr<StarDecoration> star_decoration_;
    237 
    238   // Translate icon at the end of the ominibox.
    239   scoped_ptr<TranslateDecoration> translate_decoration_;
    240 
    241   // A zoom icon at the end of the omnibox, which shows at non-standard zoom
    242   // levels.
    243   scoped_ptr<ZoomDecoration> zoom_decoration_;
    244 
    245   // The installed page actions.
    246   std::vector<ExtensionAction*> page_actions_;
    247 
    248   // Decorations for the installed Page Actions.
    249   ScopedVector<PageActionDecoration> page_action_decorations_;
    250 
    251   // The content blocked decorations.
    252   ScopedVector<ContentSettingDecoration> content_setting_decorations_;
    253 
    254   // Keyword hint decoration displayed on the right-hand side.
    255   scoped_ptr<KeywordHintDecoration> keyword_hint_decoration_;
    256 
    257   // The voice search icon.
    258   scoped_ptr<MicSearchDecoration> mic_search_decoration_;
    259 
    260   // Generated CC hint decoration.
    261   scoped_ptr<GeneratedCreditCardDecoration> generated_credit_card_decoration_;
    262 
    263   // The right-hand-side search button that is shown on search result pages.
    264   scoped_ptr<SearchButtonDecoration> search_button_decoration_;
    265 
    266   // The left-hand-side origin chip.
    267   scoped_ptr<OriginChipDecoration> origin_chip_decoration_;
    268 
    269   // The right-hand-side button to manage passwords associated with a page.
    270   scoped_ptr<ManagePasswordsDecoration> manage_passwords_decoration_;
    271 
    272   Browser* browser_;
    273 
    274   // Used to schedule a task for the first run info bubble.
    275   base::WeakPtrFactory<LocationBarViewMac> weak_ptr_factory_;
    276 
    277   // Used to change the visibility of the star decoration.
    278   BooleanPrefMember edit_bookmarks_enabled_;
    279 
    280   DISALLOW_COPY_AND_ASSIGN(LocationBarViewMac);
    281 };
    282 
    283 #endif  // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_
    284