Index: ios/web_view/internal/app/application_context.cc |
diff --git a/ios/web_view/internal/app/application_context.cc b/ios/web_view/internal/app/application_context.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ecefb8344e6749e9be7cdbb7e6395f1f6a3e420f |
--- /dev/null |
+++ b/ios/web_view/internal/app/application_context.cc |
@@ -0,0 +1,141 @@ |
+// Copyright 2017 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. |
+ |
+#include "ios/web_view/internal/app/application_context.h" |
+ |
+#include "base/command_line.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/memory/singleton.h" |
+#include "base/path_service.h" |
+#include "base/tracked_objects.h" |
+#include "components/flags_ui/pref_service_flags_storage.h" |
+#include "components/net_log/chrome_net_log.h" |
+#include "components/prefs/json_pref_store.h" |
+#include "components/prefs/pref_registry_simple.h" |
+#include "components/prefs/pref_service.h" |
+#include "components/prefs/pref_service_factory.h" |
+#include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
+#include "components/ssl_config/ssl_config_service_manager.h" |
+#include "components/translate/core/browser/translate_download_manager.h" |
+#include "ios/web/public/web_thread.h" |
+#include "ios/web_view/internal/app/web_view_io_thread.h" |
+#include "net/socket/client_socket_pool_manager.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
+ |
+namespace ios_web_view { |
+ |
+ApplicationContext* ApplicationContext::GetInstance() { |
+ return base::Singleton<ApplicationContext>::get(); |
+} |
+ |
+ApplicationContext::ApplicationContext() |
+ : local_state_task_runner_(JsonPrefStore::GetTaskRunnerForFile( |
+ GetLocalStatePath(), |
+ web::WebThread::GetBlockingPool())) { |
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
+ |
+ net_log_ = base::MakeUnique<net_log::ChromeNetLog>( |
+ base::FilePath(), net::NetLogCaptureMode::Default(), |
+ command_line->GetCommandLineString(), std::string()); |
+ |
+ SetApplicationLocale(l10n_util::GetLocaleOverride()); |
+} |
+ |
+ApplicationContext::~ApplicationContext() { |
+ tracked_objects::ThreadData::EnsureCleanupWasCalled(4); |
Eugene But (OOO till 7-30)
2017/05/31 16:25:33
Could you please create a constant for "4" with ex
michaeldo
2017/05/31 21:03:42
I deleted this instead. There is no sense in addin
|
+} |
+ |
+void ApplicationContext::PreCreateThreads() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ web_view_io_thread_ = |
+ base::MakeUnique<WebViewIOThread>(GetLocalState(), GetNetLog()); |
+} |
+ |
+void ApplicationContext::SaveState() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ // TODO(crbug.com/723854): Commit prefs when entering background. |
+ if (local_state_) { |
Eugene But (OOO till 7-30)
2017/05/31 16:25:33
Should we create local_state_ instead of having no
michaeldo
2017/05/31 21:03:42
I don't think so. No reason to create Local state
|
+ local_state_->CommitPendingWrite(); |
+ } |
+} |
+ |
+void ApplicationContext::PostDestroyThreads() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ // Resets associated state right after actual thread is stopped as |
+ // WebViewIOThread::Globals cleanup happens in CleanUp on the IO |
+ // thread, i.e. as the thread exits its message loop. |
+ // |
+ // This is important because in various places, the WebViewIOThread |
+ // object being null is considered synonymous with the IO thread |
+ // having stopped. |
+ web_view_io_thread_.reset(); |
+} |
+ |
+PrefService* ApplicationContext::GetLocalState() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ if (!local_state_) { |
+ // Register local state preferences. |
+ scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple); |
+ flags_ui::PrefServiceFlagsStorage::RegisterPrefs(pref_registry.get()); |
+ PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get()); |
+ ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry.get()); |
+ |
+ scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore( |
+ GetLocalStatePath(), local_state_task_runner_, nullptr); |
+ |
+ PrefServiceFactory factory; |
+ factory.set_user_prefs(user_pref_store); |
+ local_state_ = factory.Create(pref_registry.get()); |
+ |
+ int max_normal_socket_pool_count = |
+ net::ClientSocketPoolManager::max_sockets_per_group( |
+ net::HttpNetworkSession::NORMAL_SOCKET_POOL); |
+ int socket_count = |
+ std::max(std::min<int>(net::kDefaultMaxSocketsPerProxyServer, 99), |
Eugene But (OOO till 7-30)
2017/05/31 16:25:34
Do we know why we limit to 99 sockets?
michaeldo
2017/05/31 21:03:42
Unfortunately, I have no idea. I don't think it's
Hiroshi Ichikawa
2017/06/01 02:25:59
Can you at least add a comment which points to whe
michaeldo
2017/06/01 21:20:38
I started to add a comment, but then looked into t
|
+ max_normal_socket_pool_count); |
+ net::ClientSocketPoolManager::set_max_sockets_per_proxy_server( |
+ net::HttpNetworkSession::NORMAL_SOCKET_POOL, socket_count); |
+ } |
+ return local_state_.get(); |
+} |
+ |
+net::URLRequestContextGetter* ApplicationContext::GetSystemURLRequestContext() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ return web_view_io_thread_->system_url_request_context_getter(); |
+} |
+ |
+const std::string& ApplicationContext::GetApplicationLocale() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(!application_locale_.empty()); |
+ return application_locale_; |
+} |
+ |
+net_log::ChromeNetLog* ApplicationContext::GetNetLog() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ return net_log_.get(); |
+} |
+ |
+WebViewIOThread* ApplicationContext::GetWebViewIOThread() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(web_view_io_thread_.get()); |
+ return web_view_io_thread_.get(); |
+} |
+ |
+base::FilePath ApplicationContext::GetLocalStatePath() { |
+ base::FilePath local_state_path; |
+ PathService::Get(base::DIR_APP_DATA, &local_state_path); |
+ local_state_path = |
+ local_state_path.Append(FILE_PATH_LITERAL("ChromeWebView")); |
+ local_state_path = local_state_path.Append(FILE_PATH_LITERAL("Local State")); |
+ return local_state_path; |
+} |
+ |
+void ApplicationContext::SetApplicationLocale(const std::string& locale) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ application_locale_ = locale; |
+ translate::TranslateDownloadManager::GetInstance()->set_application_locale( |
+ application_locale_); |
+} |
+ |
+} // namespace ios_web_view |