Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(318)

Side by Side Diff: chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm

Issue 2641003002: Show scheme in black and content in gray for data: protocol urls (Closed)
Patch Set: Update iOS Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" 5 #include "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h"
6 6
7 #include <Carbon/Carbon.h> // kVK_Return 7 #include <Carbon/Carbon.h> // kVK_Return
8 8
9 #include "base/mac/foundation_util.h" 9 #include "base/mac/foundation_util.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 void StoreStateToTab(WebContents* tab, 122 void StoreStateToTab(WebContents* tab,
123 OmniboxViewMacState* state) { 123 OmniboxViewMacState* state) {
124 tab->SetUserData(kOmniboxViewMacStateKey, state); 124 tab->SetUserData(kOmniboxViewMacStateKey, state);
125 } 125 }
126 126
127 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { 127 const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) {
128 return static_cast<OmniboxViewMacState*>( 128 return static_cast<OmniboxViewMacState*>(
129 tab->GetUserData(&kOmniboxViewMacStateKey)); 129 tab->GetUserData(&kOmniboxViewMacStateKey));
130 } 130 }
131 131
132 // Helper to make converting url ranges to NSRange easier to
133 // read.
134 NSRange ComponentToNSRange(const url::Component& component) {
135 return NSMakeRange(static_cast<NSInteger>(component.begin),
136 static_cast<NSInteger>(component.len));
137 }
138
139 } // namespace 132 } // namespace
140 133
141 // static 134 // static
142 NSImage* OmniboxViewMac::ImageForResource(int resource_id) { 135 NSImage* OmniboxViewMac::ImageForResource(int resource_id) {
143 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 136 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
144 return rb.GetNativeImageNamed(resource_id).ToNSImage(); 137 return rb.GetNativeImageNamed(resource_id).ToNSImage();
145 } 138 }
146 139
147 // static 140 // static
148 NSColor* OmniboxViewMac::SuggestTextColor() { 141 NSColor* OmniboxViewMac::SuggestTextColor() {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 base::WrapUnique(new ChromeOmniboxClient(controller, profile))), 178 base::WrapUnique(new ChromeOmniboxClient(controller, profile))),
186 profile_(profile), 179 profile_(profile),
187 popup_view_(new OmniboxPopupViewMac(this, model(), field)), 180 popup_view_(new OmniboxPopupViewMac(this, model(), field)),
188 field_(field), 181 field_(field),
189 saved_temporary_selection_(NSMakeRange(0, 0)), 182 saved_temporary_selection_(NSMakeRange(0, 0)),
190 marked_range_before_change_(NSMakeRange(0, 0)), 183 marked_range_before_change_(NSMakeRange(0, 0)),
191 delete_was_pressed_(false), 184 delete_was_pressed_(false),
192 delete_at_end_pressed_(false), 185 delete_at_end_pressed_(false),
193 in_coalesced_update_block_(false), 186 in_coalesced_update_block_(false),
194 do_coalesced_text_update_(false), 187 do_coalesced_text_update_(false),
195 do_coalesced_range_update_(false) { 188 do_coalesced_range_update_(false) {
Robert Sesek 2017/03/01 18:29:45 attributing_display_string_ is uninitialized.
elawrence 2017/03/01 21:49:01 Done.
196 [field_ setObserver:this]; 189 [field_ setObserver:this];
197 190
198 // Needed so that editing doesn't lose the styling. 191 // Needed so that editing doesn't lose the styling.
199 [field_ setAllowsEditingTextAttributes:YES]; 192 [field_ setAllowsEditingTextAttributes:YES];
200 193
201 // Get the appropriate line height for the font that we use. 194 // Get the appropriate line height for the font that we use.
202 base::scoped_nsobject<NSLayoutManager> layoutManager( 195 base::scoped_nsobject<NSLayoutManager> layoutManager(
203 [[NSLayoutManager alloc] init]); 196 [[NSLayoutManager alloc] init]);
204 [layoutManager setUsesScreenFonts:YES]; 197 [layoutManager setUsesScreenFonts:YES];
205 } 198 }
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 // If this is a URL, set the top-level paragraph direction to LTR (avoids RTL 531 // If this is a URL, set the top-level paragraph direction to LTR (avoids RTL
539 // characters from making the URL render from right to left, as per RFC 3987 532 // characters from making the URL render from right to left, as per RFC 3987
540 // Section 4.1). 533 // Section 4.1).
541 if (model()->CurrentTextIsURL()) 534 if (model()->CurrentTextIsURL())
542 [paragraph_style setBaseWritingDirection:NSWritingDirectionLeftToRight]; 535 [paragraph_style setBaseWritingDirection:NSWritingDirectionLeftToRight];
543 [attributedString addAttribute:NSParagraphStyleAttributeName 536 [attributedString addAttribute:NSParagraphStyleAttributeName
544 value:paragraph_style 537 value:paragraph_style
545 range:NSMakeRange(0, [attributedString length])]; 538 range:NSMakeRange(0, [attributedString length])];
546 } 539 }
547 540
541 void OmniboxViewMac::SetEmphasis(bool emphasize, gfx::Range range) {
542 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme];
543
544 NSRange ns_range = range.IsValid()
545 ? range.ToNSRange()
546 : NSMakeRange(0, [attributing_display_string_ length]);
547
548 [attributing_display_string_
549 addAttribute:NSForegroundColorAttributeName
550 value:(emphasize) ? HostTextColor(in_dark_mode)
551 : BaseTextColor(in_dark_mode)
552 range:ns_range];
553 }
554
555 void OmniboxViewMac::UpdateSchemeEmphasis(gfx::Range range) {
556 if (!range.IsValid())
557 return;
558
559 const security_state::SecurityLevel security_level =
560 controller()->GetToolbarModel()->GetSecurityLevel(false);
561
562 if ((security_level == security_state::NONE) ||
563 (security_level == security_state::HTTP_SHOW_WARNING))
564 return;
565
566 if (security_level == security_state::DANGEROUS) {
567 // Add a strikethrough through the scheme.
568 [attributing_display_string_
569 addAttribute:NSStrikethroughStyleAttributeName
570 value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
571 range:range.ToNSRange()];
572 }
573
574 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme];
575
576 [attributing_display_string_
577 addAttribute:NSForegroundColorAttributeName
578 value:GetSecureTextColor(security_level, in_dark_mode)
579 range:range.ToNSRange()];
580 }
581
548 void OmniboxViewMac::ApplyTextAttributes( 582 void OmniboxViewMac::ApplyTextAttributes(
549 const base::string16& display_text, 583 const base::string16& display_text,
550 NSMutableAttributedString* attributedString) { 584 NSMutableAttributedString* attributedString) {
Robert Sesek 2017/03/01 18:29:45 nit: Since you're here, this should be attributed_
elawrence 2017/03/01 21:49:01 Done.
551 NSUInteger as_length = [attributedString length]; 585 NSUInteger as_length = [attributedString length];
552 if (as_length == 0) { 586 if (as_length == 0) {
553 return; 587 return;
554 } 588 }
555 NSRange as_entire_string = NSMakeRange(0, as_length);
556 bool in_dark_mode = [[field_ window] inIncognitoModeWithSystemTheme];
557 589
558 ApplyTextStyle(attributedString); 590 ApplyTextStyle(attributedString);
559 591
560 // A kinda hacky way to add breaking at periods. This is what Safari does. 592 // A kinda hacky way to add breaking at periods. This is what Safari does.
561 // This works for IDNs too, despite the "en_US". 593 // This works for IDNs too, despite the "en_US".
562 [attributedString addAttribute:@"NSLanguage" 594 [attributedString addAttribute:@"NSLanguage"
563 value:@"en_US_POSIX" 595 value:@"en_US_POSIX"
564 range:as_entire_string]; 596 range:NSMakeRange(0, as_length)];
565 597
566 [attributedString addAttribute:NSForegroundColorAttributeName 598 // Cache a pointer to the attributed string to allow the superclass'
567 value:HostTextColor(in_dark_mode) 599 // virtual method invocations to add attributes.
568 range:as_entire_string]; 600 attributing_display_string_ = attributedString;
Peter Kasting 2017/03/01 02:39:10 Nit: Consider using base::AutoReset here for safet
Robert Sesek 2017/03/01 18:29:45 Perhaps DCHECK that attributing_display_string_ is
elawrence 2017/03/01 21:49:01 Done.
elawrence 2017/03/01 21:49:01 Done.
569 601 ApplyEmphasis(display_text, ChromeAutocompleteSchemeClassifier(profile_));
570 url::Component scheme, host; 602 attributing_display_string_ = nullptr;
571 AutocompleteInput::ParseForEmphasizeComponents(
572 display_text, ChromeAutocompleteSchemeClassifier(profile_), &scheme,
573 &host);
574 bool grey_out_url = display_text.substr(scheme.begin, scheme.len) ==
575 base::UTF8ToUTF16(extensions::kExtensionScheme);
576 if (model()->CurrentTextIsURL() &&
577 (host.is_nonempty() || grey_out_url)) {
578 [attributedString addAttribute:NSForegroundColorAttributeName
579 value:BaseTextColor(in_dark_mode)
580 range:as_entire_string];
581
582 if (!grey_out_url) {
583 [attributedString addAttribute:NSForegroundColorAttributeName
584 value:HostTextColor(in_dark_mode)
585 range:ComponentToNSRange(host)];
586 }
587 }
588
589 // TODO(shess): GTK has this as a member var, figure out why.
590 // [Could it be to not change if no change? If so, I'm guessing
591 // AppKit may already handle that.]
592 const security_state::SecurityLevel security_level =
593 controller()->GetToolbarModel()->GetSecurityLevel(false);
594
595 // Emphasize the scheme for security UI display purposes (if necessary).
596 if (!model()->user_input_in_progress() && model()->CurrentTextIsURL() &&
597 scheme.is_nonempty() &&
598 (security_level != security_state::NONE) &&
599 (security_level != security_state::HTTP_SHOW_WARNING)) {
600 if (security_level == security_state::DANGEROUS) {
601 // Add a strikethrough through the scheme.
602 [attributedString addAttribute:NSStrikethroughStyleAttributeName
603 value:[NSNumber numberWithInt:NSUnderlineStyleSingle]
604 range:ComponentToNSRange(scheme)];
605 }
606 [attributedString
607 addAttribute:NSForegroundColorAttributeName
608 value:GetSecureTextColor(security_level, in_dark_mode)
609 range:ComponentToNSRange(scheme)];
610 }
611 } 603 }
612 604
613 void OmniboxViewMac::OnTemporaryTextMaybeChanged( 605 void OmniboxViewMac::OnTemporaryTextMaybeChanged(
614 const base::string16& display_text, 606 const base::string16& display_text,
615 bool save_original_selection, 607 bool save_original_selection,
616 bool notify_text_changed) { 608 bool notify_text_changed) {
617 if (save_original_selection) 609 if (save_original_selection)
618 saved_temporary_selection_ = GetSelectedRange(); 610 saved_temporary_selection_ = GetSelectedRange();
619 611
620 SetWindowTextAndCaretPos(display_text, display_text.size(), false, false); 612 SetWindowTextAndCaretPos(display_text, display_text.size(), false, false);
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 display_text); 1080 display_text);
1089 NSDictionary* notification_info = @{ 1081 NSDictionary* notification_info = @{
1090 NSAccessibilityAnnouncementKey : announcement, 1082 NSAccessibilityAnnouncementKey : announcement,
1091 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh) 1083 NSAccessibilityPriorityKey : @(NSAccessibilityPriorityHigh)
1092 }; 1084 };
1093 NSAccessibilityPostNotificationWithUserInfo( 1085 NSAccessibilityPostNotificationWithUserInfo(
1094 [field_ window], 1086 [field_ window],
1095 NSAccessibilityAnnouncementRequestedNotification, 1087 NSAccessibilityAnnouncementRequestedNotification,
1096 notification_info); 1088 notification_info);
1097 } 1089 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698