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"
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 #import "chrome/browser/app_controller_mac.h"
16 #include "chrome/browser/chrome_notification_types.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/extension_service.h"
23 #include "chrome/browser/extensions/location_bar_controller.h"
24 #include "chrome/browser/extensions/tab_helper.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.h"
29 #include "chrome/browser/search_engines/template_url_service.h"
30 #include "chrome/browser/search_engines/template_url_service_factory.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/mic_search_decoration.h"
44 #import "chrome/browser/ui/cocoa/location_bar/page_action_decoration.h"
45 #import "chrome/browser/ui/cocoa/location_bar/search_button_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/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 #include "chrome/browser/ui/omnibox/location_bar_util.h"
53 #import "chrome/browser/ui/omnibox/omnibox_popup_model.h"
54 #include "chrome/browser/ui/tabs/tab_strip_model.h"
55 #include "chrome/browser/ui/toolbar/toolbar_model.h"
56 #include "chrome/browser/ui/zoom/zoom_controller.h"
57 #include "chrome/common/chrome_switches.h"
58 #include "chrome/common/pref_names.h"
59 #include "content/public/browser/notification_service.h"
60 #include "content/public/browser/web_contents.h"
61 #include "extensions/browser/extension_system.h"
62 #include "extensions/common/extension.h"
63 #include "extensions/common/permissions/permissions_data.h"
64 #include "grit/generated_resources.h"
65 #include "grit/theme_resources.h"
66 #include "net/base/net_util.h"
67 #include "skia/ext/skia_utils_mac.h"
68 #import "ui/base/cocoa/cocoa_event_utils.h"
69 #include "ui/base/l10n/l10n_util_mac.h"
70 #include "ui/base/resource/resource_bundle.h"
71 #include "ui/gfx/image/image.h"
73 using content::WebContents;
77 // Vertical space between the bottom edge of the location_bar and the first run
78 // bubble arrow point.
79 const static int kFirstRunBubbleYOffset = 1;
81 // Functor for moving BookmarkManagerPrivate page actions to the right via
83 class IsPageActionViewRightAligned {
85 explicit IsPageActionViewRightAligned(ExtensionService* extension_service)
86 : extension_service_(extension_service) {}
88 bool operator()(PageActionDecoration* page_action_decoration) {
89 return extensions::PermissionsData::HasAPIPermission(
90 extension_service_->GetExtensionById(
91 page_action_decoration->page_action()->extension_id(),
93 extensions::APIPermission::kBookmarkManagerPrivate);
97 ExtensionService* extension_service_;
99 // NOTE: Can't DISALLOW_COPY_AND_ASSIGN as we pass this object by value to
100 // std::stable_partition().
105 // TODO(shess): This code is mostly copied from the gtk
106 // implementation. Make sure it's all appropriate and flesh it out.
108 LocationBarViewMac::LocationBarViewMac(
109 AutocompleteTextField* field,
110 CommandUpdater* command_updater,
113 : LocationBar(profile),
114 OmniboxEditController(command_updater),
115 omnibox_view_(new OmniboxViewMac(this, profile, command_updater, field)),
117 location_icon_decoration_(new LocationIconDecoration(this)),
118 selected_keyword_decoration_(new SelectedKeywordDecoration()),
119 ev_bubble_decoration_(
120 new EVBubbleDecoration(location_icon_decoration_.get())),
121 star_decoration_(new StarDecoration(command_updater)),
122 zoom_decoration_(new ZoomDecoration(this)),
123 keyword_hint_decoration_(new KeywordHintDecoration()),
124 mic_search_decoration_(new MicSearchDecoration(command_updater)),
125 generated_credit_card_decoration_(
126 new GeneratedCreditCardDecoration(this)),
127 search_button_decoration_(new SearchButtonDecoration(this)),
129 weak_ptr_factory_(this) {
131 for (size_t i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
132 DCHECK_EQ(i, content_setting_decorations_.size());
133 ContentSettingsType type = static_cast<ContentSettingsType>(i);
134 content_setting_decorations_.push_back(
135 new ContentSettingDecoration(type, this, profile));
139 this, chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
140 content::NotificationService::AllSources());
141 content::Source<Profile> profile_source = content::Source<Profile>(profile);
142 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED,
144 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, profile_source);
145 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, profile_source);
147 edit_bookmarks_enabled_.Init(
148 prefs::kEditBookmarksEnabled, profile->GetPrefs(),
149 base::Bind(&LocationBarViewMac::OnEditBookmarksEnabledChanged,
150 base::Unretained(this)));
152 browser_->search_model()->AddObserver(this);
154 [[field_ cell] setIsPopupMode:
155 !browser->SupportsWindowFeature(Browser::FEATURE_TABSTRIP)];
158 LocationBarViewMac::~LocationBarViewMac() {
159 // Disconnect from cell in case it outlives us.
160 [[field_ cell] clearDecorations];
162 browser_->search_model()->RemoveObserver(this);
165 void LocationBarViewMac::ShowFirstRunBubble() {
166 // We need the browser window to be shown before we can show the bubble, but
167 // we get called before that's happened.
168 base::MessageLoop::current()->PostTask(
169 FROM_HERE, base::Bind(&LocationBarViewMac::ShowFirstRunBubbleInternal,
170 weak_ptr_factory_.GetWeakPtr()));
173 GURL LocationBarViewMac::GetDestinationURL() const {
174 return destination_url();
177 WindowOpenDisposition LocationBarViewMac::GetWindowOpenDisposition() const {
178 return disposition();
181 content::PageTransition LocationBarViewMac::GetPageTransition() const {
185 void LocationBarViewMac::AcceptInput() {
186 WindowOpenDisposition disposition =
187 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
188 omnibox_view_->model()->AcceptInput(disposition, false);
191 void LocationBarViewMac::FocusLocation(bool select_all) {
192 omnibox_view_->FocusLocation(select_all);
195 void LocationBarViewMac::FocusSearch() {
196 omnibox_view_->SetForcedQuery();
199 void LocationBarViewMac::UpdateContentSettingsIcons() {
200 if (RefreshContentSettingsDecorations())
201 OnDecorationsChanged();
204 void LocationBarViewMac::UpdatePageActions() {
205 size_t count_before = page_action_decorations_.size();
206 RefreshPageActionDecorations();
208 if (page_action_decorations_.size() != count_before) {
209 content::NotificationService::current()->Notify(
210 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED,
211 content::Source<LocationBar>(this),
212 content::NotificationService::NoDetails());
216 void LocationBarViewMac::InvalidatePageActions() {
217 size_t count_before = page_action_decorations_.size();
218 DeletePageActionDecorations();
220 if (page_action_decorations_.size() != count_before) {
221 content::NotificationService::current()->Notify(
222 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED,
223 content::Source<LocationBar>(this),
224 content::NotificationService::NoDetails());
228 void LocationBarViewMac::UpdateOpenPDFInReaderPrompt() {
229 // Not implemented on Mac.
232 void LocationBarViewMac::UpdateGeneratedCreditCardView() {
233 generated_credit_card_decoration_->Update();
236 void LocationBarViewMac::SaveStateToContents(WebContents* contents) {
237 // TODO(shess): Why SaveStateToContents vs SaveStateToTab?
238 omnibox_view_->SaveStateToTab(contents);
241 void LocationBarViewMac::Revert() {
242 omnibox_view_->RevertAll();
245 const OmniboxView* LocationBarViewMac::GetOmniboxView() const {
246 return omnibox_view_.get();
249 OmniboxView* LocationBarViewMac::GetOmniboxView() {
250 return omnibox_view_.get();
253 LocationBarTesting* LocationBarViewMac::GetLocationBarForTesting() {
257 // TODO(pamg): Change all these, here and for other platforms, to size_t.
258 int LocationBarViewMac::PageActionCount() {
259 return static_cast<int>(page_action_decorations_.size());
262 int LocationBarViewMac::PageActionVisibleCount() {
264 for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
265 if (page_action_decorations_[i]->IsVisible())
271 ExtensionAction* LocationBarViewMac::GetPageAction(size_t index) {
272 if (index < page_action_decorations_.size())
273 return page_action_decorations_[index]->page_action();
278 ExtensionAction* LocationBarViewMac::GetVisiblePageAction(size_t index) {
280 for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
281 if (page_action_decorations_[i]->IsVisible()) {
282 if (current == index)
283 return page_action_decorations_[i]->page_action();
293 void LocationBarViewMac::TestPageActionPressed(size_t index) {
294 DCHECK_LT(index, page_action_decorations_.size());
295 if (index < page_action_decorations_.size())
296 page_action_decorations_[index]->OnMousePressed(NSZeroRect);
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 UpdateStarDecorationVisibility();
307 UpdateZoomDecoration();
312 bool LocationBarViewMac::IsEditable() {
313 return [field_ isEditable] ? true : false;
316 void LocationBarViewMac::SetStarred(bool starred) {
317 star_decoration_->SetStarred(starred);
318 UpdateStarDecorationVisibility();
319 OnDecorationsChanged();
322 void LocationBarViewMac::ZoomChangedForActiveTab(bool can_show_bubble) {
323 UpdateZoomDecoration();
324 OnDecorationsChanged();
326 if (can_show_bubble && zoom_decoration_->IsVisible())
327 zoom_decoration_->ShowBubble(YES);
330 bool LocationBarViewMac::IsStarEnabled() const {
331 return browser_defaults::bookmarks_enabled &&
332 [field_ isEditable] &&
333 !GetToolbarModel()->input_in_progress() &&
334 edit_bookmarks_enabled_.GetValue() &&
335 !IsBookmarkStarHiddenByExtension();
338 NSPoint LocationBarViewMac::GetBookmarkBubblePoint() const {
339 DCHECK(IsStarEnabled());
341 AutocompleteTextFieldCell* cell = [field_ cell];
342 const NSRect frame = [cell frameForDecoration:star_decoration_.get()
343 inFrame:[field_ bounds]];
344 const NSPoint point = star_decoration_->GetBubblePointInFrame(frame);
345 return [field_ convertPoint:point toView:nil];
348 NSPoint LocationBarViewMac::GetPageInfoBubblePoint() const {
349 AutocompleteTextFieldCell* cell = [field_ cell];
350 if (ev_bubble_decoration_->IsVisible()) {
351 const NSRect frame = [cell frameForDecoration:ev_bubble_decoration_.get()
352 inFrame:[field_ bounds]];
353 const NSPoint point = ev_bubble_decoration_->GetBubblePointInFrame(frame);
354 return [field_ convertPoint:point toView:nil];
357 [cell frameForDecoration:location_icon_decoration_.get()
358 inFrame:[field_ bounds]];
359 const NSPoint point =
360 location_icon_decoration_->GetBubblePointInFrame(frame);
361 return [field_ convertPoint:point toView:nil];
365 NSPoint LocationBarViewMac::GetGeneratedCreditCardBubblePoint() const {
366 AutocompleteTextFieldCell* cell = [field_ cell];
368 [cell frameForDecoration:generated_credit_card_decoration_.get()
369 inFrame:[field_ bounds]];
370 const NSPoint point =
371 generated_credit_card_decoration_->GetBubblePointInFrame(frame);
372 return [field_ convertPoint:point toView:nil];
375 void LocationBarViewMac::OnDecorationsChanged() {
376 // TODO(shess): The field-editor frame and cursor rects should not
378 [field_ updateMouseTracking];
379 [field_ resetFieldEditorFrameIfNeeded];
380 [field_ setNeedsDisplay:YES];
383 // TODO(shess): This function should over time grow to closely match
384 // the views Layout() function.
385 void LocationBarViewMac::Layout() {
386 AutocompleteTextFieldCell* cell = [field_ cell];
388 // Reset the left-hand decorations.
389 // TODO(shess): Shortly, this code will live somewhere else, like in
390 // the constructor. I am still wrestling with how best to deal with
391 // right-hand decorations, which are not a static set.
392 [cell clearDecorations];
393 [cell addLeftDecoration:location_icon_decoration_.get()];
394 [cell addLeftDecoration:selected_keyword_decoration_.get()];
395 [cell addLeftDecoration:ev_bubble_decoration_.get()];
396 [cell addRightDecoration:search_button_decoration_.get()];
397 [cell addRightDecoration:star_decoration_.get()];
398 [cell addRightDecoration:zoom_decoration_.get()];
399 [cell addRightDecoration:generated_credit_card_decoration_.get()];
401 // Note that display order is right to left.
402 for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
403 [cell addRightDecoration:page_action_decorations_[i]];
406 for (ScopedVector<ContentSettingDecoration>::iterator i =
407 content_setting_decorations_.begin();
408 i != content_setting_decorations_.end(); ++i) {
409 [cell addRightDecoration:*i];
412 [cell addRightDecoration:keyword_hint_decoration_.get()];
413 [cell addRightDecoration:mic_search_decoration_.get()];
415 // By default only the location icon is visible.
416 location_icon_decoration_->SetVisible(true);
417 selected_keyword_decoration_->SetVisible(false);
418 ev_bubble_decoration_->SetVisible(false);
419 keyword_hint_decoration_->SetVisible(false);
421 // Get the keyword to use for keyword-search and hinting.
422 const base::string16 keyword = omnibox_view_->model()->keyword();
423 base::string16 short_name;
424 bool is_extension_keyword = false;
425 if (!keyword.empty()) {
426 short_name = TemplateURLServiceFactory::GetForProfile(profile())->
427 GetKeywordShortName(keyword, &is_extension_keyword);
430 const bool is_keyword_hint = omnibox_view_->model()->is_keyword_hint();
431 if (!keyword.empty() && !is_keyword_hint) {
432 // Switch from location icon to keyword mode.
433 location_icon_decoration_->SetVisible(false);
434 selected_keyword_decoration_->SetVisible(true);
435 selected_keyword_decoration_->SetKeyword(short_name, is_extension_keyword);
436 selected_keyword_decoration_->SetImage(GetKeywordImage(keyword));
437 } else if (GetToolbarModel()->GetSecurityLevel(false) ==
438 ToolbarModel::EV_SECURE) {
439 // Switch from location icon to show the EV bubble instead.
440 location_icon_decoration_->SetVisible(false);
441 ev_bubble_decoration_->SetVisible(true);
443 base::string16 label(GetToolbarModel()->GetEVCertName());
444 ev_bubble_decoration_->SetFullLabel(base::SysUTF16ToNSString(label));
445 } else if (!keyword.empty() && is_keyword_hint) {
446 keyword_hint_decoration_->SetKeyword(short_name,
447 is_extension_keyword);
448 keyword_hint_decoration_->SetVisible(true);
451 // These need to change anytime the layout changes.
452 // TODO(shess): Anytime the field editor might have changed, the
453 // cursor rects almost certainly should have changed. The tooltips
454 // might change even when the rects don't change.
455 OnDecorationsChanged();
458 void LocationBarViewMac::RedrawDecoration(LocationBarDecoration* decoration) {
459 AutocompleteTextFieldCell* cell = [field_ cell];
460 NSRect frame = [cell frameForDecoration:decoration
461 inFrame:[field_ bounds]];
462 if (!NSIsEmptyRect(frame))
463 [field_ setNeedsDisplayInRect:frame];
466 void LocationBarViewMac::SetPreviewEnabledPageAction(
467 ExtensionAction* page_action, bool preview_enabled) {
469 WebContents* contents = GetWebContents();
472 RefreshPageActionDecorations();
475 PageActionDecoration* decoration = GetPageActionDecoration(page_action);
480 decoration->set_preview_enabled(preview_enabled);
481 decoration->UpdateVisibility(contents, GetToolbarModel()->GetURL());
484 NSRect LocationBarViewMac::GetPageActionFrame(ExtensionAction* page_action) {
485 PageActionDecoration* decoration = GetPageActionDecoration(page_action);
489 AutocompleteTextFieldCell* cell = [field_ cell];
490 NSRect frame = [cell frameForDecoration:decoration inFrame:[field_ bounds]];
494 NSPoint LocationBarViewMac::GetPageActionBubblePoint(
495 ExtensionAction* page_action) {
496 PageActionDecoration* decoration = GetPageActionDecoration(page_action);
500 NSRect frame = GetPageActionFrame(page_action);
501 if (NSIsEmptyRect(frame)) {
502 // The bubble point positioning assumes that the page action is visible. If
503 // not, something else needs to be done otherwise the bubble will appear
504 // near the top left corner (unanchored).
509 NSPoint bubble_point = decoration->GetBubblePointInFrame(frame);
510 return [field_ convertPoint:bubble_point toView:nil];
513 void LocationBarViewMac::Update(const WebContents* contents) {
514 UpdateStarDecorationVisibility();
515 UpdateZoomDecoration();
516 RefreshPageActionDecorations();
517 RefreshContentSettingsDecorations();
518 UpdateMicSearchDecorationVisibility();
519 UpdateGeneratedCreditCardView();
521 omnibox_view_->OnTabChanged(contents);
523 omnibox_view_->Update();
527 void LocationBarViewMac::OnChanged() {
528 // Update the location-bar icon.
529 const int resource_id = omnibox_view_->GetIcon();
530 NSImage* image = OmniboxViewMac::ImageForResource(resource_id);
531 location_icon_decoration_->SetImage(image);
532 ev_bubble_decoration_->SetImage(image);
534 ToolbarModel* toolbar_model = GetToolbarModel();
535 const chrome::DisplaySearchButtonConditions conditions =
536 chrome::GetDisplaySearchButtonConditions();
537 const bool meets_conditions =
538 (conditions == chrome::DISPLAY_SEARCH_BUTTON_ALWAYS) ||
539 ((conditions != chrome::DISPLAY_SEARCH_BUTTON_NEVER) &&
540 (toolbar_model->WouldPerformSearchTermReplacement(true) ||
541 ((conditions == chrome::DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP) &&
542 toolbar_model->input_in_progress())));
543 search_button_decoration_->SetVisible(
544 ![[field_ cell] isPopupMode] && meets_conditions);
545 search_button_decoration_->SetIcon(
546 (resource_id == IDR_OMNIBOX_SEARCH) ?
547 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE : IDR_OMNIBOX_SEARCH_BUTTON_ARROW);
551 InstantService* instant_service =
552 InstantServiceFactory::GetForProfile(profile());
553 if (instant_service) {
554 gfx::Rect bounds(NSRectToCGRect([field_ frame]));
555 instant_service->OnOmniboxStartMarginChanged(bounds.x());
559 void LocationBarViewMac::OnSetFocus() {
560 // Update the keyword and search hint states.
564 InstantController* LocationBarViewMac::GetInstant() {
565 return browser_->instant_controller() ?
566 browser_->instant_controller()->instant() : NULL;
569 WebContents* LocationBarViewMac::GetWebContents() {
570 return browser_->tab_strip_model()->GetActiveWebContents();
573 ToolbarModel* LocationBarViewMac::GetToolbarModel() {
574 return browser_->toolbar_model();
577 const ToolbarModel* LocationBarViewMac::GetToolbarModel() const {
578 return browser_->toolbar_model();
581 NSImage* LocationBarViewMac::GetKeywordImage(const base::string16& keyword) {
582 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
583 profile())->GetTemplateURLForKeyword(keyword);
585 (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
586 return extensions::OmniboxAPI::Get(profile())->
587 GetOmniboxIcon(template_url->GetExtensionId()).AsNSImage();
590 return OmniboxViewMac::ImageForResource(IDR_OMNIBOX_SEARCH);
593 void LocationBarViewMac::Observe(int type,
594 const content::NotificationSource& source,
595 const content::NotificationDetails& details) {
597 case chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED: {
598 WebContents* contents = GetWebContents();
599 if (content::Details<WebContents>(contents) != details)
602 [field_ updateMouseTracking];
603 [field_ setNeedsDisplay:YES];
607 case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED: {
608 // Only update if the updated action box was for the active tab contents.
609 WebContents* target_tab = content::Details<WebContents>(details).ptr();
610 if (target_tab == GetWebContents())
615 case chrome::NOTIFICATION_EXTENSION_LOADED:
616 case chrome::NOTIFICATION_EXTENSION_UNLOADED:
621 NOTREACHED() << "Unexpected notification";
626 void LocationBarViewMac::ModelChanged(const SearchModel::State& old_state,
627 const SearchModel::State& new_state) {
628 if (UpdateMicSearchDecorationVisibility())
632 void LocationBarViewMac::PostNotification(NSString* notification) {
633 [[NSNotificationCenter defaultCenter] postNotificationName:notification
634 object:[NSValue valueWithPointer:this]];
637 PageActionDecoration* LocationBarViewMac::GetPageActionDecoration(
638 ExtensionAction* page_action) {
640 for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
641 if (page_action_decorations_[i]->page_action() == page_action)
642 return page_action_decorations_[i];
644 // If |page_action| is the browser action of an extension, no element in
645 // |page_action_decorations_| will match.
651 void LocationBarViewMac::DeletePageActionDecorations() {
652 // TODO(shess): Deleting these decorations could result in the cell
653 // refering to them before things are laid out again. Meanwhile, at
655 [[field_ cell] clearDecorations];
657 page_action_decorations_.clear();
660 void LocationBarViewMac::OnEditBookmarksEnabledChanged() {
661 UpdateStarDecorationVisibility();
665 void LocationBarViewMac::RefreshPageActionDecorations() {
667 DeletePageActionDecorations();
671 WebContents* web_contents = GetWebContents();
673 DeletePageActionDecorations(); // Necessary?
677 std::vector<ExtensionAction*> new_page_actions =
678 extensions::TabHelper::FromWebContents(web_contents)->
679 location_bar_controller()->GetCurrentActions();
681 if (new_page_actions != page_actions_) {
682 page_actions_.swap(new_page_actions);
683 DeletePageActionDecorations();
684 for (size_t i = 0; i < page_actions_.size(); ++i) {
685 page_action_decorations_.push_back(
686 new PageActionDecoration(this, browser_, page_actions_[i]));
689 // Move rightmost extensions to the start.
690 std::stable_partition(
691 page_action_decorations_.begin(),
692 page_action_decorations_.end(),
693 IsPageActionViewRightAligned(
694 extensions::ExtensionSystem::Get(profile())->extension_service()));
697 GURL url = GetToolbarModel()->GetURL();
698 for (size_t i = 0; i < page_action_decorations_.size(); ++i) {
699 page_action_decorations_[i]->UpdateVisibility(
700 GetToolbarModel()->input_in_progress() ? NULL : web_contents, url);
704 bool LocationBarViewMac::RefreshContentSettingsDecorations() {
705 const bool input_in_progress = GetToolbarModel()->input_in_progress();
706 WebContents* web_contents = input_in_progress ?
707 NULL : browser_->tab_strip_model()->GetActiveWebContents();
708 bool icons_updated = false;
709 for (size_t i = 0; i < content_setting_decorations_.size(); ++i) {
711 content_setting_decorations_[i]->UpdateFromWebContents(web_contents);
713 return icons_updated;
716 void LocationBarViewMac::ShowFirstRunBubbleInternal() {
717 if (!field_ || ![field_ window])
720 // The first run bubble's left edge should line up with the left edge of the
721 // omnibox. This is different from other bubbles, which line up at a point
722 // set by their top arrow. Because the BaseBubbleController adjusts the
723 // window origin left to account for the arrow spacing, the first run bubble
724 // moves the window origin right by this spacing, so that the
725 // BaseBubbleController will move it back to the correct position.
726 const NSPoint kOffset = NSMakePoint(
727 info_bubble::kBubbleArrowXOffset + info_bubble::kBubbleArrowWidth/2.0,
728 kFirstRunBubbleYOffset);
729 [FirstRunBubbleController showForView:field_
735 void LocationBarViewMac::UpdateZoomDecoration() {
736 WebContents* web_contents = GetWebContents();
740 zoom_decoration_->Update(ZoomController::FromWebContents(web_contents));
743 void LocationBarViewMac::UpdateStarDecorationVisibility() {
744 star_decoration_->SetVisible(IsStarEnabled());
747 bool LocationBarViewMac::UpdateMicSearchDecorationVisibility() {
748 bool is_visible = !GetToolbarModel()->input_in_progress() &&
749 browser_->search_model()->voice_search_supported();
750 if (mic_search_decoration_->IsVisible() == is_visible)
752 mic_search_decoration_->SetVisible(is_visible);