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