Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: ios/web_view/internal/app/application_context.cc

Issue 2894483003: Initialize ios/web_view translate with a system-wide URLRequestContext. (Closed)
Patch Set: Respond to comments. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_SEQUENCE(sequence_checker_);
48 web_view_io_thread_ =
49 base::MakeUnique<WebViewIOThread>(GetLocalState(), GetNetLog());
50 }
51
52 void ApplicationContext::SaveState() {
53 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_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_SEQUENCE(sequence_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_SEQUENCE(sequence_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 = std::max<int>(net::kDefaultMaxSocketsPerProxyServer,
92 max_normal_socket_pool_count);
93 net::ClientSocketPoolManager::set_max_sockets_per_proxy_server(
94 net::HttpNetworkSession::NORMAL_SOCKET_POOL, socket_count);
95 }
96 return local_state_.get();
97 }
98
99 net::URLRequestContextGetter* ApplicationContext::GetSystemURLRequestContext() {
100 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
101 return web_view_io_thread_->system_url_request_context_getter();
102 }
103
104 const std::string& ApplicationContext::GetApplicationLocale() {
105 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
106 DCHECK(!application_locale_.empty());
107 return application_locale_;
108 }
109
110 net_log::ChromeNetLog* ApplicationContext::GetNetLog() {
111 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
112 return net_log_.get();
113 }
114
115 WebViewIOThread* ApplicationContext::GetWebViewIOThread() {
116 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
117 DCHECK(web_view_io_thread_.get());
118 return web_view_io_thread_.get();
119 }
120
121 base::FilePath ApplicationContext::GetLocalStatePath() {
122 base::FilePath local_state_path;
123 PathService::Get(base::DIR_APP_DATA, &local_state_path);
124 local_state_path =
125 local_state_path.Append(FILE_PATH_LITERAL("ChromeWebView"));
126 local_state_path = local_state_path.Append(FILE_PATH_LITERAL("Local State"));
127 return local_state_path;
128 }
129
130 void ApplicationContext::SetApplicationLocale(const std::string& locale) {
131 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
132 application_locale_ = locale;
133 translate::TranslateDownloadManager::GetInstance()->set_application_locale(
134 application_locale_);
135 }
136
137 } // namespace ios_web_view
OLDNEW
« no previous file with comments | « ios/web_view/internal/app/application_context.h ('k') | ios/web_view/internal/app/web_view_io_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698