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