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 @@
#include "base/memory/ptr_util.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"
#import "ios/web_view/internal/cwv_user_content_controller_internal.h"
#include "ios/web_view/internal/web_view_browser_state.h"
......@@ -32,14 +31,27 @@
@synthesize userContentController = _userContentController;
+ (instancetype)defaultConfiguration {
auto browserState =
base::MakeUnique<ios_web_view::WebViewBrowserState>(false);
return [[self alloc] initWithBrowserState:std::move(browserState)];
static CWVWebViewConfiguration* defaultConfiguration;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
auto browserState =
base::MakeUnique<ios_web_view::WebViewBrowserState>(false);
defaultConfiguration =
[[self alloc] initWithBrowserState:std::move(browserState)];
});
return defaultConfiguration;
}
+ (instancetype)incognitoConfiguration {
auto browserState = base::MakeUnique<ios_web_view::WebViewBrowserState>(true);
return [[self alloc] initWithBrowserState:std::move(browserState)];
static CWVWebViewConfiguration* incognitoConfiguration;
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 {
......@@ -62,10 +74,14 @@
return self;
}
#pragma mark - Public Methods
- (BOOL)isPersistent {
return !_browserState->IsOffTheRecord();
}
#pragma mark - Private Methods
- (ios_web_view::WebViewBrowserState*)browserState {
return _browserState.get();
}
......
......@@ -11,8 +11,11 @@
#include "base/strings/sys_string_conversions.h"
#include "components/translate/core/browser/translate_download_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/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_policy.h"
#include "ui/base/l10n/l10n_util.h"
......@@ -65,6 +68,17 @@ const NSInteger CWVTranslationErrorScriptLoadError =
@synthesize supportedLanguagesByCode = _supportedLanguagesByCode;
@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
- (void)setWebState:(web::WebState*)webState {
......@@ -155,6 +169,7 @@ const NSInteger CWVTranslationErrorScriptLoadError =
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageLanguage:(CWVTranslationLanguage*)pageLanguage {
DCHECK(!_webState->GetBrowserState()->IsOffTheRecord());
std::string languageCode = base::SysNSStringToUTF8(pageLanguage.languageCode);
switch (policy.type) {
case CWVTranslationPolicyAsk: {
......@@ -198,6 +213,7 @@ const NSInteger CWVTranslationErrorScriptLoadError =
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageHost:(NSString*)pageHost {
DCHECK(!_webState->GetBrowserState()->IsOffTheRecord());
DCHECK(pageHost.length);
switch (policy.type) {
case CWVTranslationPolicyAsk: {
......
......@@ -8,11 +8,13 @@
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/sequenced_task_runner.h"
#include "base/threading/thread_restrictions.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/pref_filter.h"
#include "components/prefs/pref_service_factory.h"
......@@ -56,8 +58,13 @@ WebViewBrowserState::WebViewBrowserState(bool off_the_record)
JsonPrefStore::GetTaskRunnerForFile(path_,
web::WebThread::GetBlockingPool());
scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore(
path_.Append(kPreferencesFilename), sequenced_task_runner, nullptr);
scoped_refptr<PersistentPrefStore> user_pref_store;
if (off_the_record) {
user_pref_store = new InMemoryPrefStore();
} else {
user_pref_store = new JsonPrefStore(path_.Append(kPreferencesFilename),
sequenced_task_runner, nullptr);
}
PrefServiceFactory factory;
factory.set_user_prefs(user_pref_store);
......
......@@ -52,6 +52,10 @@ CWV_EXPORT
@property(nonatomic, readonly)
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
// |targetLanguage|. These language parameters must be chosen from
// |supportedLanguages|. Set |userInitiated| to YES if translation
......@@ -70,6 +74,7 @@ CWV_EXPORT
// Sets or retrieves translation policies associated with a specified language.
// |pageLanguage| should be the language code of the language.
// Not supported in incognito mode.
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageLanguage:(CWVTranslationLanguage*)pageLanguage;
- (CWVTranslationPolicy*)translationPolicyForPageLanguage:
......@@ -77,6 +82,7 @@ CWV_EXPORT
// Sets or retrieves translation policies associated with a specified page.
// |pageHost| should be the hostname of the website. Must not be empty.
// Not supported in incognito mode.
- (void)setTranslationPolicy:(CWVTranslationPolicy*)policy
forPageHost:(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