Commit 9aa5149d authored by jzw's avatar jzw Committed by Commit Bot

Expose method to reset translate settings.

Had to refactor browser states to be global instances and force off the record
browser state to use an in-memory store instead of a json file persisted on disk.

BUG=739987

Review-Url: https://codereview.chromium.org/2965303002
Cr-Commit-Position: refs/heads/master@{#485392}
parent 019b6bf2
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "ios/web_view/internal/app/application_context.h" #include "ios/web_view/internal/app/application_context.h"
#import "ios/web_view/internal/cwv_user_content_controller_internal.h" #import "ios/web_view/internal/cwv_user_content_controller_internal.h"
#include "ios/web_view/internal/web_view_browser_state.h" #include "ios/web_view/internal/web_view_browser_state.h"
...@@ -32,14 +31,27 @@ ...@@ -32,14 +31,27 @@
@synthesize userContentController = _userContentController; @synthesize userContentController = _userContentController;
+ (instancetype)defaultConfiguration { + (instancetype)defaultConfiguration {
auto browserState = static CWVWebViewConfiguration* defaultConfiguration;
base::MakeUnique<ios_web_view::WebViewBrowserState>(false); static dispatch_once_t onceToken;
return [[self alloc] initWithBrowserState:std::move(browserState)]; dispatch_once(&onceToken, ^{
auto browserState =
base::MakeUnique<ios_web_view::WebViewBrowserState>(false);
defaultConfiguration =
[[self alloc] initWithBrowserState:std::move(browserState)];
});
return defaultConfiguration;
} }
+ (instancetype)incognitoConfiguration { + (instancetype)incognitoConfiguration {
auto browserState = base::MakeUnique<ios_web_view::WebViewBrowserState>(true); static CWVWebViewConfiguration* incognitoConfiguration;
return [[self alloc] initWithBrowserState:std::move(browserState)]; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
auto browserState =
base::MakeUnique<ios_web_view::WebViewBrowserState>(true);
incognitoConfiguration =
[[self alloc] initWithBrowserState:std::move(browserState)];
});
return incognitoConfiguration;
} }
+ (void)initialize { + (void)initialize {
...@@ -62,10 +74,14 @@ ...@@ -62,10 +74,14 @@
return self; return self;
} }
#pragma mark - Public Methods
- (BOOL)isPersistent { - (BOOL)isPersistent {
return !_browserState->IsOffTheRecord(); return !_browserState->IsOffTheRecord();
} }
#pragma mark - Private Methods
- (ios_web_view::WebViewBrowserState*)browserState { - (ios_web_view::WebViewBrowserState*)browserState {
return _browserState.get(); return _browserState.get();
} }
......
...@@ -11,8 +11,11 @@ ...@@ -11,8 +11,11 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "components/translate/core/browser/translate_download_manager.h" #include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_manager.h" #include "components/translate/core/browser/translate_manager.h"
#import "ios/web_view/internal/cwv_web_view_configuration_internal.h"
#include "ios/web_view/internal/pref_names.h"
#import "ios/web_view/internal/translate/cwv_translation_language_internal.h" #import "ios/web_view/internal/translate/cwv_translation_language_internal.h"
#import "ios/web_view/internal/translate/web_view_translate_client.h" #import "ios/web_view/internal/translate/web_view_translate_client.h"
#include "ios/web_view/internal/web_view_browser_state.h"
#import "ios/web_view/public/cwv_translation_controller_delegate.h" #import "ios/web_view/public/cwv_translation_controller_delegate.h"
#import "ios/web_view/public/cwv_translation_policy.h" #import "ios/web_view/public/cwv_translation_policy.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
...@@ -65,6 +68,17 @@ const NSInteger CWVTranslationErrorScriptLoadError = ...@@ -65,6 +68,17 @@ const NSInteger CWVTranslationErrorScriptLoadError =
@synthesize supportedLanguagesByCode = _supportedLanguagesByCode; @synthesize supportedLanguagesByCode = _supportedLanguagesByCode;
@synthesize webState = _webState; @synthesize webState = _webState;
#pragma mark - Class Methods
+ (void)resetTranslationPolicies {
CWVWebViewConfiguration* configuration =
[CWVWebViewConfiguration defaultConfiguration];
PrefService* prefService = configuration.browserState->GetPrefs();
translate::TranslatePrefs translatePrefs(prefService, prefs::kAcceptLanguages,
nullptr);
translatePrefs.ResetToDefaults();
}
#pragma mark - Internal Methods #pragma mark - Internal Methods
- (void)setWebState:(web::WebState*)webState { - (void)setWebState:(web::WebState*)webState {
...@@ -155,6 +169,7 @@ const NSInteger CWVTranslationErrorScriptLoadError = ...@@ -155,6 +169,7 @@ const NSInteger CWVTranslationErrorScriptLoadError =
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy - (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageLanguage:(CWVTranslationLanguage*)pageLanguage { forPageLanguage:(CWVTranslationLanguage*)pageLanguage {
DCHECK(!_webState->GetBrowserState()->IsOffTheRecord());
std::string languageCode = base::SysNSStringToUTF8(pageLanguage.languageCode); std::string languageCode = base::SysNSStringToUTF8(pageLanguage.languageCode);
switch (policy.type) { switch (policy.type) {
case CWVTranslationPolicyAsk: { case CWVTranslationPolicyAsk: {
...@@ -198,6 +213,7 @@ const NSInteger CWVTranslationErrorScriptLoadError = ...@@ -198,6 +213,7 @@ const NSInteger CWVTranslationErrorScriptLoadError =
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy - (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageHost:(NSString*)pageHost { forPageHost:(NSString*)pageHost {
DCHECK(!_webState->GetBrowserState()->IsOffTheRecord());
DCHECK(pageHost.length); DCHECK(pageHost.length);
switch (policy.type) { switch (policy.type) {
case CWVTranslationPolicyAsk: { case CWVTranslationPolicyAsk: {
......
...@@ -8,11 +8,13 @@ ...@@ -8,11 +8,13 @@
#include "base/base_paths.h" #include "base/base_paths.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "components/pref_registry/pref_registry_syncable.h" #include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/in_memory_pref_store.h"
#include "components/prefs/json_pref_store.h" #include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_filter.h" #include "components/prefs/pref_filter.h"
#include "components/prefs/pref_service_factory.h" #include "components/prefs/pref_service_factory.h"
...@@ -56,8 +58,13 @@ WebViewBrowserState::WebViewBrowserState(bool off_the_record) ...@@ -56,8 +58,13 @@ WebViewBrowserState::WebViewBrowserState(bool off_the_record)
JsonPrefStore::GetTaskRunnerForFile(path_, JsonPrefStore::GetTaskRunnerForFile(path_,
web::WebThread::GetBlockingPool()); web::WebThread::GetBlockingPool());
scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore( scoped_refptr<PersistentPrefStore> user_pref_store;
path_.Append(kPreferencesFilename), sequenced_task_runner, nullptr); if (off_the_record) {
user_pref_store = new InMemoryPrefStore();
} else {
user_pref_store = new JsonPrefStore(path_.Append(kPreferencesFilename),
sequenced_task_runner, nullptr);
}
PrefServiceFactory factory; PrefServiceFactory factory;
factory.set_user_prefs(user_pref_store); factory.set_user_prefs(user_pref_store);
......
...@@ -52,6 +52,10 @@ CWV_EXPORT ...@@ -52,6 +52,10 @@ CWV_EXPORT
@property(nonatomic, readonly) @property(nonatomic, readonly)
NSSet<CWVTranslationLanguage*>* supportedLanguages; NSSet<CWVTranslationLanguage*>* supportedLanguages;
// Resets all translation policies to default (CWVTranslationPolicyAsk).
// Only resets non-incognito settings.
+ (void)resetTranslationPolicies;
// Begins translation on the current page from |sourceLanguage| to // Begins translation on the current page from |sourceLanguage| to
// |targetLanguage|. These language parameters must be chosen from // |targetLanguage|. These language parameters must be chosen from
// |supportedLanguages|. Set |userInitiated| to YES if translation // |supportedLanguages|. Set |userInitiated| to YES if translation
...@@ -70,6 +74,7 @@ CWV_EXPORT ...@@ -70,6 +74,7 @@ CWV_EXPORT
// Sets or retrieves translation policies associated with a specified language. // Sets or retrieves translation policies associated with a specified language.
// |pageLanguage| should be the language code of the language. // |pageLanguage| should be the language code of the language.
// Not supported in incognito mode.
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy - (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageLanguage:(CWVTranslationLanguage*)pageLanguage; forPageLanguage:(CWVTranslationLanguage*)pageLanguage;
- (CWVTranslationPolicy*)translationPolicyForPageLanguage: - (CWVTranslationPolicy*)translationPolicyForPageLanguage:
...@@ -77,6 +82,7 @@ CWV_EXPORT ...@@ -77,6 +82,7 @@ CWV_EXPORT
// Sets or retrieves translation policies associated with a specified page. // Sets or retrieves translation policies associated with a specified page.
// |pageHost| should be the hostname of the website. Must not be empty. // |pageHost| should be the hostname of the website. Must not be empty.
// Not supported in incognito mode.
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy - (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageHost:(NSString*)pageHost; forPageHost:(NSString*)pageHost;
- (CWVTranslationPolicy*)translationPolicyForPageHost:(NSString*)pageHost; - (CWVTranslationPolicy*)translationPolicyForPageHost:(NSString*)pageHost;
......
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