Commit d7f9e639 authored by Robbie Gibson's avatar Robbie Gibson Committed by Commit Bot

Search Copied Img from omnibox longpress looks at default search engine

Before, the option would appear regardless of the default search engine,
but would DCHECK if the default search engine didn't support search-by-
image. Now, the option doesn't appear in that case.

Bug: 935386
Change-Id: Ic1e1ef896674b72bd8de8f159cf8afb9718c93a5
Reviewed-on: https://chromium-review.googlesource.com/c/1486093
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636751}
parent 0123e953
......@@ -113,6 +113,9 @@ void UpdateSearchEnginesIfNeeded(PrefService* preferences,
}
bool SupportsSearchByImage(TemplateURLService* service) {
if (!service) {
return false;
}
const TemplateURL* default_url = service->GetDefaultSearchProvider();
return default_url && !default_url->image_url().empty() &&
default_url->image_url_ref().IsValid(service->search_terms_data());
......
......@@ -26,6 +26,10 @@
// after -updateLocationText. Used for triggering NTP-specific location bar UI.
- (void)updateAfterNavigatingToNTP;
// Notifies the consumer to update after the search-by-image support status
// changes. (This is usually when the default search engine changes).
- (void)updateSearchByImageSupported:(BOOL)searchByImageSupported;
@end
#endif // IOS_CHROME_BROWSER_UI_LOCATION_BAR_LOCATION_BAR_CONSUMER_H_
......@@ -159,6 +159,8 @@ const int kLocationAuthorizationStatusCount = 4;
self.mediator = [[LocationBarMediator alloc]
initWithLocationBarModel:[self locationBarModel]];
self.mediator.webStateList = self.webStateList;
self.mediator.templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(self.browserState);
self.mediator.consumer = self;
_fullscreenObserver =
......@@ -371,6 +373,10 @@ const int kLocationAuthorizationStatusCount = 4;
[self.viewController setShareButtonEnabled:shareable];
}
- (void)updateSearchByImageSupported:(BOOL)searchByImageSupported {
self.viewController.searchByImageEnabled = searchByImageSupported;
}
#pragma mark - private
// Returns a dictionary with variation headers for qualified URLs. Can be empty.
......
......@@ -8,6 +8,7 @@
#import <UIKit/UIKit.h>
@protocol LocationBarConsumer;
class TemplateURLService;
class WebStateList;
class LocationBarModel;
......@@ -26,6 +27,10 @@ class LocationBarModel;
// the security state.
@property(nonatomic, assign, readonly) LocationBarModel* locationBarModel;
// The templateURLService used by this mediator to extract whether the default
// search engine supports search-by-image.
@property(nonatomic, assign) TemplateURLService* templateURLService;
// The consumer for this object. This can change during the lifetime of this
// object and may be nil.
@property(nonatomic, weak) id<LocationBarConsumer> consumer;
......
......@@ -8,6 +8,8 @@
#include "base/strings/sys_string_conversions.h"
#include "components/omnibox/browser/location_bar_model.h"
#include "ios/chrome/browser/chrome_url_constants.h"
#import "ios/chrome/browser/search_engines/search_engine_observer_bridge.h"
#import "ios/chrome/browser/search_engines/search_engines_util.h"
#include "ios/chrome/browser/ssl/ios_security_state_tab_helper.h"
#import "ios/chrome/browser/ui/location_bar/location_bar_consumer.h"
#import "ios/chrome/browser/ui/ntp/ntp_util.h"
......@@ -28,21 +30,22 @@
#error "This file requires ARC support."
#endif
@interface LocationBarMediator ()<CRWWebStateObserver, WebStateListObserving>
@interface LocationBarMediator () <CRWWebStateObserver,
WebStateListObserving,
SearchEngineObserving>
// The current web state associated with the toolbar.
@property(nonatomic, assign) web::WebState* webState;
// Whether the current default search engine supports search by image.
@property(nonatomic, assign) BOOL searchEngineSupportsSearchByImage;
@end
@implementation LocationBarMediator {
std::unique_ptr<web::WebStateObserverBridge> _webStateObserver;
std::unique_ptr<WebStateListObserverBridge> _webStateListObserver;
std::unique_ptr<SearchEngineObserverBridge> _searchEngineObserver;
}
@synthesize consumer = _consumer;
@synthesize webState = _webState;
@synthesize webStateList = _webStateList;
@synthesize locationBarModel = _locationBarModel;
- (instancetype)initWithLocationBarModel:(LocationBarModel*)locationBarModel {
DCHECK(locationBarModel);
self = [super init];
......@@ -50,6 +53,7 @@
_locationBarModel = locationBarModel;
_webStateObserver = std::make_unique<web::WebStateObserverBridge>(self);
_webStateListObserver = std::make_unique<WebStateListObserverBridge>(self);
_searchEngineSupportsSearchByImage = NO;
}
return self;
}
......@@ -142,6 +146,13 @@
[self.consumer defocusOmnibox];
}
#pragma mark - SearchEngineObserving
- (void)searchEngineChanged {
self.searchEngineSupportsSearchByImage =
search_engines::SupportsSearchByImage(self.templateURLService);
}
#pragma mark - Setters
- (void)setWebState:(web::WebState*)webState {
......@@ -167,6 +178,8 @@
[self notifyConsumerOfChangedLocation];
[self notifyConsumerOfChangedSecurityIcon];
}
[consumer
updateSearchByImageSupported:self.searchEngineSupportsSearchByImage];
}
- (void)setWebStateList:(WebStateList*)webStateList {
......@@ -179,6 +192,25 @@
_webStateList->AddObserver(_webStateListObserver.get());
}
- (void)setTemplateURLService:(TemplateURLService*)templateURLService {
_templateURLService = templateURLService;
self.searchEngineSupportsSearchByImage =
search_engines::SupportsSearchByImage(templateURLService);
_searchEngineObserver =
std::make_unique<SearchEngineObserverBridge>(self, templateURLService);
}
- (void)setSearchEngineSupportsSearchByImage:
(BOOL)searchEngineSupportsSearchByImage {
BOOL supportChanged =
_searchEngineSupportsSearchByImage != searchEngineSupportsSearchByImage;
_searchEngineSupportsSearchByImage = searchEngineSupportsSearchByImage;
if (supportChanged) {
[self.consumer
updateSearchByImageSupported:searchEngineSupportsSearchByImage];
}
}
#pragma mark - private
- (void)notifyConsumerOfChangedLocation {
......
......@@ -76,6 +76,10 @@
// and adds the voice search button to the empty textfield.
@property(nonatomic, assign) BOOL voiceSearchEnabled;
// Whether the default search engine supports search-by-image. This controls the
// edit menu option to do an image search.
@property(nonatomic, assign) BOOL searchByImageEnabled;
@end
#endif // IOS_CHROME_BROWSER_UI_LOCATION_BAR_LOCATION_BAR_VIEW_CONTROLLER_H_
......@@ -491,7 +491,8 @@ typedef NS_ENUM(int, TrailingButtonState) {
ClipboardRecentContent* clipboardRecentContent =
ClipboardRecentContent::GetInstance();
DCHECK(base::FeatureList::IsEnabled(kCopiedContentBehavior));
if (clipboardRecentContent->GetRecentImageFromClipboard().has_value()) {
if (self.searchByImageEnabled &&
clipboardRecentContent->GetRecentImageFromClipboard().has_value()) {
return action == @selector(searchCopiedImage:);
}
if (clipboardRecentContent->GetRecentURLFromClipboard().has_value()) {
......
......@@ -13,6 +13,10 @@
// highlighted autocomplete result.
- (void)updateAutocompleteIcon:(UIImage*)icon;
// Notifies the consumer to update after the search-by-image support status
// changes. (This is usually when the default search engine changes).
- (void)updateSearchByImageSupported:(BOOL)searchByImageSupported;
@end
#endif // IOS_CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_CONSUMER_H_
......@@ -11,6 +11,7 @@
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/strings/grit/components_strings.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#import "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/ui/commands/load_query_commands.h"
#import "ios/chrome/browser/ui/location_bar/location_bar_constants.h"
#import "ios/chrome/browser/ui/omnibox/omnibox_mediator.h"
......@@ -60,14 +61,16 @@
self.viewController =
[[OmniboxViewController alloc] initWithIncognito:isIncognito];
self.viewController.defaultLeadingImage =
GetOmniboxSuggestionIcon(DEFAULT_FAVICON);
self.viewController.emptyTextLeadingImage = GetOmniboxSuggestionIcon(SEARCH);
self.viewController.dispatcher =
static_cast<id<BrowserCommands, LoadQueryCommands, OmniboxFocuser>>(
self.dispatcher);
self.mediator = [[OmniboxMediator alloc] init];
self.mediator.templateURLService =
ios::TemplateURLServiceFactory::GetForBrowserState(self.browserState);
self.mediator.consumer = self.viewController;
DCHECK(self.editController);
......
......@@ -10,10 +10,15 @@
#import "ios/chrome/browser/ui/omnibox/omnibox_left_image_consumer.h"
@protocol OmniboxConsumer;
class TemplateURLService;
// A mediator object that updates the omnibox according to the model changes.
@interface OmniboxMediator : NSObject<OmniboxLeftImageConsumer>
// The templateURLService used by this mediator to extract whether the default
// search engine supports search-by-image.
@property(nonatomic, assign) TemplateURLService* templateURLService;
// The consumer for this object. This can change during the lifetime of this
// object and may be nil.
@property(nonatomic, weak) id<OmniboxConsumer> consumer;
......
......@@ -5,6 +5,8 @@
#import "ios/chrome/browser/ui/omnibox/omnibox_mediator.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/search_engines/search_engine_observer_bridge.h"
#import "ios/chrome/browser/search_engines/search_engines_util.h"
#import "ios/chrome/browser/ui/omnibox/omnibox_consumer.h"
#import "ios/chrome/browser/ui/omnibox/omnibox_util.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
......@@ -13,8 +15,58 @@
#error "This file requires ARC support."
#endif
@implementation OmniboxMediator
@synthesize consumer = _consumer;
@interface OmniboxMediator () <SearchEngineObserving>
// Whether the current default search engine supports search-by-image.
@property(nonatomic, assign) BOOL searchEngineSupportsSearchByImage;
@end
@implementation OmniboxMediator {
std::unique_ptr<SearchEngineObserverBridge> _searchEngineObserver;
}
- (instancetype)init {
self = [super init];
if (self) {
_searchEngineSupportsSearchByImage = NO;
}
return self;
}
#pragma mark - Setters
- (void)setConsumer:(id<OmniboxConsumer>)consumer {
_consumer = consumer;
[consumer
updateSearchByImageSupported:self.searchEngineSupportsSearchByImage];
}
- (void)setTemplateURLService:(TemplateURLService*)templateURLService {
_templateURLService = templateURLService;
self.searchEngineSupportsSearchByImage =
search_engines::SupportsSearchByImage(templateURLService);
_searchEngineObserver =
std::make_unique<SearchEngineObserverBridge>(self, templateURLService);
}
- (void)setSearchEngineSupportsSearchByImage:
(BOOL)searchEngineSupportsSearchByImage {
BOOL supportChanged = self.searchEngineSupportsSearchByImage !=
searchEngineSupportsSearchByImage;
_searchEngineSupportsSearchByImage = searchEngineSupportsSearchByImage;
if (supportChanged) {
[self.consumer
updateSearchByImageSupported:searchEngineSupportsSearchByImage];
}
}
#pragma mark - SearchEngineObserving
- (void)searchEngineChanged {
self.searchEngineSupportsSearchByImage =
search_engines::SupportsSearchByImage(self.templateURLService);
}
#pragma mark - OmniboxLeftImageConsumer
......
......@@ -36,15 +36,15 @@ const CGFloat kClearButtonSize = 28.0f;
// Override of UIViewController's view with a different type.
@property(nonatomic, strong) OmniboxContainerView* view;
// Whether the default search engine supports search-by-image. This controls the
// edit menu option to do an image search.
@property(nonatomic, assign) BOOL searchByImageEnabled;
@property(nonatomic, assign) BOOL incognito;
@end
@implementation OmniboxViewController
@synthesize incognito = _incognito;
@synthesize dispatcher = _dispatcher;
@synthesize defaultLeadingImage = _defaultLeadingImage;
@synthesize emptyTextLeadingImage = _emptyTextLeadingImage;
@dynamic view;
- (instancetype)initWithIncognito:(BOOL)isIncognito {
......@@ -143,6 +143,10 @@ const CGFloat kClearButtonSize = 28.0f;
[self.view setLeadingImage:icon];
}
- (void)updateSearchByImageSupported:(BOOL)searchByImageSupported {
self.searchByImageEnabled = searchByImageSupported;
}
#pragma mark - EditViewAnimatee
- (void)setLeadingIconFaded:(BOOL)faded {
......@@ -268,7 +272,8 @@ const CGFloat kClearButtonSize = 28.0f;
action == @selector(searchCopiedText:)) {
ClipboardRecentContent* clipboardRecentContent =
ClipboardRecentContent::GetInstance();
if (clipboardRecentContent->GetRecentImageFromClipboard().has_value()) {
if (self.searchByImageEnabled &&
clipboardRecentContent->GetRecentImageFromClipboard().has_value()) {
return action == @selector(searchCopiedImage:);
}
if (clipboardRecentContent->GetRecentURLFromClipboard().has_value()) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment