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/omnibox/location_bar.h" 19 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h" 20 #include "chrome/browser/ui/toolbar/toolbar_model.h" 21 #include "chrome/common/content_settings_types.h" 22 23 @class AutocompleteTextField; 24 class CommandUpdater; 25 class ContentSettingDecoration; 26 class EVBubbleDecoration; 27 class KeywordHintDecoration; 28 class LocationBarDecoration; 29 class LocationIconDecoration; 30 class PageActionDecoration; 31 class Profile; 32 class SelectedKeywordDecoration; 33 class StarDecoration; 34 class ToolbarModel; 35 class ZoomDecoration; 36 class ZoomDecorationTest; 37 38 // A C++ bridge class that represents the location bar UI element to 39 // the portable code. Wires up an OmniboxViewMac instance to 40 // the location bar text field, which handles most of the work. 41 42 class LocationBarViewMac : public LocationBar, 43 public LocationBarTesting, 44 public OmniboxEditController, 45 public content::NotificationObserver { 46 public: 47 LocationBarViewMac(AutocompleteTextField* field, 48 CommandUpdater* command_updater, 49 ToolbarModel* toolbar_model, 50 Profile* profile, 51 Browser* browser); 52 virtual ~LocationBarViewMac(); 53 54 // Overridden from LocationBar: 55 virtual void ShowFirstRunBubble() OVERRIDE; 56 virtual string16 GetInputString() const OVERRIDE; 57 virtual WindowOpenDisposition GetWindowOpenDisposition() const OVERRIDE; 58 virtual content::PageTransition GetPageTransition() const OVERRIDE; 59 virtual void AcceptInput() OVERRIDE; 60 virtual void FocusLocation(bool select_all) OVERRIDE; 61 virtual void FocusSearch() OVERRIDE; 62 virtual void UpdateContentSettingsIcons() OVERRIDE; 63 virtual void UpdatePageActions() OVERRIDE; 64 virtual void InvalidatePageActions() OVERRIDE; 65 virtual void UpdateOpenPDFInReaderPrompt() OVERRIDE; 66 virtual void UpdateGeneratedCreditCardView() OVERRIDE; 67 virtual void SaveStateToContents(content::WebContents* contents) OVERRIDE; 68 virtual void Revert() OVERRIDE; 69 virtual const OmniboxView* GetLocationEntry() const OVERRIDE; 70 virtual OmniboxView* GetLocationEntry() OVERRIDE; 71 virtual LocationBarTesting* GetLocationBarForTesting() OVERRIDE; 72 73 // Overridden from LocationBarTesting: 74 virtual int PageActionCount() OVERRIDE; 75 virtual int PageActionVisibleCount() OVERRIDE; 76 virtual ExtensionAction* GetPageAction(size_t index) OVERRIDE; 77 virtual ExtensionAction* GetVisiblePageAction(size_t index) OVERRIDE; 78 virtual void TestPageActionPressed(size_t index) OVERRIDE; 79 virtual bool GetBookmarkStarVisibility() OVERRIDE; 80 81 // Set/Get the editable state of the field. 82 void SetEditable(bool editable); 83 bool IsEditable(); 84 85 // Set the starred state of the bookmark star. 86 void SetStarred(bool starred); 87 88 // Happens when the zoom changes for the active tab. |can_show_bubble| is 89 // false when the change in zoom for the active tab wasn't an explicit user 90 // action (e.g. switching tabs, creating a new tab, creating a new browser). 91 // Additionally, |can_show_bubble| will only be true when the bubble wouldn't 92 // be obscured by other UI (wrench menu) or redundant (+/- from wrench). 93 void ZoomChangedForActiveTab(bool can_show_bubble); 94 95 // Get the point in window coordinates on the star for the bookmark bubble to 96 // aim at. 97 NSPoint GetBookmarkBubblePoint() const; 98 99 // Get the point in window coordinates in the security icon at which the page 100 // info bubble aims. 101 NSPoint GetPageInfoBubblePoint() const; 102 103 // When any image decorations change, call this to ensure everything is 104 // redrawn and laid out if necessary. 105 void OnDecorationsChanged(); 106 107 // Updates the location bar. Resets the bar's permanent text and 108 // security style, and if |should_restore_state| is true, restores 109 // saved state from the tab (for tab switching). 110 void Update(const content::WebContents* tab, bool should_restore_state); 111 112 // Layout the various decorations which live in the field. 113 void Layout(); 114 115 // Re-draws |decoration| if it's already being displayed. 116 void RedrawDecoration(LocationBarDecoration* decoration); 117 118 // Sets preview_enabled_ for the PageActionImageView associated with this 119 // |page_action|. If |preview_enabled|, the location bar will display the 120 // PageAction icon even if it has not been activated by the extension. 121 // This is used by the ExtensionInstalledBubble to preview what the icon 122 // will look like for the user upon installation of the extension. 123 void SetPreviewEnabledPageAction(ExtensionAction* page_action, 124 bool preview_enabled); 125 126 // Retrieve the frame for the given |page_action|. 127 NSRect GetPageActionFrame(ExtensionAction* page_action); 128 129 // Return |page_action|'s info-bubble point in window coordinates. 130 // This function should always be called with a visible page action. 131 // If |page_action| is not a page action or not visible, NOTREACHED() 132 // is called and this function returns |NSZeroPoint|. 133 NSPoint GetPageActionBubblePoint(ExtensionAction* page_action); 134 135 // Get the blocked-popup content setting's frame in window 136 // coordinates. Used by the blocked-popup animation. Returns 137 // |NSZeroRect| if the relevant content setting decoration is not 138 // visible. 139 NSRect GetBlockedPopupRect() const; 140 141 // OmniboxEditController: 142 virtual void OnAutocompleteAccept( 143 const GURL& url, 144 WindowOpenDisposition disposition, 145 content::PageTransition transition, 146 const GURL& alternate_nav_url) OVERRIDE; 147 virtual void OnChanged() OVERRIDE; 148 virtual void OnSelectionBoundsChanged() OVERRIDE; 149 virtual void OnInputInProgress(bool in_progress) OVERRIDE; 150 virtual void OnKillFocus() OVERRIDE; 151 virtual void OnSetFocus() OVERRIDE; 152 virtual gfx::Image GetFavicon() const OVERRIDE; 153 virtual string16 GetTitle() const OVERRIDE; 154 virtual InstantController* GetInstant() OVERRIDE; 155 virtual content::WebContents* GetWebContents() const OVERRIDE; 156 157 NSImage* GetKeywordImage(const string16& keyword); 158 159 AutocompleteTextField* GetAutocompleteTextField() { return field_; } 160 161 162 // content::NotificationObserver: 163 virtual void Observe(int type, 164 const content::NotificationSource& source, 165 const content::NotificationDetails& details) OVERRIDE; 166 167 Browser* browser() const { return browser_; } 168 ToolbarModel* toolbar_model() const { return toolbar_model_; } 169 170 private: 171 friend ZoomDecorationTest; 172 173 // Posts |notification| to the default notification center. 174 void PostNotification(NSString* notification); 175 176 // Return the decoration for |page_action|. 177 PageActionDecoration* GetPageActionDecoration(ExtensionAction* page_action); 178 179 // Clear the page-action decorations. 180 void DeletePageActionDecorations(); 181 182 void OnEditBookmarksEnabledChanged(); 183 184 // Re-generate the page-action decorations from the profile's 185 // extension service. 186 void RefreshPageActionDecorations(); 187 188 // Updates visibility of the content settings icons based on the current 189 // tab contents state. 190 bool RefreshContentSettingsDecorations(); 191 192 void ShowFirstRunBubbleInternal(); 193 194 // Checks if the bookmark star should be enabled or not. 195 bool IsStarEnabled(); 196 197 // Updates the zoom decoration in the omnibox with the current zoom level. 198 void UpdateZoomDecoration(); 199 200 // Ensures the star decoration is visible or hidden, as required. 201 void UpdateStarDecorationVisibility(); 202 203 scoped_ptr<OmniboxViewMac> omnibox_view_; 204 205 CommandUpdater* command_updater_; // Weak, owned by Browser. 206 207 AutocompleteTextField* field_; // owned by tab controller 208 209 // When we get an OnAutocompleteAccept notification from the autocomplete 210 // edit, we save the input string so we can give it back to the browser on 211 // the LocationBar interface via GetInputString(). 212 string16 location_input_; 213 214 // The user's desired disposition for how their input should be opened. 215 WindowOpenDisposition disposition_; 216 217 // A decoration that shows an icon to the left of the address. 218 scoped_ptr<LocationIconDecoration> location_icon_decoration_; 219 220 // A decoration that shows the keyword-search bubble on the left. 221 scoped_ptr<SelectedKeywordDecoration> selected_keyword_decoration_; 222 223 // A decoration that shows a lock icon and ev-cert label in a bubble 224 // on the left. 225 scoped_ptr<EVBubbleDecoration> ev_bubble_decoration_; 226 227 // Bookmark star right of page actions. 228 scoped_ptr<StarDecoration> star_decoration_; 229 230 // A zoom icon at the end of the omnibox, which shows at non-standard zoom 231 // levels. 232 scoped_ptr<ZoomDecoration> zoom_decoration_; 233 234 // The installed page actions. 235 std::vector<ExtensionAction*> page_actions_; 236 237 // Decorations for the installed Page Actions. 238 ScopedVector<PageActionDecoration> page_action_decorations_; 239 240 // The content blocked decorations. 241 ScopedVector<ContentSettingDecoration> content_setting_decorations_; 242 243 // Keyword hint decoration displayed on the right-hand side. 244 scoped_ptr<KeywordHintDecoration> keyword_hint_decoration_; 245 246 Profile* profile_; 247 248 Browser* browser_; 249 250 ToolbarModel* toolbar_model_; // Weak, owned by Browser. 251 252 // The transition type to use for the navigation. 253 content::PageTransition transition_; 254 255 // Used to register for notifications received by NotificationObserver. 256 content::NotificationRegistrar registrar_; 257 258 // Used to schedule a task for the first run info bubble. 259 base::WeakPtrFactory<LocationBarViewMac> weak_ptr_factory_; 260 261 // Used to change the visibility of the star decoration. 262 BooleanPrefMember edit_bookmarks_enabled_; 263 264 DISALLOW_COPY_AND_ASSIGN(LocationBarViewMac); 265 }; 266 267 #endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_ 268