| 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]); |
| 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; | |
| 642 AutocompleteInput::ParseForEmphasizeComponents( | |
| 643 text, AutocompleteSchemeClassifierImpl(), &scheme, &host); | |
| 644 | 688 |
| 645 const bool emphasize = model()->CurrentTextIsURL() && (host.len > 0); | 689 // Cache a pointer to the attributed string to allow the superclass' |
| 646 if (emphasize) { | 690 // virtual method invocations to add attributes. |
| 647 [as addAttribute:NSForegroundColorAttributeName | 691 DCHECK(attributing_display_string_ == nil); |
| 648 value:BaseTextColor() | 692 base::AutoReset<NSMutableAttributedString*> resetter( |
| 649 range:NSMakeRange(0, [as length])]; | 693 &attributing_display_string_, as); |
| 650 | 694 UpdateTextStyle(text, AutocompleteSchemeClassifierImpl()); |
| 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; | 695 return as; |
| 693 } | 696 } |
| 694 | 697 |
| 695 void OmniboxViewIOS::UpdateAppearance() { | 698 void OmniboxViewIOS::UpdateAppearance() { |
| 696 // If Siri is thinking, treat that as user input being in progress. It is | 699 // 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. | 700 // unsafe to modify the text field while voice entry is pending. |
| 698 if (model()->UpdatePermanentText()) { | 701 if (model()->UpdatePermanentText()) { |
| 699 // Revert everything to the baseline look. | 702 // Revert everything to the baseline look. |
| 700 RevertAll(); | 703 RevertAll(); |
| 701 } else if (!model()->has_focus() && | 704 } 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 | 869 // is being left out for now because it was not present before the OmniboxView |
| 867 // rewrite. | 870 // rewrite. |
| 868 #if 0 | 871 #if 0 |
| 869 // When editing is in progress, the url text is not colored, so there is | 872 // 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 | 873 // 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.) | 874 // harmful, as it would reset the carat position to the end of the text.) |
| 872 if (!IsEditingOrEmpty()) | 875 if (!IsEditingOrEmpty()) |
| 873 SetText(GetText()); | 876 SetText(GetText()); |
| 874 #endif | 877 #endif |
| 875 } | 878 } |
| OLD | NEW |