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 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 } | 615 } |
628 | 616 |
629 bool OmniboxViewIOS::CanCopyURL() { | 617 bool OmniboxViewIOS::CanCopyURL() { |
630 return false; | 618 return false; |
631 } | 619 } |
632 | 620 |
633 void OmniboxViewIOS::WillPaste() { | 621 void OmniboxViewIOS::WillPaste() { |
634 model()->OnPaste(); | 622 model()->OnPaste(); |
635 } | 623 } |
636 | 624 |
625 // static | |
626 UIColor* OmniboxViewIOS::GetSecureTextColor( | |
627 security_state::SecurityLevel security_level, | |
628 bool in_dark_mode) { | |
629 if (security_level == security_state::EV_SECURE || | |
630 security_level == security_state::SECURE) { | |
631 return in_dark_mode ? IncognitoSecureTextColor() : SecureTextColor(); | |
632 } | |
633 | |
634 // Don't color strikethrough in dark mode. See https://crbug.com/635004#c6 | |
635 if (security_level == security_state::DANGEROUS && !in_dark_mode) | |
636 return ErrorTextColor(); | |
637 | |
638 return nil; | |
639 } | |
640 | |
641 void OmniboxViewIOS::SetEmphasis(bool emphasize, gfx::Range range) { | |
642 NSRange ns_range = range.IsValid() | |
643 ? range.ToNSRange() | |
644 : NSMakeRange(0, [attributing_display_string_ length]); | |
645 | |
646 [attributing_display_string_ | |
647 addAttribute:NSForegroundColorAttributeName | |
648 value:(emphasize) ? [field_ displayedTextColor] : BaseTextColor() | |
649 range:ns_range]; | |
650 } | |
651 | |
652 void OmniboxViewIOS::UpdateSchemeEmphasis(gfx::Range range) { | |
653 if (!range.IsValid()) | |
654 return; | |
655 | |
656 const security_state::SecurityLevel security_level = | |
657 controller()->GetToolbarModel()->GetSecurityLevel(false); | |
658 | |
659 if ((security_level == security_state::NONE) || | |
660 (security_level == security_state::HTTP_SHOW_WARNING)) | |
Eugene But (OOO till 7-30)
2017/02/28 23:04:38
nit: Do you want to add braces, because if stateme
elawrence
2017/03/01 21:49:02
Ok. I didn't see a style guide opinion on this one
Eugene But (OOO till 7-30)
2017/03/01 22:31:19
C++ Style Guide is quite vague on this: "In genera
Peter Kasting
2017/03/01 23:18:47
Read "statement" as specifically "loop body" in th
| |
661 return; | |
662 | |
663 DCHECK(security_level != security_state::SECURITY_WARNING); | |
Peter Kasting
2017/03/01 02:39:11
Nit: DCHECK_NE(security_state::SECURITY_WARNING, s
elawrence
2017/03/01 21:49:02
Done.
| |
664 DCHECK(security_level != security_state::SECURE_WITH_POLICY_INSTALLED_CERT); | |
665 | |
666 if (security_level == security_state::DANGEROUS) { | |
667 // Add a strikethrough through the scheme. | |
668 [attributing_display_string_ | |
669 addAttribute:NSStrikethroughStyleAttributeName | |
670 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
671 range:range.ToNSRange()]; | |
672 } | |
673 | |
674 UIColor* color = GetSecureTextColor(security_level, [field_ incognito]); | |
675 if (color) { | |
676 [attributing_display_string_ addAttribute:NSForegroundColorAttributeName | |
677 value:color | |
678 range:range.ToNSRange()]; | |
679 } | |
680 } | |
681 | |
637 NSAttributedString* OmniboxViewIOS::ApplyTextAttributes( | 682 NSAttributedString* OmniboxViewIOS::ApplyTextAttributes( |
638 const base::string16& text) { | 683 const base::string16& text) { |
639 NSMutableAttributedString* as = [[[NSMutableAttributedString alloc] | 684 NSMutableAttributedString* as = [[[NSMutableAttributedString alloc] |
640 initWithString:base::SysUTF16ToNSString(text)] autorelease]; | 685 initWithString:base::SysUTF16ToNSString(text)] autorelease]; |
641 url::Component scheme, host; | |
642 AutocompleteInput::ParseForEmphasizeComponents( | |
643 text, AutocompleteSchemeClassifierImpl(), &scheme, &host); | |
644 | 686 |
645 const bool emphasize = model()->CurrentTextIsURL() && (host.len > 0); | 687 // Cache a pointer to the attributed string to allow the superclass' |
646 if (emphasize) { | 688 // virtual method invocations to add attributes. |
647 [as addAttribute:NSForegroundColorAttributeName | 689 attributing_display_string_ = as; |
Peter Kasting
2017/03/01 02:39:11
Nit: See Mac comment
elawrence
2017/03/01 21:49:02
Done.
| |
648 value:BaseTextColor() | 690 ApplyEmphasis(text, AutocompleteSchemeClassifierImpl()); |
649 range:NSMakeRange(0, [as length])]; | 691 attributing_display_string_ = nullptr; |
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 break; | |
660 case security_state::SECURITY_WARNING: | |
661 // Don't color strikethrough schemes. See https://crbug.com/635004#c6 | |
662 if (![field_ incognito]) | |
663 color = WarningTextColor(); | |
664 [as addAttribute:NSStrikethroughStyleAttributeName | |
665 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
666 range:ComponentToNSRange(scheme)]; | |
667 break; | |
668 case security_state::SECURE: | |
669 case security_state::EV_SECURE: | |
670 color = [field_ incognito] ? IncognitoSecureTextColor() | |
671 : SecureTextColor(); | |
672 break; | |
673 case security_state::DANGEROUS: | |
674 // Don't color strikethrough schemes. See https://crbug.com/635004#c6 | |
675 if (![field_ incognito]) | |
676 color = ErrorTextColor(); | |
677 [as addAttribute:NSStrikethroughStyleAttributeName | |
678 value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] | |
679 range:ComponentToNSRange(scheme)]; | |
680 break; | |
681 case security_state::HTTP_SHOW_WARNING: | |
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; | 692 return as; |
693 } | 693 } |
694 | 694 |
695 void OmniboxViewIOS::UpdateAppearance() { | 695 void OmniboxViewIOS::UpdateAppearance() { |
696 // If Siri is thinking, treat that as user input being in progress. It is | 696 // 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. | 697 // unsafe to modify the text field while voice entry is pending. |
698 if (model()->UpdatePermanentText()) { | 698 if (model()->UpdatePermanentText()) { |
699 // Revert everything to the baseline look. | 699 // Revert everything to the baseline look. |
700 RevertAll(); | 700 RevertAll(); |
701 } else if (!model()->has_focus() && | 701 } 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 | 866 // is being left out for now because it was not present before the OmniboxView |
867 // rewrite. | 867 // rewrite. |
868 #if 0 | 868 #if 0 |
869 // When editing is in progress, the url text is not colored, so there is | 869 // 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 | 870 // 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.) | 871 // harmful, as it would reset the carat position to the end of the text.) |
872 if (!IsEditingOrEmpty()) | 872 if (!IsEditingOrEmpty()) |
873 SetText(GetText()); | 873 SetText(GetText()); |
874 #endif | 874 #endif |
875 } | 875 } |
OLD | NEW |