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_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_
6 #define CHROME_BROWSER_UI_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_
10 #import <Cocoa/Cocoa.h>
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/chrome_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 #include "components/ui/zoom/zoom_event_manager_observer.h"
24 @
class AutocompleteTextField
;
26 class ContentSettingDecoration
;
27 class EVBubbleDecoration
;
28 class KeywordHintDecoration
;
29 class LocationBarDecoration
;
30 class LocationIconDecoration
;
31 class ManagePasswordsDecoration
;
32 class MicSearchDecoration
;
33 class PageActionDecoration
;
35 class SelectedKeywordDecoration
;
37 class TranslateDecoration
;
39 class ZoomDecorationTest
;
41 // A C++ bridge class that represents the location bar UI element to
42 // the portable code. Wires up an OmniboxViewMac instance to
43 // the location bar text field, which handles most of the work.
45 class LocationBarViewMac
: public LocationBar
,
46 public LocationBarTesting
,
47 public ChromeOmniboxEditController
,
48 public SearchModelObserver
,
49 public ui_zoom::ZoomEventManagerObserver
{
51 LocationBarViewMac(AutocompleteTextField
* field
,
52 CommandUpdater
* command_updater
,
55 ~LocationBarViewMac() override
;
57 // Overridden from LocationBar:
58 void ShowFirstRunBubble() override
;
59 GURL
GetDestinationURL() const override
;
60 WindowOpenDisposition
GetWindowOpenDisposition() const override
;
61 ui::PageTransition
GetPageTransition() const override
;
62 void AcceptInput() override
;
63 void FocusLocation(bool select_all
) override
;
64 void FocusSearch() override
;
65 void UpdateContentSettingsIcons() override
;
66 void UpdateManagePasswordsIconAndBubble() override
;
67 void UpdatePageActions() override
;
68 void UpdateBookmarkStarVisibility() override
;
69 void UpdateLocationBarVisibility(bool visible
, bool animate
) override
;
70 bool ShowPageActionPopup(const extensions::Extension
* extension
,
71 bool grant_active_tab
) override
;
72 void UpdateOpenPDFInReaderPrompt() override
;
73 void SaveStateToContents(content::WebContents
* contents
) override
;
74 void Revert() override
;
75 const OmniboxView
* GetOmniboxView() const override
;
76 OmniboxView
* GetOmniboxView() override
;
77 LocationBarTesting
* GetLocationBarForTesting() override
;
79 // Overridden from LocationBarTesting:
80 int PageActionCount() override
;
81 int PageActionVisibleCount() override
;
82 ExtensionAction
* GetPageAction(size_t index
) override
;
83 ExtensionAction
* GetVisiblePageAction(size_t index
) override
;
84 void TestPageActionPressed(size_t index
) override
;
85 bool GetBookmarkStarVisibility() override
;
87 // Set/Get the editable state of the field.
88 void SetEditable(bool editable
);
91 // Set the starred state of the bookmark star.
92 void SetStarred(bool starred
);
94 // Set whether or not the translate icon is lit.
95 void SetTranslateIconLit(bool on
);
97 // Happens when the zoom changes for the active tab. |can_show_bubble| is
98 // false when the change in zoom for the active tab wasn't an explicit user
99 // action (e.g. switching tabs, creating a new tab, creating a new browser).
100 // Additionally, |can_show_bubble| will only be true when the bubble wouldn't
101 // be obscured by other UI (wrench menu) or redundant (+/- from wrench).
102 void ZoomChangedForActiveTab(bool can_show_bubble
);
104 // Checks if the bookmark star should be enabled or not.
105 bool IsStarEnabled() const;
107 // Get the point in window coordinates on the star for the bookmark bubble to
108 // aim at. Only works if IsStarEnabled returns YES.
109 NSPoint
GetBookmarkBubblePoint() const;
111 // Get the point in window coordinates on the star for the Translate bubble to
113 NSPoint
GetTranslateBubblePoint() const;
115 // Get the point in window coordinates in the lock icon for the Manage
116 // Passwords bubble to aim at.
117 NSPoint
GetManagePasswordsBubblePoint() const;
119 // Get the point in window coordinates in the security icon at which the page
121 NSPoint
GetPageInfoBubblePoint() const;
123 // When any image decorations change, call this to ensure everything is
124 // redrawn and laid out if necessary.
125 void OnDecorationsChanged();
127 // Layout the various decorations which live in the field.
130 // Re-draws |decoration| if it's already being displayed.
131 void RedrawDecoration(LocationBarDecoration
* decoration
);
133 // Sets preview_enabled_ for the PageActionImageView associated with this
134 // |page_action|. If |preview_enabled|, the location bar will display the
135 // PageAction icon even if it has not been activated by the extension.
136 // This is used by the ExtensionInstalledBubble to preview what the icon
137 // will look like for the user upon installation of the extension.
138 void SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
139 bool preview_enabled
);
141 // Retrieve the frame for the given |page_action|.
142 NSRect
GetPageActionFrame(ExtensionAction
* page_action
);
144 // Return |page_action|'s info-bubble point in window coordinates.
145 // This function should always be called with a visible page action.
146 // If |page_action| is not a page action or not visible, NOTREACHED()
147 // is called and this function returns |NSZeroPoint|.
148 NSPoint
GetPageActionBubblePoint(ExtensionAction
* page_action
);
150 // Updates the controller, and, if |contents| is non-null, restores saved
151 // state that the tab holds.
152 void Update(const content::WebContents
* contents
);
154 // Clears any location bar state stored for |contents|.
155 void ResetTabState(content::WebContents
* contents
);
157 // ChromeOmniboxEditController:
158 void UpdateWithoutTabRestore() override
;
159 void OnChanged() override
;
160 void OnSetFocus() override
;
161 void ShowURL() override
;
162 ToolbarModel
* GetToolbarModel() override
;
163 const ToolbarModel
* GetToolbarModel() const override
;
164 content::WebContents
* GetWebContents() override
;
166 NSImage
* GetKeywordImage(const base::string16
& keyword
);
168 AutocompleteTextField
* GetAutocompleteTextField() { return field_
; }
170 ManagePasswordsDecoration
* manage_passwords_decoration() {
171 return manage_passwords_decoration_
.get();
174 // SearchModelObserver:
175 void ModelChanged(const SearchModel::State
& old_state
,
176 const SearchModel::State
& new_state
) override
;
178 Browser
* browser() const { return browser_
; }
180 // ZoomManagerObserver:
181 // Updates the view for the zoom icon when default zoom levels change.
182 void OnDefaultZoomLevelChanged() override
;
185 friend ZoomDecorationTest
;
187 // Posts |notification| to the default notification center.
188 void PostNotification(NSString
* notification
);
190 // Return the decoration for |page_action|.
191 PageActionDecoration
* GetPageActionDecoration(ExtensionAction
* page_action
);
193 // Clear the page-action decorations.
194 void DeletePageActionDecorations();
196 void OnEditBookmarksEnabledChanged();
198 // Re-generate the page-action decorations from the profile's
199 // extension service.
200 void RefreshPageActionDecorations();
202 // Whether the page actions represented by |page_action_decorations_| differ
203 // in ordering or value from |page_actions|.
204 bool PageActionsDiffer(
205 const std::vector
<ExtensionAction
*>& page_actions
) const;
207 // Updates visibility of the content settings icons based on the current
208 // tab contents state.
209 bool RefreshContentSettingsDecorations();
211 void ShowFirstRunBubbleInternal();
213 // Updates the translate decoration in the omnibox with the current translate
215 void UpdateTranslateDecoration();
217 // Updates the zoom decoration in the omnibox with the current zoom level.
218 // Returns whether any updates were made.
219 bool UpdateZoomDecoration(bool default_zoom_changed
);
221 // Updates the voice search decoration. Returns true if the visible state was
223 bool UpdateMicSearchDecorationVisibility();
225 scoped_ptr
<OmniboxViewMac
> omnibox_view_
;
227 AutocompleteTextField
* field_
; // owned by tab controller
229 // A decoration that shows an icon to the left of the address.
230 scoped_ptr
<LocationIconDecoration
> location_icon_decoration_
;
232 // A decoration that shows the keyword-search bubble on the left.
233 scoped_ptr
<SelectedKeywordDecoration
> selected_keyword_decoration_
;
235 // A decoration that shows a lock icon and ev-cert label in a bubble
237 scoped_ptr
<EVBubbleDecoration
> ev_bubble_decoration_
;
239 // Bookmark star right of page actions.
240 scoped_ptr
<StarDecoration
> star_decoration_
;
242 // Translate icon at the end of the ominibox.
243 scoped_ptr
<TranslateDecoration
> translate_decoration_
;
245 // A zoom icon at the end of the omnibox, which shows at non-standard zoom
247 scoped_ptr
<ZoomDecoration
> zoom_decoration_
;
249 // Decorations for the installed Page Actions.
250 ScopedVector
<PageActionDecoration
> page_action_decorations_
;
252 // The content blocked decorations.
253 ScopedVector
<ContentSettingDecoration
> content_setting_decorations_
;
255 // Keyword hint decoration displayed on the right-hand side.
256 scoped_ptr
<KeywordHintDecoration
> keyword_hint_decoration_
;
258 // The voice search icon.
259 scoped_ptr
<MicSearchDecoration
> mic_search_decoration_
;
261 // The right-hand-side button to manage passwords associated with a page.
262 scoped_ptr
<ManagePasswordsDecoration
> manage_passwords_decoration_
;
266 // Used to change the visibility of the star decoration.
267 BooleanPrefMember edit_bookmarks_enabled_
;
269 // Used to schedule a task for the first run info bubble.
270 base::WeakPtrFactory
<LocationBarViewMac
> weak_ptr_factory_
;
272 DISALLOW_COPY_AND_ASSIGN(LocationBarViewMac
);
275 #endif // CHROME_BROWSER_UI_COCOA_LOCATION_BAR_LOCATION_BAR_VIEW_MAC_H_