OLD | NEW |
---|---|
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 "ios/chrome/browser/ui/omnibox/omnibox_view_ios.h" | 5 #include "ios/chrome/browser/ui/omnibox/omnibox_view_ios.h" |
6 | 6 |
7 #import <CoreText/CoreText.h> | 7 #import <CoreText/CoreText.h> |
8 #import <MobileCoreServices/MobileCoreServices.h> | 8 #import <MobileCoreServices/MobileCoreServices.h> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 // The color of the rest of the URL (i.e. after the TLD) in the omnibox. | 43 // The color of the rest of the URL (i.e. after the TLD) in the omnibox. |
44 UIColor* BaseTextColor() { | 44 UIColor* BaseTextColor() { |
45 return [UIColor colorWithWhite:(161 / 255.0) alpha:1.0]; | 45 return [UIColor colorWithWhite:(161 / 255.0) alpha:1.0]; |
46 } | 46 } |
47 | 47 |
48 // The color of the https when there is an error. | 48 // The color of the https when there is an error. |
49 UIColor* ErrorTextColor() { | 49 UIColor* ErrorTextColor() { |
50 return skia::UIColorFromSkColor(gfx::kGoogleRed700); | 50 return skia::UIColorFromSkColor(gfx::kGoogleRed700); |
51 } | 51 } |
52 | 52 |
53 // The color of the https when there is a warning. | |
54 UIColor* WarningTextColor() { | |
55 return skia::UIColorFromSkColor(gfx::kGoogleYellow700); | |
56 } | |
57 | |
58 // The color of the https when there is not an error. | 53 // The color of the https when there is not an error. |
59 UIColor* SecureTextColor() { | 54 UIColor* SecureTextColor() { |
60 return skia::UIColorFromSkColor(gfx::kGoogleGreen700); | 55 return skia::UIColorFromSkColor(gfx::kGoogleGreen700); |
61 } | 56 } |
62 | 57 |
63 // The color of the https when highlighted in incognito. | 58 // The color of the https when highlighted in incognito. |
64 UIColor* IncognitoSecureTextColor() { | 59 UIColor* IncognitoSecureTextColor() { |
65 return [UIColor colorWithWhite:(255 / 255.0) alpha:1.0]; | 60 return [UIColor colorWithWhite:(255 / 255.0) alpha:1.0]; |
66 } | 61 } |
67 | 62 |
68 // Helper to make converting url_parse ranges to NSRange easier to | |
69 // read. | |
70 NSRange ComponentToNSRange(const url::Component& component) { | |
71 return NSMakeRange(static_cast<NSInteger>(component.begin), | |
72 static_cast<NSInteger>(component.len)); | |
73 } | |
74 | |
75 } // namespace | 63 } // namespace |
76 | 64 |
77 // Simple Obj-C object to forward UITextFieldDelegate method calls back to the | 65 // Simple Obj-C object to forward UITextFieldDelegate method calls back to the |
78 // OmniboxViewIOS. | 66 // OmniboxViewIOS. |
79 @interface AutocompleteTextFieldDelegate : NSObject<OmniboxTextFieldDelegate> { | 67 @interface AutocompleteTextFieldDelegate : NSObject<OmniboxTextFieldDelegate> { |
80 @private | 68 @private |
81 OmniboxViewIOS* editView_; // weak, owns us | 69 OmniboxViewIOS* editView_; // weak, owns us |
82 | 70 |
83 // YES if we are already forwarding an OnDidChange() message to the edit view. | 71 // YES if we are already forwarding an OnDidChange() message to the edit view. |
84 // Needed to prevent infinite recursion. | 72 // Needed to prevent infinite recursion. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 ios::ChromeBrowserState* browser_state, | 166 ios::ChromeBrowserState* browser_state, |
179 id<PreloadProvider> preloader, | 167 id<PreloadProvider> preloader, |
180 id<OmniboxPopupPositioner> positioner) | 168 id<OmniboxPopupPositioner> positioner) |
181 : OmniboxView( | 169 : OmniboxView( |
182 controller, | 170 controller, |
183 base::MakeUnique<ChromeOmniboxClientIOS>(controller, browser_state)), | 171 base::MakeUnique<ChromeOmniboxClientIOS>(controller, browser_state)), |
184 browser_state_(browser_state), | 172 browser_state_(browser_state), |
185 field_([field retain]), | 173 field_([field retain]), |
186 controller_(controller), | 174 controller_(controller), |
187 preloader_(preloader), | 175 preloader_(preloader), |
188 ignore_popup_updates_(false) { | 176 ignore_popup_updates_(false), |
177 attributing_display_string_(nil) { | |
189 popup_view_.reset(new OmniboxPopupViewIOS(this, model(), positioner)); | 178 popup_view_.reset(new OmniboxPopupViewIOS(this, model(), positioner)); |
190 field_delegate_.reset( | 179 field_delegate_.reset( |
191 [[AutocompleteTextFieldDelegate alloc] initWithEditView:this]); | 180 [[AutocompleteTextFieldDelegate alloc] initWithEditView:this]); |
192 [field_ setDelegate:field_delegate_]; | 181 [field_ setDelegate:field_delegate_]; |
193 [field_ addTarget:field_delegate_ | 182 [field_ addTarget:field_delegate_ |
194 action:@selector(textFieldDidChange:) | 183 action:@selector(textFieldDidChange:) |
195 forControlEvents:UIControlEventEditingChanged]; | 184 forControlEvents:UIControlEventEditingChanged]; |
196 } | 185 } |
197 | 186 |
198 OmniboxViewIOS::~OmniboxViewIOS() { | 187 OmniboxViewIOS::~OmniboxViewIOS() { |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 } | 616 } |
628 | 617 |
629 bool OmniboxViewIOS::CanCopyURL() { | 618 bool OmniboxViewIOS::CanCopyURL() { |
630 return false; | 619 return false; |
631 } | 620 } |
632 | 621 |
633 void OmniboxViewIOS::WillPaste() { | 622 void OmniboxViewIOS::WillPaste() { |
634 model()->OnPaste(); | 623 model()->OnPaste(); |
635 } | 624 } |
636 | 625 |
626 // static | |
627 UIColor* OmniboxViewIOS::GetSecureTextColor( | |
628 security_state::SecurityLevel security_level, | |
629 bool in_dark_mode) { | |
630 if (security_level == security_state::EV_SECURE || | |
631 security_level == security_state::SECURE) { | |
632 return in_dark_mode ? IncognitoSecureTextColor() : SecureTextColor(); | |
633 } | |
634 | |
635 // Don't color strikethrough in dark mode. See https://crbug.com/635004#c6 | |
636 if (security_level == security_state::DANGEROUS && !in_dark_mode) | |
637 return ErrorTextColor(); | |
638 | |
639 return nil; | |
640 } | |
641 | |
642 void OmniboxViewIOS::SetEmphasis(bool emphasize, const gfx::Range& range) { | |
643 NSRange ns_range = range.IsValid() | |
644 ? range.ToNSRange() | |
645 : NSMakeRange(0, [attributing_display_string_ length]); | |
rohitrao (ping after 24h)
2017/03/03 21:53:51
If the range is invalid, we emphasize everything i
elawrence
2017/03/03 22:01:59
Yes, if the range is invalid, we apply the color (
| |
646 | |
647 [attributing_display_string_ | |
648 addAttribute:NSForegroundColorAttributeName | |
649 value:(emphasize) ? [field_ displayedTextColor] : BaseTextColor() | |
650 range:ns_range]; | |
651 } | |
652 | |
653 void OmniboxViewIOS::UpdateSchemeStyle(const gfx::Range& range) { | |
654 if (!range.IsValid()) | |
655 return; | |
656 | |
657 const security_state::SecurityLevel security_level = | |
658 controller()->GetToolbarModel()->GetSecurityLevel(false); | |
659 | |
660 if ((security_level == security_state::NONE) || | |
661 (security_level == security_state::HTTP_SHOW_WARNING)) { | |
662 return; | |
663 } | |
664 | |
665 DCHECK_NE(security_state::SECURITY_WARNING, security_level); | |
666 DCHECK_NE(security_state::SECURE_WITH_POLICY_INSTALLED_CERT, security_level); | |
667 | |
668 if (security_level == security_state::DANGEROUS) { | |
669 // Add a strikethrough through the scheme. | |
670 [attributing_display_string_ | |
671 addAttribute:NSStrikethroughStyleAttributeName | |
672 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
673 range:range.ToNSRange()]; | |
674 } | |
675 | |
676 UIColor* color = GetSecureTextColor(security_level, [field_ incognito]); | |
677 if (color) { | |
678 [attributing_display_string_ addAttribute:NSForegroundColorAttributeName | |
679 value:color | |
680 range:range.ToNSRange()]; | |
681 } | |
682 } | |
683 | |
637 NSAttributedString* OmniboxViewIOS::ApplyTextAttributes( | 684 NSAttributedString* OmniboxViewIOS::ApplyTextAttributes( |
638 const base::string16& text) { | 685 const base::string16& text) { |
639 NSMutableAttributedString* as = [[[NSMutableAttributedString alloc] | 686 NSMutableAttributedString* as = [[[NSMutableAttributedString alloc] |
640 initWithString:base::SysUTF16ToNSString(text)] autorelease]; | 687 initWithString:base::SysUTF16ToNSString(text)] autorelease]; |
641 url::Component scheme, host; | 688 // Cache a pointer to the attributed string to allow the superclass' |
642 AutocompleteInput::ParseForEmphasizeComponents( | 689 // virtual method invocations to add attributes. |
643 text, AutocompleteSchemeClassifierImpl(), &scheme, &host); | 690 DCHECK(attributing_display_string_ == nil); |
644 | 691 base::AutoReset<NSMutableAttributedString*> resetter( |
645 const bool emphasize = model()->CurrentTextIsURL() && (host.len > 0); | 692 &attributing_display_string_, as); |
646 if (emphasize) { | 693 UpdateTextStyle(text, AutocompleteSchemeClassifierImpl()); |
647 [as addAttribute:NSForegroundColorAttributeName | |
648 value:BaseTextColor() | |
649 range:NSMakeRange(0, [as length])]; | |
650 | |
651 [as addAttribute:NSForegroundColorAttributeName | |
652 value:[field_ displayedTextColor] | |
653 range:ComponentToNSRange(host)]; | |
654 | |
655 if (scheme.len > 0) { | |
656 UIColor* color = nil; | |
657 switch (controller_->GetToolbarModel()->GetSecurityLevel(false)) { | |
658 case security_state::NONE: | |
659 case security_state::HTTP_SHOW_WARNING: | |
660 break; | |
661 case security_state::SECURITY_WARNING: | |
662 // Don't color strikethrough schemes. See https://crbug.com/635004#c6 | |
663 if (![field_ incognito]) | |
664 color = WarningTextColor(); | |
665 [as addAttribute:NSStrikethroughStyleAttributeName | |
666 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
667 range:ComponentToNSRange(scheme)]; | |
668 break; | |
669 case security_state::SECURE: | |
670 case security_state::EV_SECURE: | |
671 color = [field_ incognito] ? IncognitoSecureTextColor() | |
672 : SecureTextColor(); | |
673 break; | |
674 case security_state::DANGEROUS: | |
675 // Don't color strikethrough schemes. See https://crbug.com/635004#c6 | |
676 if (![field_ incognito]) | |
677 color = ErrorTextColor(); | |
678 [as addAttribute:NSStrikethroughStyleAttributeName | |
679 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
680 range:ComponentToNSRange(scheme)]; | |
681 break; | |
682 case security_state::SECURE_WITH_POLICY_INSTALLED_CERT: | |
683 NOTREACHED(); | |
684 } | |
685 if (color) { | |
686 [as addAttribute:NSForegroundColorAttributeName | |
687 value:color | |
688 range:ComponentToNSRange(scheme)]; | |
689 } | |
690 } | |
691 } | |
692 return as; | 694 return as; |
693 } | 695 } |
694 | 696 |
695 void OmniboxViewIOS::UpdateAppearance() { | 697 void OmniboxViewIOS::UpdateAppearance() { |
696 // If Siri is thinking, treat that as user input being in progress. It is | 698 // If Siri is thinking, treat that as user input being in progress. It is |
697 // unsafe to modify the text field while voice entry is pending. | 699 // unsafe to modify the text field while voice entry is pending. |
698 if (model()->UpdatePermanentText()) { | 700 if (model()->UpdatePermanentText()) { |
699 // Revert everything to the baseline look. | 701 // Revert everything to the baseline look. |
700 RevertAll(); | 702 RevertAll(); |
701 } else if (!model()->has_focus() && | 703 } else if (!model()->has_focus() && |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
866 // is being left out for now because it was not present before the OmniboxView | 868 // is being left out for now because it was not present before the OmniboxView |
867 // rewrite. | 869 // rewrite. |
868 #if 0 | 870 #if 0 |
869 // When editing is in progress, the url text is not colored, so there is | 871 // When editing is in progress, the url text is not colored, so there is |
870 // nothing to emphasize. (Calling SetText() in that situation would also be | 872 // nothing to emphasize. (Calling SetText() in that situation would also be |
871 // harmful, as it would reset the carat position to the end of the text.) | 873 // harmful, as it would reset the carat position to the end of the text.) |
872 if (!IsEditingOrEmpty()) | 874 if (!IsEditingOrEmpty()) |
873 SetText(GetText()); | 875 SetText(GetText()); |
874 #endif | 876 #endif |
875 } | 877 } |
OLD | NEW |