Split //chrome-specific parts of ToolbarModel into ChromeToolbarModel
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / location_bar_view_mac.mm
blob864b405e68c755b3e63aacdaf78dbb8daff4ca4a
1 // Copyright 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 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/app/chrome_command_ids.h"
16 #import "chrome/browser/app_controller_mac.h"
17 #include "chrome/browser/command_updater.h"
18 #include "chrome/browser/defaults.h"
19 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
20 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
21 #include "chrome/browser/extensions/extension_action.h"
22 #include "chrome/browser/extensions/location_bar_controller.h"
23 #include "chrome/browser/extensions/tab_helper.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/search/instant_service.h"
26 #include "chrome/browser/search/instant_service_factory.h"
27 #include "chrome/browser/search/search.h"
28 #include "chrome/browser/search_engines/template_url_service_factory.h"
29 #include "chrome/browser/translate/chrome_translate_client.h"
30 #include "chrome/browser/translate/translate_service.h"
31 #include "chrome/browser/ui/browser_list.h"
32 #import "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h"
33 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
34 #import "chrome/browser/ui/cocoa/first_run_bubble_controller.h"
35 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
36 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
37 #import "chrome/browser/ui/cocoa/location_bar/content_setting_decoration.h"
38 #import "chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.h"
39 #import "chrome/browser/ui/cocoa/location_bar/generated_credit_card_decoration.h"
40 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h"
41 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h"
42 #import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
43 #import "chrome/browser/ui/cocoa/location_bar/mic_search_decoration.h"
44 #import "chrome/browser/ui/cocoa/location_bar/page_action_decoration.h"
45 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
46 #import "chrome/browser/ui/cocoa/location_bar/star_decoration.h"
47 #import "chrome/browser/ui/cocoa/location_bar/translate_decoration.h"
48 #import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
49 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
50 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
51 #include "chrome/browser/ui/content_settings/content_setting_image_model.h"
52 #import "chrome/browser/ui/omnibox/omnibox_popup_model.h"
53 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
54 #include "chrome/browser/ui/tabs/tab_strip_model.h"
55 #include "chrome/browser/ui/toolbar/chrome_toolbar_model.h"
56 #include "chrome/common/chrome_switches.h"
57 #include "chrome/common/pref_names.h"
58 #include "components/search_engines/template_url.h"
59 #include "components/search_engines/template_url_service.h"
60 #include "components/translate/core/browser/language_state.h"
61 #include "components/ui/zoom/zoom_controller.h"
62 #include "components/ui/zoom/zoom_event_manager.h"
63 #include "content/public/browser/web_contents.h"
64 #include "extensions/browser/extension_system.h"
65 #include "extensions/common/extension.h"
66 #include "grit/components_scaled_resources.h"
67 #include "grit/theme_resources.h"
68 #include "net/base/net_util.h"
69 #include "skia/ext/skia_utils_mac.h"
70 #import "ui/base/cocoa/cocoa_base_utils.h"
71 #include "ui/base/l10n/l10n_util_mac.h"
72 #include "ui/gfx/image/image.h"
74 using content::WebContents;
76 namespace {
78 // Vertical space between the bottom edge of the location_bar and the first run
79 // bubble arrow point.
80 const static int kFirstRunBubbleYOffset = 1;
82 }  // namespace
84 // TODO(shess): This code is mostly copied from the gtk
85 // implementation.  Make sure it's all appropriate and flesh it out.
87 LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field,
88                                        CommandUpdater* command_updater,
89                                        Profile* profile,
90                                        Browser* browser)
91     : LocationBar(profile),
92       OmniboxEditController(command_updater),
93       omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)),
94       field_(field),
95       location_icon_decoration_(new LocationIconDecoration(this)),
96       selected_keyword_decoration_(new SelectedKeywordDecoration()),
97       ev_bubble_decoration_(
98           new EVBubbleDecoration(location_icon_decoration_.get())),
99       star_decoration_(new StarDecoration(command_updater)),
100       translate_decoration_(new TranslateDecoration(command_updater)),
101       zoom_decoration_(new ZoomDecoration(this)),
102       keyword_hint_decoration_(new KeywordHintDecoration()),
103       mic_search_decoration_(new MicSearchDecoration(command_updater)),
104       generated_credit_card_decoration_(
105           new GeneratedCreditCardDecoration(this)),
106       manage_passwords_decoration_(
107           new ManagePasswordsDecoration(command_updater, this)),
108       browser_(browser),
109       weak_ptr_factory_(this) {
110   for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
111     DCHECK_EQ(i, content_setting_decorations_.size());
112     ContentSettingsType type = static_cast<ContentSettingsType>(i);
113     content_setting_decorations_.push_back(
114         new ContentSettingDecoration(type, this, profile));
115   }
117   edit_bookmarks_enabled_.Init(
118       bookmarks::prefs::kEditBookmarksEnabled, profile->GetPrefs(),
119       base::Bind(&LocationBarViewMac::OnEditBookmarksEnabledChanged,
120                  base::Unretained(this)));
122   browser_->search_model()->AddObserver(this);
124   ui_zoom::ZoomEventManager::GetForBrowserContext(profile)
125       ->AddZoomEventManagerObserver(this);
127   [[field_ cell] setIsPopupMode:
128       !browser->SupportsWindowFeature(Browser::FEATURE_TABSTRIP)];
130   // Sets images for the decorations, and performs a layout. This call ensures
131   // that this class is in a consistent state after initialization.
132   OnChanged();
135 LocationBarViewMac::~LocationBarViewMac() {
136   // Disconnect from cell in case it outlives us.
137   [[field_ cell] clearDecorations];
139   browser_->search_model()->RemoveObserver(this);
140   ui_zoom::ZoomEventManager::GetForBrowserContext(profile())
141       ->RemoveZoomEventManagerObserver(this);
144 void LocationBarViewMac::ShowFirstRunBubble() {
145   // We need the browser window to be shown before we can show the bubble, but
146   // we get called before that's happened.
147   base::MessageLoop::current()->PostTask(
148       FROM_HERE, base::Bind(&LocationBarViewMac::ShowFirstRunBubbleInternal,
149                             weak_ptr_factory_.GetWeakPtr()));
152 GURL LocationBarViewMac::GetDestinationURL() const {
153   return destination_url();
156 WindowOpenDisposition LocationBarViewMac::GetWindowOpenDisposition() const {
157   return disposition();
160 ui::PageTransition LocationBarViewMac::GetPageTransition() const {
161   return transition();
164 void LocationBarViewMac::AcceptInput() {
165   WindowOpenDisposition disposition =
166       ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
167   omnibox_view_->model()->AcceptInput(disposition, false);
170 void LocationBarViewMac::FocusLocation(bool select_all) {
171   omnibox_view_->FocusLocation(select_all);
174 void LocationBarViewMac::FocusSearch() {
175   omnibox_view_->SetForcedQuery();
178 void LocationBarViewMac::UpdateContentSettingsIcons() {
179   if (RefreshContentSettingsDecorations())
180     OnDecorationsChanged();
183 void LocationBarViewMac::UpdateManagePasswordsIconAndBubble() {
184   WebContents* web_contents = GetWebContents();
185   if (!web_contents)
186     return;
187   ManagePasswordsUIController::FromWebContents(web_contents)
188       ->UpdateIconAndBubbleState(manage_passwords_decoration_->icon());
189   OnDecorationsChanged();
192 void LocationBarViewMac::UpdatePageActions() {
193   RefreshPageActionDecorations();
194   Layout();
196   [field_ updateMouseTracking];
197   [field_ setNeedsDisplay:YES];
200 void LocationBarViewMac::UpdateBookmarkStarVisibility() {
201   star_decoration_->SetVisible(IsStarEnabled());
204 void LocationBarViewMac::UpdateLocationBarVisibility(bool visible,
205                                                      bool animate) {
206   // Not implemented on Mac.
209 bool LocationBarViewMac::ShowPageActionPopup(
210     const extensions::Extension* extension, bool grant_active_tab) {
211   for (ScopedVector<PageActionDecoration>::iterator iter =
212            page_action_decorations_.begin();
213        iter != page_action_decorations_.end(); ++iter) {
214     if ((*iter)->GetExtension() == extension)
215       return (*iter)->ActivatePageAction(grant_active_tab);
216   }
217   return false;
220 void LocationBarViewMac::UpdateOpenPDFInReaderPrompt() {
221   // Not implemented on Mac.
224 void LocationBarViewMac::UpdateGeneratedCreditCardView() {
225   generated_credit_card_decoration_->Update();
228 void LocationBarViewMac::SaveStateToContents(WebContents* contents) {
229   // TODO(shess): Why SaveStateToContents vs SaveStateToTab?
230   omnibox_view_->SaveStateToTab(contents);
233 void LocationBarViewMac::Revert() {
234   omnibox_view_->RevertAll();
237 const OmniboxView* LocationBarViewMac::GetOmniboxView() const {
238   return omnibox_view_.get();
241 OmniboxView* LocationBarViewMac::GetOmniboxView() {
242   return omnibox_view_.get();
245 LocationBarTesting* LocationBarViewMac::GetLocationBarForTesting() {
246   return this;
249 // TODO(pamg): Change all these, here and for other platforms, to size_t.
250 int LocationBarViewMac::PageActionCount() {
251   return static_cast<int>(page_action_decorations_.size());
254 int LocationBarViewMac::PageActionVisibleCount() {
255   int result = 0;
256   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
257     if (page_action_decorations_[i]->IsVisible())
258       ++result;
259   }
260   return result;
263 ExtensionAction* LocationBarViewMac::GetPageAction(size_t index) {
264   if (index < page_action_decorations_.size())
265     return page_action_decorations_[index]->GetPageAction();
266   NOTREACHED();
267   return NULL;
270 ExtensionAction* LocationBarViewMac::GetVisiblePageAction(size_t index) {
271   size_t current = 0;
272   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
273     if (page_action_decorations_[i]->IsVisible()) {
274       if (current == index)
275         return page_action_decorations_[i]->GetPageAction();
277       ++current;
278     }
279   }
281   NOTREACHED();
282   return NULL;
285 void LocationBarViewMac::TestPageActionPressed(size_t index) {
286   DCHECK_LT(index, static_cast<size_t>(PageActionVisibleCount()));
287   size_t current = 0;
288   for (PageActionDecoration* decoration : page_action_decorations_) {
289     if (decoration->IsVisible()) {
290       if (current == index) {
291         decoration->OnMousePressed(NSZeroRect, NSZeroPoint);
292         return;
293       }
294       ++current;
295     }
296   }
299 bool LocationBarViewMac::GetBookmarkStarVisibility() {
300   DCHECK(star_decoration_.get());
301   return star_decoration_->IsVisible();
304 void LocationBarViewMac::SetEditable(bool editable) {
305   [field_ setEditable:editable ? YES : NO];
306   UpdateBookmarkStarVisibility();
307   UpdateZoomDecoration(/*default_zoom_changed=*/false);
308   UpdatePageActions();
309   Layout();
312 bool LocationBarViewMac::IsEditable() {
313   return [field_ isEditable] ? true : false;
316 void LocationBarViewMac::SetStarred(bool starred) {
317   if (star_decoration_->starred() == starred)
318     return;
320   star_decoration_->SetStarred(starred);
321   UpdateBookmarkStarVisibility();
322   OnDecorationsChanged();
325 void LocationBarViewMac::SetTranslateIconLit(bool on) {
326   translate_decoration_->SetLit(on);
327   OnDecorationsChanged();
330 void LocationBarViewMac::ZoomChangedForActiveTab(bool can_show_bubble) {
331   bool changed = UpdateZoomDecoration(/*default_zoom_changed=*/false);
332   if (changed)
333     OnDecorationsChanged();
335   if (can_show_bubble && zoom_decoration_->IsVisible())
336     zoom_decoration_->ShowBubble(YES);
339 bool LocationBarViewMac::IsStarEnabled() const {
340   return browser_defaults::bookmarks_enabled &&
341          [field_ isEditable] &&
342          !GetToolbarModel()->input_in_progress() &&
343          edit_bookmarks_enabled_.GetValue() &&
344          !IsBookmarkStarHiddenByExtension();
347 NSPoint LocationBarViewMac::GetBookmarkBubblePoint() const {
348   DCHECK(IsStarEnabled());
349   return [field_ bubblePointForDecoration:star_decoration_.get()];
352 NSPoint LocationBarViewMac::GetTranslateBubblePoint() const {
353   return [field_ bubblePointForDecoration:translate_decoration_.get()];
356 NSPoint LocationBarViewMac::GetManagePasswordsBubblePoint() const {
357   return [field_ bubblePointForDecoration:manage_passwords_decoration_.get()];
360 NSPoint LocationBarViewMac::GetPageInfoBubblePoint() const {
361   if (ev_bubble_decoration_->IsVisible()) {
362     return [field_ bubblePointForDecoration:ev_bubble_decoration_.get()];
363   } else {
364     return [field_ bubblePointForDecoration:location_icon_decoration_.get()];
365   }
368 NSPoint LocationBarViewMac::GetGeneratedCreditCardBubblePoint() const {
369   return
370       [field_ bubblePointForDecoration:generated_credit_card_decoration_.get()];
373 void LocationBarViewMac::OnDecorationsChanged() {
374   // TODO(shess): The field-editor frame and cursor rects should not
375   // change, here.
376   [field_ updateMouseTracking];
377   [field_ resetFieldEditorFrameIfNeeded];
378   [field_ setNeedsDisplay:YES];
381 // TODO(shess): This function should over time grow to closely match
382 // the views Layout() function.
383 void LocationBarViewMac::Layout() {
384   AutocompleteTextFieldCell* cell = [field_ cell];
386   // Reset the left-hand decorations.
387   // TODO(shess): Shortly, this code will live somewhere else, like in
388   // the constructor.  I am still wrestling with how best to deal with
389   // right-hand decorations, which are not a static set.
390   [cell clearDecorations];
391   [cell addLeftDecoration:location_icon_decoration_.get()];
392   [cell addLeftDecoration:selected_keyword_decoration_.get()];
393   [cell addLeftDecoration:ev_bubble_decoration_.get()];
394   [cell addRightDecoration:star_decoration_.get()];
395   [cell addRightDecoration:translate_decoration_.get()];
396   [cell addRightDecoration:zoom_decoration_.get()];
397   [cell addRightDecoration:generated_credit_card_decoration_.get()];
398   [cell addRightDecoration:manage_passwords_decoration_.get()];
400   // Note that display order is right to left.
401   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
402     [cell addRightDecoration:page_action_decorations_[i]];
403   }
405   for (ScopedVector<ContentSettingDecoration>::iterator i =
406        content_setting_decorations_.begin();
407        i != content_setting_decorations_.end(); ++i) {
408     [cell addRightDecoration:*i];
409   }
411   [cell addRightDecoration:keyword_hint_decoration_.get()];
412   [cell addRightDecoration:mic_search_decoration_.get()];
414   // By default only the location icon is visible.
415   location_icon_decoration_->SetVisible(true);
416   selected_keyword_decoration_->SetVisible(false);
417   ev_bubble_decoration_->SetVisible(false);
418   keyword_hint_decoration_->SetVisible(false);
420   // Get the keyword to use for keyword-search and hinting.
421   const base::string16 keyword = omnibox_view_->model()->keyword();
422   base::string16 short_name;
423   bool is_extension_keyword = false;
424   if (!keyword.empty()) {
425     short_name = TemplateURLServiceFactory::GetForProfile(profile())->
426         GetKeywordShortName(keyword, &is_extension_keyword);
427   }
429   const bool is_keyword_hint = omnibox_view_->model()->is_keyword_hint();
430   ChromeToolbarModel* chrome_toolbar_model =
431       static_cast<ChromeToolbarModel*>(GetToolbarModel());
432   if (!keyword.empty() && !is_keyword_hint) {
433     // Switch from location icon to keyword mode.
434     location_icon_decoration_->SetVisible(false);
435     selected_keyword_decoration_->SetVisible(true);
436     selected_keyword_decoration_->SetKeyword(short_name, is_extension_keyword);
437     selected_keyword_decoration_->SetImage(GetKeywordImage(keyword));
438   } else if (chrome_toolbar_model->GetSecurityLevel(false) ==
439              connection_security::EV_SECURE) {
440     // Switch from location icon to show the EV bubble instead.
441     location_icon_decoration_->SetVisible(false);
442     ev_bubble_decoration_->SetVisible(true);
444     base::string16 label(GetToolbarModel()->GetEVCertName());
445     ev_bubble_decoration_->SetFullLabel(base::SysUTF16ToNSString(label));
446   } else if (!keyword.empty() && is_keyword_hint) {
447     keyword_hint_decoration_->SetKeyword(short_name,
448                                          is_extension_keyword);
449     keyword_hint_decoration_->SetVisible(true);
450   }
452   // These need to change anytime the layout changes.
453   // TODO(shess): Anytime the field editor might have changed, the
454   // cursor rects almost certainly should have changed.  The tooltips
455   // might change even when the rects don't change.
456   OnDecorationsChanged();
459 void LocationBarViewMac::RedrawDecoration(LocationBarDecoration* decoration) {
460   AutocompleteTextFieldCell* cell = [field_ cell];
461   NSRect frame = [cell frameForDecoration:decoration
462                                   inFrame:[field_ bounds]];
463   if (!NSIsEmptyRect(frame))
464     [field_ setNeedsDisplayInRect:frame];
467 void LocationBarViewMac::SetPreviewEnabledPageAction(
468     ExtensionAction* page_action, bool preview_enabled) {
469   DCHECK(page_action);
470   WebContents* contents = GetWebContents();
471   if (!contents)
472     return;
473   RefreshPageActionDecorations();
474   Layout();
476   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
477   DCHECK(decoration);
478   if (!decoration)
479     return;
481   decoration->set_preview_enabled(preview_enabled);
482   decoration->UpdateVisibility(contents);
485 NSRect LocationBarViewMac::GetPageActionFrame(ExtensionAction* page_action) {
486   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
487   if (!decoration)
488     return NSZeroRect;
490   AutocompleteTextFieldCell* cell = [field_ cell];
491   NSRect frame = [cell frameForDecoration:decoration inFrame:[field_ bounds]];
492   return frame;
495 NSPoint LocationBarViewMac::GetPageActionBubblePoint(
496     ExtensionAction* page_action) {
497   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
498   if (!decoration)
499     return NSZeroPoint;
501   NSRect frame = GetPageActionFrame(page_action);
502   if (NSIsEmptyRect(frame)) {
503     // The bubble point positioning assumes that the page action is visible. If
504     // not, something else needs to be done otherwise the bubble will appear
505     // near the top left corner (unanchored).
506     NOTREACHED();
507     return NSZeroPoint;
508   }
510   NSPoint bubble_point = decoration->GetBubblePointInFrame(frame);
511   return [field_ convertPoint:bubble_point toView:nil];
514 void LocationBarViewMac::ResetTabState(WebContents* contents) {
515   omnibox_view_->ResetTabState(contents);
518 void LocationBarViewMac::Update(const WebContents* contents) {
519   UpdateManagePasswordsIconAndBubble();
520   UpdateBookmarkStarVisibility();
521   UpdateTranslateDecoration();
522   UpdateZoomDecoration(/*default_zoom_changed=*/false);
523   RefreshPageActionDecorations();
524   RefreshContentSettingsDecorations();
525   UpdateMicSearchDecorationVisibility();
526   UpdateGeneratedCreditCardView();
527   if (contents)
528     omnibox_view_->OnTabChanged(contents);
529   else
530     omnibox_view_->Update();
531   OnChanged();
534 void LocationBarViewMac::UpdateWithoutTabRestore() {
535   Update(nullptr);
538 void LocationBarViewMac::OnChanged() {
539   // Update the location-bar icon.
540   const int resource_id = omnibox_view_->GetIcon();
541   NSImage* image = OmniboxViewMac::ImageForResource(resource_id);
542   location_icon_decoration_->SetImage(image);
543   ev_bubble_decoration_->SetImage(image);
544   Layout();
546   InstantService* instant_service =
547       InstantServiceFactory::GetForProfile(profile());
548   if (instant_service) {
549     gfx::Rect bounds(NSRectToCGRect([field_ frame]));
550     instant_service->OnOmniboxStartMarginChanged(bounds.x());
551   }
554 void LocationBarViewMac::OnSetFocus() {
555   // Update the keyword and search hint states.
556   OnChanged();
559 void LocationBarViewMac::ShowURL() {
560   omnibox_view_->ShowURL();
563 WebContents* LocationBarViewMac::GetWebContents() {
564   return browser_->tab_strip_model()->GetActiveWebContents();
567 ToolbarModel* LocationBarViewMac::GetToolbarModel() {
568   return browser_->toolbar_model();
571 const ToolbarModel* LocationBarViewMac::GetToolbarModel() const {
572   return browser_->toolbar_model();
575 NSImage* LocationBarViewMac::GetKeywordImage(const base::string16& keyword) {
576   const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
577       profile())->GetTemplateURLForKeyword(keyword);
578   if (template_url &&
579       (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
580     return extensions::OmniboxAPI::Get(profile())->
581         GetOmniboxIcon(template_url->GetExtensionId()).AsNSImage();
582   }
584   return OmniboxViewMac::ImageForResource(IDR_OMNIBOX_SEARCH);
587 void LocationBarViewMac::ModelChanged(const SearchModel::State& old_state,
588                                       const SearchModel::State& new_state) {
589   if (UpdateMicSearchDecorationVisibility())
590     Layout();
593 void LocationBarViewMac::PostNotification(NSString* notification) {
594   [[NSNotificationCenter defaultCenter] postNotificationName:notification
595                                         object:[NSValue valueWithPointer:this]];
598 PageActionDecoration* LocationBarViewMac::GetPageActionDecoration(
599     ExtensionAction* page_action) {
600   DCHECK(page_action);
601   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
602     if (page_action_decorations_[i]->GetPageAction() == page_action)
603       return page_action_decorations_[i];
604   }
605   // If |page_action| is the browser action of an extension, no element in
606   // |page_action_decorations_| will match.
607   NOTREACHED();
608   return NULL;
611 void LocationBarViewMac::DeletePageActionDecorations() {
612   // TODO(shess): Deleting these decorations could result in the cell
613   // refering to them before things are laid out again.  Meanwhile, at
614   // least fail safe.
615   [[field_ cell] clearDecorations];
617   page_action_decorations_.clear();
620 void LocationBarViewMac::OnEditBookmarksEnabledChanged() {
621   UpdateBookmarkStarVisibility();
622   OnChanged();
625 void LocationBarViewMac::RefreshPageActionDecorations() {
626   if (!IsEditable()) {
627     DeletePageActionDecorations();
628     return;
629   }
631   WebContents* web_contents = GetWebContents();
632   if (!web_contents) {
633     DeletePageActionDecorations();
634     return;
635   }
637   std::vector<ExtensionAction*> new_page_actions =
638       extensions::TabHelper::FromWebContents(web_contents)->
639           location_bar_controller()->GetCurrentActions();
641   if (PageActionsDiffer(new_page_actions)) {
642     DeletePageActionDecorations();
643     for (size_t i = 0; i < new_page_actions.size(); ++i) {
644       page_action_decorations_.push_back(
645           new PageActionDecoration(this, browser_, new_page_actions[i]));
646     }
647   }
649   GURL url = GetToolbarModel()->GetURL();
650   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
651     page_action_decorations_[i]->UpdateVisibility(
652         GetToolbarModel()->input_in_progress() ? NULL : web_contents);
653   }
656 bool LocationBarViewMac::PageActionsDiffer(
657     const std::vector<ExtensionAction*>& page_actions) const {
658   if (page_action_decorations_.size() != page_actions.size())
659     return true;
661   for (size_t index = 0; index < page_actions.size(); ++index) {
662     PageActionDecoration* decoration = page_action_decorations_[index];
663     if (decoration->GetPageAction() != page_actions[index])
664       return true;
665   }
667   return false;
670 bool LocationBarViewMac::RefreshContentSettingsDecorations() {
671   const bool input_in_progress = GetToolbarModel()->input_in_progress();
672   WebContents* web_contents = input_in_progress ?
673       NULL : browser_->tab_strip_model()->GetActiveWebContents();
674   bool icons_updated = false;
675   for (size_t i = 0; i < content_setting_decorations_.size(); ++i) {
676     icons_updated |=
677         content_setting_decorations_[i]->UpdateFromWebContents(web_contents);
678   }
679   return icons_updated;
682 void LocationBarViewMac::ShowFirstRunBubbleInternal() {
683   if (!field_ || ![field_ window])
684     return;
686   // The first run bubble's left edge should line up with the left edge of the
687   // omnibox. This is different from other bubbles, which line up at a point
688   // set by their top arrow. Because the BaseBubbleController adjusts the
689   // window origin left to account for the arrow spacing, the first run bubble
690   // moves the window origin right by this spacing, so that the
691   // BaseBubbleController will move it back to the correct position.
692   const NSPoint kOffset = NSMakePoint(
693       info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth/2.0,
694       kFirstRunBubbleYOffset);
695   [FirstRunBubbleController showForView:field_
696                                  offset:kOffset
697                                 browser:browser_
698                                 profile:profile()];
701 void LocationBarViewMac::UpdateTranslateDecoration() {
702   if (!TranslateService::IsTranslateBubbleEnabled())
703     return;
705   WebContents* web_contents = GetWebContents();
706   if (!web_contents)
707     return;
708   translate::LanguageState& language_state =
709       ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
710   bool enabled = language_state.translate_enabled();
711   command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled);
712   translate_decoration_->SetVisible(enabled);
713   translate_decoration_->SetLit(language_state.IsPageTranslated());
716 bool LocationBarViewMac::UpdateZoomDecoration(bool default_zoom_changed) {
717   WebContents* web_contents = GetWebContents();
718   if (!web_contents)
719     return false;
721   return zoom_decoration_->UpdateIfNecessary(
722       ui_zoom::ZoomController::FromWebContents(web_contents),
723       default_zoom_changed);
726 void LocationBarViewMac::OnDefaultZoomLevelChanged() {
727   if (UpdateZoomDecoration(/*default_zoom_changed=*/true))
728     OnDecorationsChanged();
731 bool LocationBarViewMac::UpdateMicSearchDecorationVisibility() {
732   bool is_visible = !GetToolbarModel()->input_in_progress() &&
733                     browser_->search_model()->voice_search_supported();
734   if (mic_search_decoration_->IsVisible() == is_visible)
735     return false;
736   mic_search_decoration_->SetVisible(is_visible);
737   return true;