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 "base/tracked_objects.h" |
| 12 #include "components/flags_ui/pref_service_flags_storage.h" |
| 13 #include "components/net_log/chrome_net_log.h" |
| 14 #include "components/prefs/json_pref_store.h" |
| 15 #include "components/prefs/pref_registry_simple.h" |
| 16 #include "components/prefs/pref_service.h" |
| 17 #include "components/prefs/pref_service_factory.h" |
| 18 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
| 19 #include "components/ssl_config/ssl_config_service_manager.h" |
| 20 #include "components/translate/core/browser/translate_download_manager.h" |
| 21 #include "ios/web/public/web_thread.h" |
| 22 #include "ios/web_view/internal/app/web_view_io_thread.h" |
| 23 #include "net/socket/client_socket_pool_manager.h" |
| 24 #include "ui/base/l10n/l10n_util_mac.h" |
| 25 |
| 26 namespace ios_web_view { |
| 27 |
| 28 ApplicationContext* ApplicationContext::GetInstance() { |
| 29 return base::Singleton<ApplicationContext>::get(); |
| 30 } |
| 31 |
| 32 ApplicationContext::ApplicationContext() |
| 33 : local_state_task_runner_(JsonPrefStore::GetTaskRunnerForFile( |
| 34 GetLocalStatePath(), |
| 35 web::WebThread::GetBlockingPool())) { |
| 36 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 37 |
| 38 net_log_ = base::MakeUnique<net_log::ChromeNetLog>( |
| 39 base::FilePath(), net::NetLogCaptureMode::Default(), |
| 40 command_line->GetCommandLineString(), std::string()); |
| 41 |
| 42 SetApplicationLocale(l10n_util::GetLocaleOverride()); |
| 43 } |
| 44 |
| 45 ApplicationContext::~ApplicationContext() { |
| 46 tracked_objects::ThreadData::EnsureCleanupWasCalled(4); |
| 47 } |
| 48 |
| 49 void ApplicationContext::PreCreateThreads() { |
| 50 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 51 web_view_io_thread_ = |
| 52 base::MakeUnique<WebViewIOThread>(GetLocalState(), GetNetLog()); |
| 53 } |
| 54 |
| 55 void ApplicationContext::SaveState() { |
| 56 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 57 // TODO(crbug.com/723854): Commit prefs when entering background. |
| 58 if (local_state_) { |
| 59 local_state_->CommitPendingWrite(); |
| 60 } |
| 61 } |
| 62 |
| 63 void ApplicationContext::PostDestroyThreads() { |
| 64 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 65 // Resets associated state right after actual thread is stopped as |
| 66 // WebViewIOThread::Globals cleanup happens in CleanUp on the IO |
| 67 // thread, i.e. as the thread exits its message loop. |
| 68 // |
| 69 // This is important because in various places, the WebViewIOThread |
| 70 // object being null is considered synonymous with the IO thread |
| 71 // having stopped. |
| 72 web_view_io_thread_.reset(); |
| 73 } |
| 74 |
| 75 PrefService* ApplicationContext::GetLocalState() { |
| 76 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 77 if (!local_state_) { |
| 78 // Register local state preferences. |
| 79 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple); |
| 80 flags_ui::PrefServiceFlagsStorage::RegisterPrefs(pref_registry.get()); |
| 81 PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get()); |
| 82 ssl_config::SSLConfigServiceManager::RegisterPrefs(pref_registry.get()); |
| 83 |
| 84 scoped_refptr<PersistentPrefStore> user_pref_store = new JsonPrefStore( |
| 85 GetLocalStatePath(), local_state_task_runner_, nullptr); |
| 86 |
| 87 PrefServiceFactory factory; |
| 88 factory.set_user_prefs(user_pref_store); |
| 89 local_state_ = factory.Create(pref_registry.get()); |
| 90 |
| 91 net::ClientSocketPoolManager::set_max_sockets_per_proxy_server( |
| 92 net::HttpNetworkSession::NORMAL_SOCKET_POOL, |
| 93 std::max(std::min<int>(net::kDefaultMaxSocketsPerProxyServer, 99), |
| 94 net::ClientSocketPoolManager::max_sockets_per_group( |
| 95 net::HttpNetworkSession::NORMAL_SOCKET_POOL))); |
| 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 |