Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / location_bar / location_bar_view_mac.mm
blob3acfa7733713191ac0d7cacc4ae9b535b62da352
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_instant_controller.h"
32 #include "chrome/browser/ui/browser_list.h"
33 #import "chrome/browser/ui/cocoa/content_settings/content_setting_bubble_cocoa.h"
34 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h"
35 #import "chrome/browser/ui/cocoa/first_run_bubble_controller.h"
36 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h"
37 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field_cell.h"
38 #import "chrome/browser/ui/cocoa/location_bar/content_setting_decoration.h"
39 #import "chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.h"
40 #import "chrome/browser/ui/cocoa/location_bar/generated_credit_card_decoration.h"
41 #import "chrome/browser/ui/cocoa/location_bar/keyword_hint_decoration.h"
42 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h"
43 #import "chrome/browser/ui/cocoa/location_bar/manage_passwords_decoration.h"
44 #import "chrome/browser/ui/cocoa/location_bar/mic_search_decoration.h"
45 #import "chrome/browser/ui/cocoa/location_bar/page_action_decoration.h"
46 #import "chrome/browser/ui/cocoa/location_bar/selected_keyword_decoration.h"
47 #import "chrome/browser/ui/cocoa/location_bar/star_decoration.h"
48 #import "chrome/browser/ui/cocoa/location_bar/translate_decoration.h"
49 #import "chrome/browser/ui/cocoa/location_bar/zoom_decoration.h"
50 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
51 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
52 #include "chrome/browser/ui/content_settings/content_setting_image_model.h"
53 #import "chrome/browser/ui/omnibox/omnibox_popup_model.h"
54 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
55 #include "chrome/browser/ui/tabs/tab_strip_model.h"
56 #include "chrome/browser/ui/toolbar/toolbar_model.h"
57 #include "chrome/common/chrome_switches.h"
58 #include "chrome/common/pref_names.h"
59 #include "components/search_engines/template_url.h"
60 #include "components/search_engines/template_url_service.h"
61 #include "components/translate/core/browser/language_state.h"
62 #include "components/ui/zoom/zoom_controller.h"
63 #include "components/ui/zoom/zoom_event_manager.h"
64 #include "content/public/browser/web_contents.h"
65 #include "extensions/browser/extension_system.h"
66 #include "extensions/common/extension.h"
67 #include "grit/components_scaled_resources.h"
68 #include "grit/theme_resources.h"
69 #include "net/base/net_util.h"
70 #include "skia/ext/skia_utils_mac.h"
71 #import "ui/base/cocoa/cocoa_base_utils.h"
72 #include "ui/base/l10n/l10n_util_mac.h"
73 #include "ui/gfx/image/image.h"
75 using content::WebContents;
77 namespace {
79 // Vertical space between the bottom edge of the location_bar and the first run
80 // bubble arrow point.
81 const static int kFirstRunBubbleYOffset = 1;
83 }  // namespace
85 // TODO(shess): This code is mostly copied from the gtk
86 // implementation.  Make sure it's all appropriate and flesh it out.
88 LocationBarViewMac::LocationBarViewMac(AutocompleteTextField* field,
89                                        CommandUpdater* command_updater,
90                                        Profile* profile,
91                                        Browser* browser)
92     : LocationBar(profile),
93       OmniboxEditController(command_updater),
94       omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)),
95       field_(field),
96       location_icon_decoration_(new LocationIconDecoration(this)),
97       selected_keyword_decoration_(new SelectedKeywordDecoration()),
98       ev_bubble_decoration_(
99           new EVBubbleDecoration(location_icon_decoration_.get())),
100       star_decoration_(new StarDecoration(command_updater)),
101       translate_decoration_(new TranslateDecoration(command_updater)),
102       zoom_decoration_(new ZoomDecoration(this)),
103       keyword_hint_decoration_(new KeywordHintDecoration()),
104       mic_search_decoration_(new MicSearchDecoration(command_updater)),
105       generated_credit_card_decoration_(
106           new GeneratedCreditCardDecoration(this)),
107       manage_passwords_decoration_(
108           new ManagePasswordsDecoration(command_updater, this)),
109       browser_(browser),
110       weak_ptr_factory_(this) {
111   for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
112     DCHECK_EQ(i, content_setting_decorations_.size());
113     ContentSettingsType type = static_cast<ContentSettingsType>(i);
114     content_setting_decorations_.push_back(
115         new ContentSettingDecoration(type, this, profile));
116   }
118   edit_bookmarks_enabled_.Init(
119       bookmarks::prefs::kEditBookmarksEnabled, profile->GetPrefs(),
120       base::Bind(&LocationBarViewMac::OnEditBookmarksEnabledChanged,
121                  base::Unretained(this)));
123   browser_->search_model()->AddObserver(this);
125   ui_zoom::ZoomEventManager::GetForBrowserContext(profile)
126       ->AddZoomEventManagerObserver(this);
128   [[field_ cell] setIsPopupMode:
129       !browser->SupportsWindowFeature(Browser::FEATURE_TABSTRIP)];
131   // Sets images for the decorations, and performs a layout. This call ensures
132   // that this class is in a consistent state after initialization.
133   OnChanged();
136 LocationBarViewMac::~LocationBarViewMac() {
137   // Disconnect from cell in case it outlives us.
138   [[field_ cell] clearDecorations];
140   browser_->search_model()->RemoveObserver(this);
141   ui_zoom::ZoomEventManager::GetForBrowserContext(profile())
142       ->RemoveZoomEventManagerObserver(this);
145 void LocationBarViewMac::ShowFirstRunBubble() {
146   // We need the browser window to be shown before we can show the bubble, but
147   // we get called before that's happened.
148   base::MessageLoop::current()->PostTask(
149       FROM_HERE, base::Bind(&LocationBarViewMac::ShowFirstRunBubbleInternal,
150                             weak_ptr_factory_.GetWeakPtr()));
153 GURL LocationBarViewMac::GetDestinationURL() const {
154   return destination_url();
157 WindowOpenDisposition LocationBarViewMac::GetWindowOpenDisposition() const {
158   return disposition();
161 ui::PageTransition LocationBarViewMac::GetPageTransition() const {
162   return transition();
165 void LocationBarViewMac::AcceptInput() {
166   WindowOpenDisposition disposition =
167       ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
168   omnibox_view_->model()->AcceptInput(disposition, false);
171 void LocationBarViewMac::FocusLocation(bool select_all) {
172   omnibox_view_->FocusLocation(select_all);
175 void LocationBarViewMac::FocusSearch() {
176   omnibox_view_->SetForcedQuery();
179 void LocationBarViewMac::UpdateContentSettingsIcons() {
180   if (RefreshContentSettingsDecorations())
181     OnDecorationsChanged();
184 void LocationBarViewMac::UpdateManagePasswordsIconAndBubble() {
185   WebContents* web_contents = GetWebContents();
186   if (!web_contents)
187     return;
188   ManagePasswordsUIController::FromWebContents(web_contents)
189       ->UpdateIconAndBubbleState(manage_passwords_decoration_->icon());
190   OnDecorationsChanged();
193 void LocationBarViewMac::UpdatePageActions() {
194   RefreshPageActionDecorations();
195   Layout();
197   [field_ updateMouseTracking];
198   [field_ setNeedsDisplay:YES];
201 void LocationBarViewMac::UpdateBookmarkStarVisibility() {
202   star_decoration_->SetVisible(IsStarEnabled());
205 bool LocationBarViewMac::ShowPageActionPopup(
206     const extensions::Extension* extension, bool grant_active_tab) {
207   for (ScopedVector<PageActionDecoration>::iterator iter =
208            page_action_decorations_.begin();
209        iter != page_action_decorations_.end(); ++iter) {
210     if ((*iter)->GetExtension() == extension)
211       return (*iter)->ActivatePageAction(grant_active_tab);
212   }
213   return false;
216 void LocationBarViewMac::UpdateOpenPDFInReaderPrompt() {
217   // Not implemented on Mac.
220 void LocationBarViewMac::UpdateGeneratedCreditCardView() {
221   generated_credit_card_decoration_->Update();
224 void LocationBarViewMac::SaveStateToContents(WebContents* contents) {
225   // TODO(shess): Why SaveStateToContents vs SaveStateToTab?
226   omnibox_view_->SaveStateToTab(contents);
229 void LocationBarViewMac::Revert() {
230   omnibox_view_->RevertAll();
233 const OmniboxView* LocationBarViewMac::GetOmniboxView() const {
234   return omnibox_view_.get();
237 OmniboxView* LocationBarViewMac::GetOmniboxView() {
238   return omnibox_view_.get();
241 LocationBarTesting* LocationBarViewMac::GetLocationBarForTesting() {
242   return this;
245 // TODO(pamg): Change all these, here and for other platforms, to size_t.
246 int LocationBarViewMac::PageActionCount() {
247   return static_cast<int>(page_action_decorations_.size());
250 int LocationBarViewMac::PageActionVisibleCount() {
251   int result = 0;
252   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
253     if (page_action_decorations_[i]->IsVisible())
254       ++result;
255   }
256   return result;
259 ExtensionAction* LocationBarViewMac::GetPageAction(size_t index) {
260   if (index < page_action_decorations_.size())
261     return page_action_decorations_[index]->GetPageAction();
262   NOTREACHED();
263   return NULL;
266 ExtensionAction* LocationBarViewMac::GetVisiblePageAction(size_t index) {
267   size_t current = 0;
268   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
269     if (page_action_decorations_[i]->IsVisible()) {
270       if (current == index)
271         return page_action_decorations_[i]->GetPageAction();
273       ++current;
274     }
275   }
277   NOTREACHED();
278   return NULL;
281 void LocationBarViewMac::TestPageActionPressed(size_t index) {
282   DCHECK_LT(index, static_cast<size_t>(PageActionVisibleCount()));
283   size_t current = 0;
284   for (PageActionDecoration* decoration : page_action_decorations_) {
285     if (decoration->IsVisible()) {
286       if (current == index) {
287         decoration->OnMousePressed(NSZeroRect, NSZeroPoint);
288         return;
289       }
290       ++current;
291     }
292   }
295 bool LocationBarViewMac::GetBookmarkStarVisibility() {
296   DCHECK(star_decoration_.get());
297   return star_decoration_->IsVisible();
300 void LocationBarViewMac::SetEditable(bool editable) {
301   [field_ setEditable:editable ? YES : NO];
302   UpdateBookmarkStarVisibility();
303   UpdateZoomDecoration(/*default_zoom_changed=*/false);
304   UpdatePageActions();
305   Layout();
308 bool LocationBarViewMac::IsEditable() {
309   return [field_ isEditable] ? true : false;
312 void LocationBarViewMac::SetStarred(bool starred) {
313   if (star_decoration_->starred() == starred)
314     return;
316   star_decoration_->SetStarred(starred);
317   UpdateBookmarkStarVisibility();
318   OnDecorationsChanged();
321 void LocationBarViewMac::SetTranslateIconLit(bool on) {
322   translate_decoration_->SetLit(on);
323   OnDecorationsChanged();
326 void LocationBarViewMac::ZoomChangedForActiveTab(bool can_show_bubble) {
327   bool changed = UpdateZoomDecoration(/*default_zoom_changed=*/false);
328   if (changed)
329     OnDecorationsChanged();
331   if (can_show_bubble && zoom_decoration_->IsVisible())
332     zoom_decoration_->ShowBubble(YES);
335 bool LocationBarViewMac::IsStarEnabled() const {
336   return browser_defaults::bookmarks_enabled &&
337          [field_ isEditable] &&
338          !GetToolbarModel()->input_in_progress() &&
339          edit_bookmarks_enabled_.GetValue() &&
340          !IsBookmarkStarHiddenByExtension();
343 NSPoint LocationBarViewMac::GetBookmarkBubblePoint() const {
344   DCHECK(IsStarEnabled());
345   return [field_ bubblePointForDecoration:star_decoration_.get()];
348 NSPoint LocationBarViewMac::GetTranslateBubblePoint() const {
349   return [field_ bubblePointForDecoration:translate_decoration_.get()];
352 NSPoint LocationBarViewMac::GetManagePasswordsBubblePoint() const {
353   return [field_ bubblePointForDecoration:manage_passwords_decoration_.get()];
356 NSPoint LocationBarViewMac::GetPageInfoBubblePoint() const {
357   if (ev_bubble_decoration_->IsVisible()) {
358     return [field_ bubblePointForDecoration:ev_bubble_decoration_.get()];
359   } else {
360     return [field_ bubblePointForDecoration:location_icon_decoration_.get()];
361   }
364 NSPoint LocationBarViewMac::GetGeneratedCreditCardBubblePoint() const {
365   return
366       [field_ bubblePointForDecoration:generated_credit_card_decoration_.get()];
369 void LocationBarViewMac::OnDecorationsChanged() {
370   // TODO(shess): The field-editor frame and cursor rects should not
371   // change, here.
372   [field_ updateMouseTracking];
373   [field_ resetFieldEditorFrameIfNeeded];
374   [field_ setNeedsDisplay:YES];
377 // TODO(shess): This function should over time grow to closely match
378 // the views Layout() function.
379 void LocationBarViewMac::Layout() {
380   AutocompleteTextFieldCell* cell = [field_ cell];
382   // Reset the left-hand decorations.
383   // TODO(shess): Shortly, this code will live somewhere else, like in
384   // the constructor.  I am still wrestling with how best to deal with
385   // right-hand decorations, which are not a static set.
386   [cell clearDecorations];
387   [cell addLeftDecoration:location_icon_decoration_.get()];
388   [cell addLeftDecoration:selected_keyword_decoration_.get()];
389   [cell addLeftDecoration:ev_bubble_decoration_.get()];
390   [cell addRightDecoration:star_decoration_.get()];
391   [cell addRightDecoration:translate_decoration_.get()];
392   [cell addRightDecoration:zoom_decoration_.get()];
393   [cell addRightDecoration:generated_credit_card_decoration_.get()];
394   [cell addRightDecoration:manage_passwords_decoration_.get()];
396   // Note that display order is right to left.
397   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
398     [cell addRightDecoration:page_action_decorations_[i]];
399   }
401   for (ScopedVector<ContentSettingDecoration>::iterator i =
402        content_setting_decorations_.begin();
403        i != content_setting_decorations_.end(); ++i) {
404     [cell addRightDecoration:*i];
405   }
407   [cell addRightDecoration:keyword_hint_decoration_.get()];
408   [cell addRightDecoration:mic_search_decoration_.get()];
410   // By default only the location icon is visible.
411   location_icon_decoration_->SetVisible(true);
412   selected_keyword_decoration_->SetVisible(false);
413   ev_bubble_decoration_->SetVisible(false);
414   keyword_hint_decoration_->SetVisible(false);
416   // Get the keyword to use for keyword-search and hinting.
417   const base::string16 keyword = omnibox_view_->model()->keyword();
418   base::string16 short_name;
419   bool is_extension_keyword = false;
420   if (!keyword.empty()) {
421     short_name = TemplateURLServiceFactory::GetForProfile(profile())->
422         GetKeywordShortName(keyword, &is_extension_keyword);
423   }
425   const bool is_keyword_hint = omnibox_view_->model()->is_keyword_hint();
426   if (!keyword.empty() && !is_keyword_hint) {
427     // Switch from location icon to keyword mode.
428     location_icon_decoration_->SetVisible(false);
429     selected_keyword_decoration_->SetVisible(true);
430     selected_keyword_decoration_->SetKeyword(short_name, is_extension_keyword);
431     selected_keyword_decoration_->SetImage(GetKeywordImage(keyword));
432   } else if ((GetToolbarModel()->GetSecurityLevel(false) ==
433               ToolbarModel::EV_SECURE)) {
434     // Switch from location icon to show the EV bubble instead.
435     location_icon_decoration_->SetVisible(false);
436     ev_bubble_decoration_->SetVisible(true);
438     base::string16 label(GetToolbarModel()->GetEVCertName());
439     ev_bubble_decoration_->SetFullLabel(base::SysUTF16ToNSString(label));
440   } else if (!keyword.empty() && is_keyword_hint) {
441     keyword_hint_decoration_->SetKeyword(short_name,
442                                          is_extension_keyword);
443     keyword_hint_decoration_->SetVisible(true);
444   }
446   // These need to change anytime the layout changes.
447   // TODO(shess): Anytime the field editor might have changed, the
448   // cursor rects almost certainly should have changed.  The tooltips
449   // might change even when the rects don't change.
450   OnDecorationsChanged();
453 void LocationBarViewMac::RedrawDecoration(LocationBarDecoration* decoration) {
454   AutocompleteTextFieldCell* cell = [field_ cell];
455   NSRect frame = [cell frameForDecoration:decoration
456                                   inFrame:[field_ bounds]];
457   if (!NSIsEmptyRect(frame))
458     [field_ setNeedsDisplayInRect:frame];
461 void LocationBarViewMac::SetPreviewEnabledPageAction(
462     ExtensionAction* page_action, bool preview_enabled) {
463   DCHECK(page_action);
464   WebContents* contents = GetWebContents();
465   if (!contents)
466     return;
467   RefreshPageActionDecorations();
468   Layout();
470   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
471   DCHECK(decoration);
472   if (!decoration)
473     return;
475   decoration->set_preview_enabled(preview_enabled);
476   decoration->UpdateVisibility(contents);
479 NSRect LocationBarViewMac::GetPageActionFrame(ExtensionAction* page_action) {
480   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
481   if (!decoration)
482     return NSZeroRect;
484   AutocompleteTextFieldCell* cell = [field_ cell];
485   NSRect frame = [cell frameForDecoration:decoration inFrame:[field_ bounds]];
486   return frame;
489 NSPoint LocationBarViewMac::GetPageActionBubblePoint(
490     ExtensionAction* page_action) {
491   PageActionDecoration* decoration = GetPageActionDecoration(page_action);
492   if (!decoration)
493     return NSZeroPoint;
495   NSRect frame = GetPageActionFrame(page_action);
496   if (NSIsEmptyRect(frame)) {
497     // The bubble point positioning assumes that the page action is visible. If
498     // not, something else needs to be done otherwise the bubble will appear
499     // near the top left corner (unanchored).
500     NOTREACHED();
501     return NSZeroPoint;
502   }
504   NSPoint bubble_point = decoration->GetBubblePointInFrame(frame);
505   return [field_ convertPoint:bubble_point toView:nil];
508 void LocationBarViewMac::ResetTabState(WebContents* contents) {
509   omnibox_view_->ResetTabState(contents);
512 void LocationBarViewMac::Update(const WebContents* contents) {
513   UpdateManagePasswordsIconAndBubble();
514   UpdateBookmarkStarVisibility();
515   UpdateTranslateDecoration();
516   UpdateZoomDecoration(/*default_zoom_changed=*/false);
517   RefreshPageActionDecorations();
518   RefreshContentSettingsDecorations();
519   UpdateMicSearchDecorationVisibility();
520   UpdateGeneratedCreditCardView();
521   if (contents)
522     omnibox_view_->OnTabChanged(contents);
523   else
524     omnibox_view_->Update();
525   OnChanged();
528 void LocationBarViewMac::OnChanged() {
529   // Update the location-bar icon.
530   const int resource_id = omnibox_view_->GetIcon();
531   NSImage* image = OmniboxViewMac::ImageForResource(resource_id);
532   location_icon_decoration_->SetImage(image);
533   ev_bubble_decoration_->SetImage(image);
534   Layout();
536   InstantService* instant_service =
537       InstantServiceFactory::GetForProfile(profile());
538   if (instant_service) {
539     gfx::Rect bounds(NSRectToCGRect([field_ frame]));
540     instant_service->OnOmniboxStartMarginChanged(bounds.x());
541   }
544 void LocationBarViewMac::OnSetFocus() {
545   // Update the keyword and search hint states.
546   OnChanged();
549 void LocationBarViewMac::ShowURL() {
550   omnibox_view_->ShowURL();
553 InstantController* LocationBarViewMac::GetInstant() {
554   return browser_->instant_controller() ?
555       browser_->instant_controller()->instant() : NULL;
558 WebContents* LocationBarViewMac::GetWebContents() {
559   return browser_->tab_strip_model()->GetActiveWebContents();
562 ToolbarModel* LocationBarViewMac::GetToolbarModel() {
563   return browser_->toolbar_model();
566 const ToolbarModel* LocationBarViewMac::GetToolbarModel() const {
567   return browser_->toolbar_model();
570 NSImage* LocationBarViewMac::GetKeywordImage(const base::string16& keyword) {
571   const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
572       profile())->GetTemplateURLForKeyword(keyword);
573   if (template_url &&
574       (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
575     return extensions::OmniboxAPI::Get(profile())->
576         GetOmniboxIcon(template_url->GetExtensionId()).AsNSImage();
577   }
579   return OmniboxViewMac::ImageForResource(IDR_OMNIBOX_SEARCH);
582 void LocationBarViewMac::ModelChanged(const SearchModel::State& old_state,
583                                       const SearchModel::State& new_state) {
584   if (UpdateMicSearchDecorationVisibility())
585     Layout();
588 void LocationBarViewMac::PostNotification(NSString* notification) {
589   [[NSNotificationCenter defaultCenter] postNotificationName:notification
590                                         object:[NSValue valueWithPointer:this]];
593 PageActionDecoration* LocationBarViewMac::GetPageActionDecoration(
594     ExtensionAction* page_action) {
595   DCHECK(page_action);
596   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
597     if (page_action_decorations_[i]->GetPageAction() == page_action)
598       return page_action_decorations_[i];
599   }
600   // If |page_action| is the browser action of an extension, no element in
601   // |page_action_decorations_| will match.
602   NOTREACHED();
603   return NULL;
606 void LocationBarViewMac::DeletePageActionDecorations() {
607   // TODO(shess): Deleting these decorations could result in the cell
608   // refering to them before things are laid out again.  Meanwhile, at
609   // least fail safe.
610   [[field_ cell] clearDecorations];
612   page_action_decorations_.clear();
615 void LocationBarViewMac::OnEditBookmarksEnabledChanged() {
616   UpdateBookmarkStarVisibility();
617   OnChanged();
620 void LocationBarViewMac::RefreshPageActionDecorations() {
621   if (!IsEditable()) {
622     DeletePageActionDecorations();
623     return;
624   }
626   WebContents* web_contents = GetWebContents();
627   if (!web_contents) {
628     DeletePageActionDecorations();
629     return;
630   }
632   std::vector<ExtensionAction*> new_page_actions =
633       extensions::TabHelper::FromWebContents(web_contents)->
634           location_bar_controller()->GetCurrentActions();
636   if (PageActionsDiffer(new_page_actions)) {
637     DeletePageActionDecorations();
638     for (size_t i = 0; i < new_page_actions.size(); ++i) {
639       page_action_decorations_.push_back(
640           new PageActionDecoration(this, browser_, new_page_actions[i]));
641     }
642   }
644   GURL url = GetToolbarModel()->GetURL();
645   for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
646     page_action_decorations_[i]->UpdateVisibility(
647         GetToolbarModel()->input_in_progress() ? NULL : web_contents);
648   }
651 bool LocationBarViewMac::PageActionsDiffer(
652     const std::vector<ExtensionAction*>& page_actions) const {
653   if (page_action_decorations_.size() != page_actions.size())
654     return true;
656   for (size_t index = 0; index < page_actions.size(); ++index) {
657     PageActionDecoration* decoration = page_action_decorations_[index];
658     if (decoration->GetPageAction() != page_actions[index])
659       return true;
660   }
662   return false;
665 bool LocationBarViewMac::RefreshContentSettingsDecorations() {
666   const bool input_in_progress = GetToolbarModel()->input_in_progress();
667   WebContents* web_contents = input_in_progress ?
668       NULL : browser_->tab_strip_model()->GetActiveWebContents();
669   bool icons_updated = false;
670   for (size_t i = 0; i < content_setting_decorations_.size(); ++i) {
671     icons_updated |=
672         content_setting_decorations_[i]->UpdateFromWebContents(web_contents);
673   }
674   return icons_updated;
677 void LocationBarViewMac::ShowFirstRunBubbleInternal() {
678   if (!field_ || ![field_ window])
679     return;
681   // The first run bubble's left edge should line up with the left edge of the
682   // omnibox. This is different from other bubbles, which line up at a point
683   // set by their top arrow. Because the BaseBubbleController adjusts the
684   // window origin left to account for the arrow spacing, the first run bubble
685   // moves the window origin right by this spacing, so that the
686   // BaseBubbleController will move it back to the correct position.
687   const NSPoint kOffset = NSMakePoint(
688       info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth/2.0,
689       kFirstRunBubbleYOffset);
690   [FirstRunBubbleController showForView:field_
691                                  offset:kOffset
692                                 browser:browser_
693                                 profile:profile()];
696 void LocationBarViewMac::UpdateTranslateDecoration() {
697   if (!TranslateService::IsTranslateBubbleEnabled())
698     return;
700   WebContents* web_contents = GetWebContents();
701   if (!web_contents)
702     return;
703   translate::LanguageState& language_state =
704       ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
705   bool enabled = language_state.translate_enabled();
706   command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled);
707   translate_decoration_->SetVisible(enabled);
708   translate_decoration_->SetLit(language_state.IsPageTranslated());
711 bool LocationBarViewMac::UpdateZoomDecoration(bool default_zoom_changed) {
712   WebContents* web_contents = GetWebContents();
713   if (!web_contents)
714     return false;
716   return zoom_decoration_->UpdateIfNecessary(
717       ui_zoom::ZoomController::FromWebContents(web_contents),
718       default_zoom_changed);
721 void LocationBarViewMac::OnDefaultZoomLevelChanged() {
722   if (UpdateZoomDecoration(/*default_zoom_changed=*/true))
723     OnDecorationsChanged();
726 bool LocationBarViewMac::UpdateMicSearchDecorationVisibility() {
727   bool is_visible = !GetToolbarModel()->input_in_progress() &&
728                     browser_->search_model()->voice_search_supported();
729   if (mic_search_decoration_->IsVisible() == is_visible)
730     return false;
731   mic_search_decoration_->SetVisible(is_visible);
732   return true;