Commit 7f9d9790 authored by Robbie Gibson's avatar Robbie Gibson Committed by Commit Bot

[iOS] Store default search engine search-by-image status in UserDefaults

This value is needed in extensions, so we can use NSUserDefaults to
store it.

Bug: 913958
Change-Id: I4cacfb56c62e84e97a6529797bb97473715b5eb0
Reviewed-on: https://chromium-review.googlesource.com/c/1488911Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Cr-Commit-Position: refs/heads/master@{#636918}
parent 8ae59ba1
......@@ -196,6 +196,7 @@ source_set("app_internal") {
"//ios/chrome/browser/payments:constants",
"//ios/chrome/browser/reading_list",
"//ios/chrome/browser/search_engines",
"//ios/chrome/browser/search_engines:extension_search_engine_data_updater",
"//ios/chrome/browser/share_extension",
"//ios/chrome/browser/signin",
"//ios/chrome/browser/snapshots",
......
......@@ -93,6 +93,7 @@
#include "ios/chrome/browser/pref_names.h"
#import "ios/chrome/browser/reading_list/reading_list_download_service.h"
#import "ios/chrome/browser/reading_list/reading_list_download_service_factory.h"
#import "ios/chrome/browser/search_engines/extension_search_engine_data_updater.h"
#include "ios/chrome/browser/search_engines/search_engines_util.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#import "ios/chrome/browser/share_extension/share_extension_service.h"
......@@ -389,6 +390,11 @@ enum class EnterTabSwitcherSnapshotResult {
// Registrar for pref changes notifications to the local state.
PrefChangeRegistrar _localStatePrefChangeRegistrar;
// Updates data about the current default search engine to be accessed in
// extensions.
std::unique_ptr<ExtensionSearchEngineDataUpdater>
_extensionSearchEngineDataUpdater;
// The class in charge of showing/hiding the memory debugger when the
// appropriate pref changes.
MemoryDebuggerManager* _memoryDebuggerManager;
......@@ -968,6 +974,8 @@ enum class EnterTabSwitcherSnapshotResult {
[_browserViewWrangler shutdown];
_browserViewWrangler = nil;
_extensionSearchEngineDataUpdater = nullptr;
[_historyCoordinator stop];
_historyCoordinator = nil;
......@@ -1039,12 +1047,19 @@ enum class EnterTabSwitcherSnapshotResult {
&_localStatePrefChangeRegistrar);
// Calls the onPreferenceChanged function in case there was
// a
// change to the observed preferences before the observer
// a change to the observed preferences before the observer
// bridge was set up.
[self onPreferenceChanged:metrics::prefs::
kMetricsReportingEnabled];
[self onPreferenceChanged:prefs::kMetricsReportingWifiOnly];
// Track changes to default search engine.
TemplateURLService* service =
ios::TemplateURLServiceFactory::GetForBrowserState(
_mainBrowserState);
_extensionSearchEngineDataUpdater =
std::make_unique<ExtensionSearchEngineDataUpdater>(
service);
}];
}
......
......@@ -172,9 +172,7 @@ enum SearchExtensionAction {
+ (instancetype)newExtensionCommandAppStartupParametersFromWithURL:(NSURL*)url
fromSourceApplication:
(NSString*)appId {
NSString* appGroup = app_group::ApplicationGroup();
NSUserDefaults* sharedDefaults =
[[NSUserDefaults alloc] initWithSuiteName:appGroup];
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
NSString* commandDictionaryPreference =
base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandPreference);
......
......@@ -2,8 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//ios/web/js_compile.gni")
import("//build/config/features.gni")
import("//ios/web/js_compile.gni")
import("//rlz/buildflags/buildflags.gni")
source_set("feature_flags") {
......@@ -65,14 +65,31 @@ source_set("search_engines") {
}
}
source_set("extension_search_engine_data_updater") {
sources = [
"extension_search_engine_data_updater.h",
"extension_search_engine_data_updater.mm",
]
deps = [
":search_engines",
"//base",
"//components/search_engines",
"//ios/chrome/common/app_group",
]
configs += [ "//build/config/compiler:enable_arc" ]
}
source_set("unit_tests") {
configs += [ "//build/config/compiler:enable_arc" ]
testonly = true
sources = [
"extension_search_engine_data_updater_unittest.mm",
"search_engine_js_unittest.mm",
"search_engine_tab_helper_unittest.mm",
]
deps = [
":extension_search_engine_data_updater",
":search_engine_js",
":search_engines",
"//base:base",
......@@ -83,6 +100,7 @@ source_set("unit_tests") {
"//ios/chrome/browser/browser_state:test_support",
"//ios/chrome/browser/favicon:favicon",
"//ios/chrome/browser/web:test_support",
"//ios/chrome/common/app_group",
"//ios/web",
"//ios/web/public/test",
"//ios/web/public/test/fakes",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_SEARCH_ENGINES_EXTENSION_SEARCH_ENGINE_DATA_UPDATER_H_
#define IOS_CHROME_BROWSER_SEARCH_ENGINES_EXTENSION_SEARCH_ENGINE_DATA_UPDATER_H_
#include "components/search_engines/template_url_service_observer.h"
class TemplateURLService;
// Extensions need to know data about the current default search provider. This
// class observes that change and writes the necessary data to |NSUserDefaults|.
class ExtensionSearchEngineDataUpdater : public TemplateURLServiceObserver {
public:
explicit ExtensionSearchEngineDataUpdater(TemplateURLService* urlService);
~ExtensionSearchEngineDataUpdater() override;
private:
// TemplateURLServiceObserver
void OnTemplateURLServiceChanged() override;
TemplateURLService* templateURLService_; // weak
};
#endif // IOS_CHROME_BROWSER_SEARCH_ENGINES_EXTENSION_SEARCH_ENGINE_DATA_UPDATER_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/search_engines/extension_search_engine_data_updater.h"
#include "base/strings/sys_string_conversions.h"
#include "components/search_engines/template_url_service.h"
#import "ios/chrome/browser/search_engines/search_engines_util.h"
#include "ios/chrome/common/app_group/app_group_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
ExtensionSearchEngineDataUpdater::ExtensionSearchEngineDataUpdater(
TemplateURLService* urlService)
: templateURLService_(urlService) {
templateURLService_->AddObserver(this);
OnTemplateURLServiceChanged();
}
ExtensionSearchEngineDataUpdater::~ExtensionSearchEngineDataUpdater() {
templateURLService_->RemoveObserver(this);
}
void ExtensionSearchEngineDataUpdater::OnTemplateURLServiceChanged() {
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
BOOL supportsSearchByImage =
search_engines::SupportsSearchByImage(templateURLService_);
NSString* userDefaultsKey =
base::SysUTF8ToNSString(app_group::kChromeAppGroupSupportsSearchByImage);
[sharedDefaults setBool:supportsSearchByImage forKey:userDefaultsKey];
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/search_engines/extension_search_engine_data_updater.h"
#include "base/macros.h"
#include "base/strings/sys_string_conversions.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_data.h"
#include "components/search_engines/template_url_service.h"
#include "ios/chrome/common/app_group/app_group_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
// Test fixture for ExtensionSearchEngineDataUpdater class.
class ExtensionSearchEngineDataUpdaterTest : public PlatformTest {
protected:
ExtensionSearchEngineDataUpdaterTest()
: search_by_image_key_(base::SysUTF8ToNSString(
app_group::kChromeAppGroupSupportsSearchByImage)) {}
void SetUp() override {
PlatformTest::SetUp();
template_url_service_.reset(new TemplateURLService(nullptr, 0));
template_url_service_->Load();
observer_.reset(
new ExtensionSearchEngineDataUpdater(template_url_service_.get()));
NSUserDefaults* shared_defaults = app_group::GetGroupUserDefaults();
[shared_defaults setBool:NO forKey:search_by_image_key_];
}
bool StoredSupportsSearchByImage() {
NSUserDefaults* shared_defaults = app_group::GetGroupUserDefaults();
return [shared_defaults boolForKey:search_by_image_key_];
}
std::unique_ptr<TemplateURLService> template_url_service_;
private:
std::unique_ptr<ExtensionSearchEngineDataUpdater> observer_;
NSString* search_by_image_key_;
};
TEST_F(ExtensionSearchEngineDataUpdaterTest, AddSupportedSearchEngine) {
ASSERT_FALSE(StoredSupportsSearchByImage());
const char kImageSearchURL[] = "http://foo.com/sbi";
const char kPostParamsString[] = "image_content={google:imageThumbnail}";
TemplateURLData supported_template_url_data{};
supported_template_url_data.image_url = kImageSearchURL;
supported_template_url_data.image_url_post_params = kPostParamsString;
TemplateURL supported_template_url(supported_template_url_data);
template_url_service_->SetUserSelectedDefaultSearchProvider(
&supported_template_url);
ASSERT_TRUE(StoredSupportsSearchByImage());
}
TEST_F(ExtensionSearchEngineDataUpdaterTest, AddUnsupportedSearchEngine) {
ASSERT_FALSE(StoredSupportsSearchByImage());
TemplateURLData unsupported_template_url_data{};
TemplateURL unsupported_template_url(unsupported_template_url_data);
template_url_service_->SetUserSelectedDefaultSearchProvider(
&unsupported_template_url);
ASSERT_FALSE(StoredSupportsSearchByImage());
}
......@@ -86,6 +86,10 @@ extern const char kChromeAppGroupCommandDataPreference[];
// if the command requires one.
extern const char kChromeAppGroupCommandIndexPreference[];
// The key of a preference containing whether the current default search engine
// supports Search by Image.
extern const char kChromeAppGroupSupportsSearchByImage[];
// The key of a preference containing Chrome client ID reported in the metrics
// client ID. If the user does not opt in, this value must be cleared from the
// shared user defaults.
......
......@@ -38,6 +38,8 @@ const char kChromeAppGroupFocusOmniboxCommand[] = "focusomnibox";
const char kChromeAppGroupIncognitoSearchCommand[] = "incognitosearch";
const char kChromeAppGroupQRScannerCommand[] = "qrscanner";
const char kChromeAppGroupSupportsSearchByImage[] = "supportsSearchByImage";
const char kChromeAppClientID[] = "ClientID";
const char kUserMetricsEnabledDate[] = "UserMetricsEnabledDate";
const char kInstallDate[] = "InstallDate";
......
......@@ -35,6 +35,8 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
@property(nonatomic, strong)
ClipboardRecentContentImplIOS* clipboardRecentContent;
@property(nonatomic, copy, nullable) NSDictionary* fieldTrialValues;
// Whether the current default search engine supports search by image
@property(nonatomic, assign) BOOL supportsSearchByImage;
@property(nonatomic, readonly) BOOL copiedContentBehaviorEnabled;
// Updates the widget with latest data from the clipboard. Returns whether any
......@@ -136,6 +138,11 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
base::SysUTF8ToNSString(app_group::kChromeExtensionFieldTrialPreference);
self.fieldTrialValues = [sharedDefaults dictionaryForKey:fieldTrialKey];
NSString* supportsSearchByImageKey =
base::SysUTF8ToNSString(app_group::kChromeAppGroupSupportsSearchByImage);
self.supportsSearchByImage =
[sharedDefaults boolForKey:supportsSearchByImageKey];
NSString* copiedText;
UIImage* copiedImage;
CopiedContentType type = CopiedContentTypeNone;
......@@ -171,7 +178,7 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
// image.
// TODO(crbug.com/932116): Can be removed when the flag is cleaned up.
- (UIImage*)getCopiedImageUsingFlag {
if (!self.copiedContentBehaviorEnabled) {
if (!self.copiedContentBehaviorEnabled || !self.supportsSearchByImage) {
return nil;
}
return [self.clipboardRecentContent recentImageFromClipboard];
......
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