OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/web_view/internal/app/application_context.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/singleton.h" |
| 10 #include "base/path_service.h" |
| 11 #include "components/flags_ui/pref_service_flags_storage.h" |
| 12 #include "components/net_log/chrome_net_log.h" |
| 13 #include "components/prefs/json_pref_store.h" |
| 14 #include "components/prefs/pref_registry_simple.h" |
| 15 #include "components/prefs/pref_service.h" |
| 16 #include "components/prefs/pref_service_factory.h" |
| 17 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
| 18 #include "components/ssl_config/ssl_config_service_manager.h" |
| 19 #include "components/translate/core/browser/translate_download_manager.h" |
| 20 #include "ios/web/public/web_thread.h" |
| 21 #include "ios/web_view/internal/app/web_view_io_thread.h" |
| 22 #include "net/socket/client_socket_pool_manager.h" |
| 23 #include "ui/base/l10n/l10n_util_mac.h" |
| 24 |
| 25 namespace ios_web_view { |
| 26 |
| 27 ApplicationContext* ApplicationContext::GetInstance() { |
| 28 return base::Singleton<ApplicationContext>::get(); |
| 29 } |
| 30 |
| 31 ApplicationContext::ApplicationContext() |
| 32 : local_state_task_runner_(JsonPrefStore::GetTaskRunnerForFile( |
| 33 GetLocalStatePath(), |
| 34 web::WebThread::GetBlockingPool())) { |
| 35 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 36 |
| 37 net_log_ = base::MakeUnique<net_log::ChromeNetLog>( |
| 38 base::FilePath(), net::NetLogCaptureMode::Default(), |
| 39 command_line->GetCommandLineString(), std::string()); |
| 40 |
| 41 SetApplicationLocale(l10n_util::GetLocaleOverride()); |
| 42 } |
| 43 |
| 44 ApplicationContext::~ApplicationContext() = default; |
| 45 |
| 46 void ApplicationContext::PreCreateThreads() { |
| 47 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 48 web_view_io_thread_ = |
| 49 base::MakeUnique<WebViewIOThread>(GetLocalState(), GetNetLog()); |
| 50 } |
| 51 |
| 52 void ApplicationContext::SaveState() { |
| 53 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 54 // TODO(crbug.com/723854): Commit prefs when entering background. |
| 55 if (local_state_) { |
| 56 local_state_->CommitPendingWrite(); |
| 57 } |
| 58 } |
| 59 |
| 60 void ApplicationContext::PostDestroyThreads() { |
| 61 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 62 // Resets associated state right after actual thread is stopped as |
| 63 // WebViewIOThread::Globals cleanup happens in CleanUp on the IO |
| 64 // thread, i.e. as the thread exits its message loop. |
| 65 // |
| 66 // This is important because in various places, the WebViewIOThread |
| 67 // object being null is considered synonymous with the IO thread |
| 68 // having stopped. |
| 69 web_view_io_thread_.reset(); |
| 70 } |
| 71 |
| 72 PrefService* ApplicationContext::GetLocalState() { |
| 73 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 74 if (!local_state_) { |
| 75 // Register local state preferences. |
| 76 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple); |
| 77 flags_ui::PrefServiceFlagsStorage::RegisterPrefs(pref_registry.get()); |
| 78 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get()); |
| 79 ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry.get()); |
| 80 |
| 81 scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore( |
| 82 GetLocalStatePath(), local_state_task_runner_, nullptr); |
| 83 |
| 84 PrefServiceFactory factory; |
| 85 factory.set_user_prefs(user_pref_store); |
| 86 local_state_ = factory.Create(pref_registry.get()); |
| 87 |
| 88 int max_normal_socket_pool_count = |
| 89 net::ClientSocketPoolManager::max_sockets_per_group( |
| 90 net::HttpNetworkSession::NORMAL_SOCKET_POOL); |
| 91 int socket_count = |
| 92 std::max(std::min<int>(net::kDefaultMaxSocketsPerProxyServer, 99), |
| 93 max_normal_socket_pool_count); |
| 94 net::ClientSocketPoolManager::set_max_sockets_per_proxy_server( |
| 95 net::HttpNetworkSession::NORMAL_SOCKET_POOL, socket_count); |
| 96 } |
| 97 return local_state_.get(); |
| 98 } |
| 99 |
| 100 net::URLRequestContextGetter* ApplicationContext::GetSystemURLRequestContext() { |
| 101 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 102 return web_view_io_thread_->system_url_request_context_getter(); |
| 103 } |
| 104 |
| 105 const std::string& ApplicationContext::GetApplicationLocale() { |
| 106 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 107 DCHECK(!application_locale_.empty()); |
| 108 return application_locale_; |
| 109 } |
| 110 |
| 111 net_log::ChromeNetLog* ApplicationContext::GetNetLog() { |
| 112 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 113 return net_log_.get(); |
| 114 } |
| 115 |
| 116 WebViewIOThread* ApplicationContext::GetWebViewIOThread() { |
| 117 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 118 DCHECK(web_view_io_thread_.get()); |
| 119 return web_view_io_thread_.get(); |
| 120 } |
| 121 |
| 122 base::FilePath ApplicationContext::GetLocalStatePath() { |
| 123 base::FilePath local_state_path; |
| 124 PathService::Get(base::DIR_APP_DATA, &local_state_path); |
| 125 local_state_path = |
| 126 local_state_path.Append(FILE_PATH_LITERAL("ChromeWebView")); |
| 127 local_state_path = local_state_path.Append(FILE_PATH_LITERAL("Local State")); |
| 128 return local_state_path; |
| 129 } |
| 130 |
| 131 void ApplicationContext::SetApplicationLocale(const std::string& locale) { |
| 132 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 133 application_locale_ = locale; |
| 134 translate::TranslateDownloadManager::GetInstance()->set_application_locale( |
| 135 application_locale_); |
| 136 } |
| 137 |
| 138 } // namespace ios_web_view |
OLD | NEW |