OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/net_export_ui.h" | 5 #include "chrome/browser/ui/webui/net_export_ui.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
14 #include "base/scoped_observer.h" | 15 #include "base/scoped_observer.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
19 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
21 #include "chrome/browser/io_thread.h" | |
20 #include "chrome/browser/net/net_export_helper.h" | 22 #include "chrome/browser/net/net_export_helper.h" |
21 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/ui/chrome_select_file_policy.h" | 24 #include "chrome/browser/ui/chrome_select_file_policy.h" |
23 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
24 #include "components/grit/components_resources.h" | 26 #include "components/grit/components_resources.h" |
25 #include "components/net_log/chrome_net_log.h" | 27 #include "components/net_log/chrome_net_log.h" |
26 #include "components/net_log/net_export_ui_constants.h" | 28 #include "components/net_log/net_export_ui_constants.h" |
27 #include "components/net_log/net_log_file_writer.h" | 29 #include "components/net_log/net_log_file_writer.h" |
28 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/storage_partition.h" | |
29 #include "content/public/browser/url_data_source.h" | 32 #include "content/public/browser/url_data_source.h" |
30 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
31 #include "content/public/browser/web_ui.h" | 34 #include "content/public/browser/web_ui.h" |
32 #include "content/public/browser/web_ui_data_source.h" | 35 #include "content/public/browser/web_ui_data_source.h" |
33 #include "content/public/browser/web_ui_message_handler.h" | 36 #include "content/public/browser/web_ui_message_handler.h" |
37 #include "extensions/features/features.h" | |
34 #include "net/log/net_log_capture_mode.h" | 38 #include "net/log/net_log_capture_mode.h" |
35 #include "net/url_request/url_request_context_getter.h" | 39 #include "net/url_request/url_request_context_getter.h" |
36 #include "ui/shell_dialogs/select_file_dialog.h" | 40 #include "ui/shell_dialogs/select_file_dialog.h" |
37 | 41 |
38 #if defined(OS_ANDROID) | 42 #if defined(OS_ANDROID) |
39 #include "chrome/browser/android/intent_helper.h" | 43 #include "chrome/browser/android/intent_helper.h" |
40 #endif | 44 #endif |
41 | 45 |
42 using content::BrowserThread; | 46 using content::BrowserThread; |
43 using content::WebContents; | 47 using content::WebContents; |
44 using content::WebUIMessageHandler; | 48 using content::WebUIMessageHandler; |
45 | 49 |
46 namespace { | 50 namespace { |
47 | 51 |
52 class ProxyScriptFetcherContextGetter : public net::URLRequestContextGetter { | |
53 public: | |
54 explicit ProxyScriptFetcherContextGetter(IOThread* io_thread); | |
55 | |
56 // net::URLRequestContextGetter implementation. | |
57 net::URLRequestContext* GetURLRequestContext() override; | |
58 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() | |
59 const override; | |
60 | |
61 protected: | |
62 ~ProxyScriptFetcherContextGetter() override; | |
63 | |
64 private: | |
65 IOThread* const io_thread_; // Owned by BrowserProcess. | |
66 }; | |
67 | |
68 ProxyScriptFetcherContextGetter::ProxyScriptFetcherContextGetter( | |
eroman
2017/02/22 02:03:29
[optional] My style suggestion here would be to in
wangyix1
2017/02/23 02:14:57
Done.
| |
69 IOThread* io_thread) | |
70 : io_thread_(io_thread) {} | |
71 | |
72 ProxyScriptFetcherContextGetter::~ProxyScriptFetcherContextGetter() {} | |
73 | |
74 net::URLRequestContext* | |
75 ProxyScriptFetcherContextGetter::GetURLRequestContext() { | |
76 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
77 DCHECK(io_thread_->globals()->proxy_script_fetcher_context.get()); | |
78 return io_thread_->globals()->proxy_script_fetcher_context.get(); | |
79 } | |
80 | |
81 scoped_refptr<base::SingleThreadTaskRunner> | |
82 ProxyScriptFetcherContextGetter::GetNetworkTaskRunner() const { | |
83 return BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); | |
84 } | |
85 | |
48 // May only be accessed on the UI thread | 86 // May only be accessed on the UI thread |
49 base::LazyInstance<base::FilePath>::Leaky | 87 base::LazyInstance<base::FilePath>::Leaky |
50 last_save_dir = LAZY_INSTANCE_INITIALIZER; | 88 last_save_dir = LAZY_INSTANCE_INITIALIZER; |
51 | 89 |
52 content::WebUIDataSource* CreateNetExportHTMLSource() { | 90 content::WebUIDataSource* CreateNetExportHTMLSource() { |
53 content::WebUIDataSource* source = | 91 content::WebUIDataSource* source = |
54 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); | 92 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); |
55 | 93 |
56 source->SetJsonPath("strings.js"); | 94 source->SetJsonPath("strings.js"); |
57 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); | 95 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 // ui::SelectFileDialog::Listener implementation. | 129 // ui::SelectFileDialog::Listener implementation. |
92 void FileSelected(const base::FilePath& path, | 130 void FileSelected(const base::FilePath& path, |
93 int index, | 131 int index, |
94 void* params) override; | 132 void* params) override; |
95 void FileSelectionCanceled(void* params) override; | 133 void FileSelectionCanceled(void* params) override; |
96 | 134 |
97 // net_log::NetLogFileWriter::StateObserver implementation. | 135 // net_log::NetLogFileWriter::StateObserver implementation. |
98 void OnNewState(const base::DictionaryValue& state) override; | 136 void OnNewState(const base::DictionaryValue& state) override; |
99 | 137 |
100 private: | 138 private: |
139 using URLRequestContextGetterList = | |
140 std::vector<scoped_refptr<net::URLRequestContextGetter>>; | |
141 | |
101 // Send NetLog data via email. | 142 // Send NetLog data via email. |
102 static void SendEmail(const base::FilePath& file_to_send); | 143 static void SendEmail(const base::FilePath& file_to_send); |
103 | 144 |
104 // chrome://net-export can be used on both mobile and desktop platforms. | 145 // chrome://net-export can be used on both mobile and desktop platforms. |
105 // On mobile a user cannot pick where their NetLog file is saved to. | 146 // On mobile a user cannot pick where their NetLog file is saved to. |
106 // Instead, everything is saved on the user's temp directory. Thus the | 147 // Instead, everything is saved on the user's temp directory. Thus the |
107 // mobile user has the UI available to send their NetLog file as an | 148 // mobile user has the UI available to send their NetLog file as an |
108 // email while the desktop user, who gets to choose their NetLog file's | 149 // email while the desktop user, who gets to choose their NetLog file's |
109 // location, does not. Furthermore, since every time a user starts logging | 150 // location, does not. Furthermore, since every time a user starts logging |
110 // to a new NetLog file on mobile platforms it overwrites the previous | 151 // to a new NetLog file on mobile platforms it overwrites the previous |
111 // NetLog file, a warning message appears on the Start Logging button | 152 // NetLog file, a warning message appears on the Start Logging button |
112 // that informs the user of this. This does not exist on the desktop | 153 // that informs the user of this. This does not exist on the desktop |
113 // UI. | 154 // UI. |
114 static bool UsingMobileUI(); | 155 static bool UsingMobileUI(); |
115 | 156 |
116 // Calls NetExportView.onExportNetLogInfoChanged JavaScript function in the | 157 // Calls NetExportView.onExportNetLogInfoChanged JavaScript function in the |
117 // renderer, passing in |file_writer_state|. | 158 // renderer, passing in |file_writer_state|. |
118 void NotifyUIWithState(std::unique_ptr<base::DictionaryValue> state); | 159 void NotifyUIWithState(std::unique_ptr<base::DictionaryValue> state); |
119 | 160 |
120 // Opens the SelectFileDialog UI with the default path to save a | 161 // Opens the SelectFileDialog UI with the default path to save a |
121 // NetLog file. | 162 // NetLog file. |
122 void ShowSelectFileDialog(const base::FilePath& default_path); | 163 void ShowSelectFileDialog(const base::FilePath& default_path); |
123 | 164 |
165 // Returns a list of context getters used to retrieve ongoing events when | |
166 // logging starts so that net log entries can be added for those events. | |
167 URLRequestContextGetterList GetURLRequestContexts() const; | |
168 | |
124 // Cache of g_browser_process->net_log()->net_log_file_writer(). This | 169 // Cache of g_browser_process->net_log()->net_log_file_writer(). This |
125 // is owned by ChromeNetLog which is owned by BrowserProcessImpl. | 170 // is owned by ChromeNetLog which is owned by BrowserProcessImpl. |
126 net_log::NetLogFileWriter* file_writer_; | 171 net_log::NetLogFileWriter* file_writer_; |
127 | 172 |
128 ScopedObserver<net_log::NetLogFileWriter, | 173 ScopedObserver<net_log::NetLogFileWriter, |
129 net_log::NetLogFileWriter::StateObserver> | 174 net_log::NetLogFileWriter::StateObserver> |
130 state_observer_manager_; | 175 state_observer_manager_; |
131 | 176 |
132 // The capture mode the user chose in the UI when logging started is cached | 177 // The capture mode the user chose in the UI when logging started is cached |
133 // here and is read after a file path is chosen in the save dialog. | 178 // here and is read after a file path is chosen in the save dialog. |
134 // Its value is only valid while the save dialog is open on the desktop UI. | 179 // Its value is only valid while the save dialog is open on the desktop UI. |
135 net::NetLogCaptureMode capture_mode_; | 180 net::NetLogCaptureMode capture_mode_; |
136 | 181 |
137 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | 182 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
138 | 183 |
184 // A context getter for | |
185 // g_browser_process->io_thread()->globals()->proxy_script_fetcher_context. | |
186 scoped_refptr<net::URLRequestContextGetter> | |
187 proxy_script_fetcher_context_getter_; | |
eroman
2017/02/22 02:03:29
Please remove this variable (can be constructed di
wangyix1
2017/02/23 02:14:57
Done.
| |
188 | |
139 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; | 189 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; |
140 | 190 |
141 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); | 191 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); |
142 }; | 192 }; |
143 | 193 |
144 NetExportMessageHandler::NetExportMessageHandler() | 194 NetExportMessageHandler::NetExportMessageHandler() |
145 : file_writer_(g_browser_process->net_log()->net_log_file_writer()), | 195 : file_writer_(g_browser_process->net_log()->net_log_file_writer()), |
146 state_observer_manager_(this), | 196 state_observer_manager_(this), |
197 proxy_script_fetcher_context_getter_( | |
198 new ProxyScriptFetcherContextGetter(g_browser_process->io_thread())), | |
147 weak_ptr_factory_(this) { | 199 weak_ptr_factory_(this) { |
148 file_writer_->Initialize( | 200 file_writer_->Initialize( |
149 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE_USER_BLOCKING), | 201 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE_USER_BLOCKING), |
150 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); | 202 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); |
151 } | 203 } |
152 | 204 |
153 NetExportMessageHandler::~NetExportMessageHandler() { | 205 NetExportMessageHandler::~NetExportMessageHandler() { |
154 // There may be a pending file dialog, it needs to be told that the user | 206 // There may be a pending file dialog, it needs to be told that the user |
155 // has gone away so that it doesn't try to call back. | 207 // has gone away so that it doesn't try to call back. |
156 if (select_file_dialog_) | 208 if (select_file_dialog_) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { | 247 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { |
196 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 248 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
197 std::string capture_mode_string; | 249 std::string capture_mode_string; |
198 bool result = list->GetString(0, &capture_mode_string); | 250 bool result = list->GetString(0, &capture_mode_string); |
199 DCHECK(result); | 251 DCHECK(result); |
200 | 252 |
201 capture_mode_ = | 253 capture_mode_ = |
202 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string); | 254 net_log::NetLogFileWriter::CaptureModeFromString(capture_mode_string); |
203 | 255 |
204 if (UsingMobileUI()) { | 256 if (UsingMobileUI()) { |
205 file_writer_->StartNetLog(base::FilePath(), capture_mode_); | 257 file_writer_->StartNetLog(base::FilePath(), capture_mode_, |
258 GetURLRequestContexts()); | |
206 } else { | 259 } else { |
207 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? | 260 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? |
208 DownloadPrefs::FromBrowserContext( | 261 DownloadPrefs::FromBrowserContext( |
209 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : | 262 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : |
210 *last_save_dir.Pointer(); | 263 *last_save_dir.Pointer(); |
211 base::FilePath initial_path = | 264 base::FilePath initial_path = |
212 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); | 265 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); |
213 ShowSelectFileDialog(initial_path); | 266 ShowSelectFileDialog(initial_path); |
214 } | 267 } |
215 } | 268 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 } | 300 } |
248 | 301 |
249 void NetExportMessageHandler::FileSelected(const base::FilePath& path, | 302 void NetExportMessageHandler::FileSelected(const base::FilePath& path, |
250 int index, | 303 int index, |
251 void* params) { | 304 void* params) { |
252 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 305 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
253 DCHECK(select_file_dialog_); | 306 DCHECK(select_file_dialog_); |
254 select_file_dialog_ = nullptr; | 307 select_file_dialog_ = nullptr; |
255 *last_save_dir.Pointer() = path.DirName(); | 308 *last_save_dir.Pointer() = path.DirName(); |
256 | 309 |
257 file_writer_->StartNetLog(path, capture_mode_); | 310 file_writer_->StartNetLog(path, capture_mode_, GetURLRequestContexts()); |
258 } | 311 } |
259 | 312 |
260 void NetExportMessageHandler::FileSelectionCanceled(void* params) { | 313 void NetExportMessageHandler::FileSelectionCanceled(void* params) { |
261 DCHECK(select_file_dialog_); | 314 DCHECK(select_file_dialog_); |
262 select_file_dialog_ = nullptr; | 315 select_file_dialog_ = nullptr; |
263 } | 316 } |
264 | 317 |
265 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) { | 318 void NetExportMessageHandler::OnNewState(const base::DictionaryValue& state) { |
266 NotifyUIWithState(state.CreateDeepCopy()); | 319 NotifyUIWithState(state.CreateDeepCopy()); |
267 } | 320 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 select_file_dialog_ = ui::SelectFileDialog::Create( | 368 select_file_dialog_ = ui::SelectFileDialog::Create( |
316 this, new ChromeSelectFilePolicy(webcontents)); | 369 this, new ChromeSelectFilePolicy(webcontents)); |
317 ui::SelectFileDialog::FileTypeInfo file_type_info; | 370 ui::SelectFileDialog::FileTypeInfo file_type_info; |
318 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; | 371 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; |
319 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); | 372 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); |
320 select_file_dialog_->SelectFile( | 373 select_file_dialog_->SelectFile( |
321 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, | 374 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, |
322 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); | 375 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); |
323 } | 376 } |
324 | 377 |
378 NetExportMessageHandler::URLRequestContextGetterList | |
379 NetExportMessageHandler::GetURLRequestContexts() const { | |
eroman
2017/02/22 02:03:29
Pelase add: DCHECK_CURRENTLY_ON(BrowserThread::UI)
wangyix1
2017/02/23 02:14:57
Done.
| |
380 URLRequestContextGetterList context_getters; | |
381 | |
382 Profile* profile = Profile::FromWebUI(web_ui()); | |
383 | |
384 context_getters.push_back(profile->GetRequestContext()); | |
385 context_getters.push_back( | |
386 content::BrowserContext::GetDefaultStoragePartition(profile) | |
387 ->GetMediaURLRequestContext()); | |
388 #if BUILDFLAG(ENABLE_EXTENSIONS) | |
389 context_getters.push_back(profile->GetRequestContextForExtensions()); | |
390 #endif | |
391 context_getters.push_back( | |
392 g_browser_process->io_thread()->system_url_request_context_getter()); | |
393 context_getters.push_back(proxy_script_fetcher_context_getter_); | |
394 | |
395 return context_getters; | |
396 } | |
397 | |
325 } // namespace | 398 } // namespace |
326 | 399 |
327 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 400 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
328 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>()); | 401 web_ui->AddMessageHandler(base::MakeUnique<NetExportMessageHandler>()); |
329 | 402 |
330 // Set up the chrome://net-export/ source. | 403 // Set up the chrome://net-export/ source. |
331 Profile* profile = Profile::FromWebUI(web_ui); | 404 Profile* profile = Profile::FromWebUI(web_ui); |
332 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); | 405 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); |
333 } | 406 } |
OLD | NEW |