Chromium Code Reviews| 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 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "components/grit/components_resources.h" | 24 #include "components/grit/components_resources.h" |
| 25 #include "components/net_log/chrome_net_log.h" | 25 #include "components/net_log/chrome_net_log.h" |
| 26 #include "components/net_log/net_export_ui_constants.h" | 26 #include "components/net_log/net_export_ui_constants.h" |
| 27 #include "components/net_log/net_log_file_writer.h" | 27 #include "components/net_log/net_log_file_writer.h" |
| 28 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 29 #include "content/public/browser/url_data_source.h" | 29 #include "content/public/browser/url_data_source.h" |
| 30 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/browser/web_ui.h" | 31 #include "content/public/browser/web_ui.h" |
| 32 #include "content/public/browser/web_ui_data_source.h" | 32 #include "content/public/browser/web_ui_data_source.h" |
| 33 #include "content/public/browser/web_ui_message_handler.h" | 33 #include "content/public/browser/web_ui_message_handler.h" |
| 34 #include "net/log/net_log_util.h" | |
| 35 #include "net/url_request/url_request_context_getter.h" | |
| 34 #include "ui/shell_dialogs/select_file_dialog.h" | 36 #include "ui/shell_dialogs/select_file_dialog.h" |
| 35 | 37 |
| 36 #if BUILDFLAG(ANDROID_JAVA_UI) | 38 #if BUILDFLAG(ANDROID_JAVA_UI) |
| 37 #include "chrome/browser/android/intent_helper.h" | 39 #include "chrome/browser/android/intent_helper.h" |
| 38 #endif | 40 #endif |
| 39 | 41 |
| 40 using content::BrowserThread; | 42 using content::BrowserThread; |
| 41 using content::WebContents; | 43 using content::WebContents; |
| 42 using content::WebUIMessageHandler; | 44 using content::WebUIMessageHandler; |
| 43 | 45 |
| 44 namespace { | 46 namespace { |
| 45 | 47 |
| 46 // May only be accessed on the UI thread | 48 // May only be accessed on the UI thread |
| 47 base::LazyInstance<base::FilePath>::Leaky | 49 base::LazyInstance<base::FilePath>::Leaky |
| 48 last_save_dir = LAZY_INSTANCE_INITIALIZER; | 50 last_save_dir = LAZY_INSTANCE_INITIALIZER; |
| 49 | 51 |
| 50 content::WebUIDataSource* CreateNetExportHTMLSource() { | 52 content::WebUIDataSource* CreateNetExportHTMLSource() { |
| 51 content::WebUIDataSource* source = | 53 content::WebUIDataSource* source = |
| 52 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); | 54 content::WebUIDataSource::Create(chrome::kChromeUINetExportHost); |
| 53 | 55 |
| 54 source->SetJsonPath("strings.js"); | 56 source->SetJsonPath("strings.js"); |
| 55 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); | 57 source->AddResourcePath(net_log::kNetExportUIJS, IDR_NET_LOG_NET_EXPORT_JS); |
| 56 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML); | 58 source->SetDefaultResource(IDR_NET_LOG_NET_EXPORT_HTML); |
| 57 return source; | 59 return source; |
| 58 } | 60 } |
| 59 | 61 |
| 60 // This class receives javascript messages from the renderer. | 62 // This class receives javascript messages from the renderer. |
| 61 // Note that the WebUI infrastructure runs on the UI thread, therefore all of | 63 // Note that the WebUI infrastructure runs on the UI thread, therefore all of |
| 62 // this class's public methods are expected to run on the UI thread. All static | 64 // this class's public methods are expected to run on the UI thread. All static |
| 63 // functions except SendEmail run on FILE_USER_BLOCKING thread. | 65 // functions except SendEmail run on FILE_USER_BLOCKING thread. |
|
eroman
2017/01/06 21:46:22
This comment is no longer true.
wangyix1
2017/01/10 22:20:14
Done.
| |
| 64 class NetExportMessageHandler | 66 class NetExportMessageHandler |
| 65 : public WebUIMessageHandler, | 67 : public WebUIMessageHandler, |
| 66 public base::SupportsWeakPtr<NetExportMessageHandler>, | 68 public base::SupportsWeakPtr<NetExportMessageHandler>, |
| 67 public ui::SelectFileDialog::Listener { | 69 public ui::SelectFileDialog::Listener { |
| 68 public: | 70 public: |
| 69 NetExportMessageHandler(); | 71 NetExportMessageHandler(); |
| 70 ~NetExportMessageHandler() override; | 72 ~NetExportMessageHandler() override; |
| 71 | 73 |
| 72 // WebUIMessageHandler implementation. | 74 // WebUIMessageHandler implementation. |
| 73 void RegisterMessages() override; | 75 void RegisterMessages() override; |
| 74 | 76 |
| 75 // Messages. | 77 // Messages. |
| 76 void OnGetExportNetLogInfo(const base::ListValue* list); | 78 void OnGetExportNetLogInfo(const base::ListValue* list); |
| 77 void OnStartNetLog(const base::ListValue* list); | 79 void OnStartNetLog(const base::ListValue* list); |
| 78 void OnStopNetLog(const base::ListValue* list); | 80 void OnStopNetLog(const base::ListValue* list); |
| 79 void OnSendNetLog(const base::ListValue* list); | 81 void OnSendNetLog(const base::ListValue* list); |
| 80 | 82 |
| 81 // ui::SelectFileDialog::Listener: | 83 // ui::SelectFileDialog::Listener: |
| 82 void FileSelected(const base::FilePath& path, | 84 void FileSelected(const base::FilePath& path, |
| 83 int index, | 85 int index, |
| 84 void* params) override; | 86 void* params) override; |
| 85 void FileSelectionCanceled(void* params) override; | 87 void FileSelectionCanceled(void* params) override; |
| 86 | 88 |
| 87 private: | 89 private: |
| 88 // Calls NetLogFileWriter's ProcessCommand with DO_START and DO_STOP commands. | 90 void StartNetLogAndNotifyUI(const base::FilePath& log_path, |
|
eroman
2017/01/06 21:46:22
I suggest naming this "StartNetLogThenNotifyUI"
(
wangyix1
2017/01/10 22:20:14
Done.
| |
| 89 static void ProcessNetLogCommand( | 91 net::NetLogCaptureMode capture_mode); |
| 90 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, | |
| 91 net_log::NetLogFileWriter* net_log_file_writer, | |
| 92 net_log::NetLogFileWriter::Command command); | |
| 93 | 92 |
| 94 // Returns the path to the file which has NetLog data. | 93 void StopNetLogAndNotifyUI(); |
|
eroman
2017/01/06 21:46:22
Same comment here.
wangyix1
2017/01/10 22:20:15
Done.
| |
| 95 static base::FilePath GetNetLogFileName( | |
| 96 net_log::NetLogFileWriter* net_log_file_writer); | |
| 97 | |
| 98 // Send state/file information from NetLogFileWriter. | |
| 99 static void SendExportNetLogInfo( | |
| 100 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, | |
| 101 net_log::NetLogFileWriter* net_log_file_writer); | |
| 102 | 94 |
| 103 // Send NetLog data via email. This runs on UI thread. | 95 // Send NetLog data via email. This runs on UI thread. |
|
eroman
2017/01/06 21:46:22
The mention of "UI" thread is probably redundant,
wangyix1
2017/01/10 22:20:14
Done.
| |
| 104 static void SendEmail(const base::FilePath& file_to_send); | 96 static void SendEmail(const base::FilePath& file_to_send); |
| 105 | 97 |
| 106 // chrome://net-export can be used on both mobile and desktop platforms. | 98 // chrome://net-export can be used on both mobile and desktop platforms. |
| 107 // On mobile a user cannot pick where their NetLog file is saved to. | 99 // On mobile a user cannot pick where their NetLog file is saved to. |
| 108 // Instead, everything is saved on the user's temp directory. Thus the | 100 // Instead, everything is saved on the user's temp directory. Thus the |
| 109 // mobile user has the UI available to send their NetLog file as an | 101 // mobile user has the UI available to send their NetLog file as an |
| 110 // email while the desktop user, who gets to chose their NetLog file's | 102 // email while the desktop user, who gets to chose their NetLog file's |
| 111 // location, does not. Furthermore, since every time a user starts logging | 103 // location, does not. Furthermore, since every time a user starts logging |
| 112 // to a new NetLog file on mobile platforms it overwrites the previous | 104 // to a new NetLog file on mobile platforms it overwrites the previous |
| 113 // NetLog file, a warning message appears on the Start Logging button | 105 // NetLog file, a warning message appears on the Start Logging button |
| 114 // that informs the user of this. This does not exist on the desktop | 106 // that informs the user of this. This does not exist on the desktop |
| 115 // UI. | 107 // UI. |
| 116 static bool UsingMobileUI(); | 108 static bool UsingMobileUI(); |
| 117 | 109 |
| 118 // Sets the correct start command and sends this to ProcessNetLogCommand. | |
| 119 void StartNetLog(); | |
| 120 | |
| 121 // Call NetExportView.onExportNetLogInfoChanged JavsScript function in the | 110 // Call NetExportView.onExportNetLogInfoChanged JavsScript function in the |
|
eroman
2017/01/06 21:46:22
While you are editing this, could you change "Call
wangyix1
2017/01/10 22:20:14
Done.
| |
| 122 // renderer, passing in |arg|. Takes ownership of |arg|. | 111 // renderer, passing in |file_writer_state|. |
| 123 void OnExportNetLogInfoChanged(base::Value* arg); | 112 void NotifyUIWithNetLogFileWriterState( |
| 113 std::unique_ptr<base::DictionaryValue> file_writer_state); | |
| 124 | 114 |
| 125 // Opens the SelectFileDialog UI with the default path to save a | 115 // Opens the SelectFileDialog UI with the default path to save a |
| 126 // NetLog file. | 116 // NetLog file. |
| 127 void ShowSelectFileDialog(const base::FilePath& default_path); | 117 void ShowSelectFileDialog(const base::FilePath& default_path); |
| 128 | 118 |
| 129 // Cache of g_browser_process->net_log()->net_log_file_writer(). This | 119 // Cache of g_browser_process->net_log()->net_log_file_writer(). This |
| 130 // is owned by ChromeNetLog which is owned by BrowserProcessImpl. There are | 120 // is owned by ChromeNetLog which is owned by BrowserProcessImpl. There are |
| 131 // four instances in this class where a pointer to net_log_file_writer_ is | 121 // four instances in this class where a pointer to net_log_file_writer_ is |
| 132 // posted to the FILE_USER_BLOCKING thread. Base::Unretained is used here | 122 // posted to the FILE_USER_BLOCKING thread. Base::Unretained is used here |
|
eroman
2017/01/06 21:46:22
This comment is no longer accurate.
wangyix1
2017/01/10 22:20:14
Done.
| |
| 133 // because BrowserProcessImpl is destroyed on the UI thread after joining the | 123 // because BrowserProcessImpl is destroyed on the UI thread after joining the |
| 134 // FILE_USER_BLOCKING thread making it impossible for there to be an invalid | 124 // FILE_USER_BLOCKING thread making it impossible for there to be an invalid |
| 135 // pointer to this object when going back to the UI thread. Furthermore this | 125 // pointer to this object when going back to the UI thread. Furthermore this |
| 136 // pointer is never dereferenced prematurely on the UI thread. Thus the | 126 // pointer is never dereferenced prematurely on the UI thread. Thus the |
| 137 // lifetime of this object is assured and can be safely used with | 127 // lifetime of this object is assured and can be safely used with |
| 138 // base::Unretained. | 128 // base::Unretained. |
| 139 net_log::NetLogFileWriter* net_log_file_writer_; | 129 net_log::NetLogFileWriter* net_log_file_writer_; |
| 140 | 130 |
| 141 std::string log_mode_; | 131 // The capture mode the user chose in the UI when logging started is cached |
| 132 // here. | |
|
eroman
2017/01/06 21:46:22
Please explain the purpose of this variable, and i
wangyix1
2017/01/10 22:20:14
Done.
| |
| 133 net::NetLogCaptureMode capture_mode_; | |
| 142 | 134 |
| 143 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; | 135 scoped_refptr<ui::SelectFileDialog> select_file_dialog_; |
| 144 | 136 |
| 145 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; | 137 base::WeakPtrFactory<NetExportMessageHandler> weak_ptr_factory_; |
| 146 | 138 |
| 147 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); | 139 DISALLOW_COPY_AND_ASSIGN(NetExportMessageHandler); |
| 148 }; | 140 }; |
| 149 | 141 |
| 150 NetExportMessageHandler::NetExportMessageHandler() | 142 NetExportMessageHandler::NetExportMessageHandler() |
| 151 : net_log_file_writer_(g_browser_process->net_log()->net_log_file_writer()), | 143 : net_log_file_writer_(g_browser_process->net_log()->net_log_file_writer()), |
| 152 weak_ptr_factory_(this) {} | 144 weak_ptr_factory_(this) { |
| 145 if (!net_log_file_writer_->GetFileTaskRunner()) { | |
|
eroman
2017/01/06 21:46:22
Since these will always be set in unison, how abou
wangyix1
2017/01/10 22:20:14
Done.
| |
| 146 net_log_file_writer_->SetFileTaskRunner( | |
| 147 BrowserThread::GetTaskRunnerForThread( | |
| 148 BrowserThread::FILE_USER_BLOCKING)); | |
| 149 } | |
| 150 if (!net_log_file_writer_->GetNetTaskRunner()) { | |
| 151 net_log_file_writer_->SetNetTaskRunner( | |
| 152 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)); | |
| 153 } | |
| 154 } | |
| 153 | 155 |
| 154 NetExportMessageHandler::~NetExportMessageHandler() { | 156 NetExportMessageHandler::~NetExportMessageHandler() { |
| 155 // There may be a pending file dialog, it needs to be told that the user | 157 // There may be a pending file dialog, it needs to be told that the user |
| 156 // has gone away so that it doesn't try to call back. | 158 // has gone away so that it doesn't try to call back. |
| 157 if (select_file_dialog_.get()) | 159 if (select_file_dialog_.get()) |
| 158 select_file_dialog_->ListenerDestroyed(); | 160 select_file_dialog_->ListenerDestroyed(); |
| 159 | 161 |
| 160 // Cancel any in-progress requests to collect net_log into a file. | 162 net_log_file_writer_->StopNetLog( |
| 161 BrowserThread::PostTask(BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | 163 nullptr, |
| 162 base::Bind(&net_log::NetLogFileWriter::ProcessCommand, | 164 Profile::FromWebUI(web_ui())->GetRequestContext(), |
| 163 base::Unretained(net_log_file_writer_), | 165 base::Bind([](std::unique_ptr<base::DictionaryValue>) {})); |
| 164 net_log::NetLogFileWriter::DO_STOP)); | |
| 165 } | 166 } |
| 166 | 167 |
| 167 void NetExportMessageHandler::RegisterMessages() { | 168 void NetExportMessageHandler::RegisterMessages() { |
| 168 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 169 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 169 | 170 |
| 170 web_ui()->RegisterMessageCallback( | 171 web_ui()->RegisterMessageCallback( |
| 171 net_log::kGetExportNetLogInfoHandler, | 172 net_log::kGetExportNetLogInfoHandler, |
| 172 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo, | 173 base::Bind(&NetExportMessageHandler::OnGetExportNetLogInfo, |
| 173 base::Unretained(this))); | 174 base::Unretained(this))); |
| 174 web_ui()->RegisterMessageCallback( | 175 web_ui()->RegisterMessageCallback( |
| 175 net_log::kStartNetLogHandler, | 176 net_log::kStartNetLogHandler, |
| 176 base::Bind(&NetExportMessageHandler::OnStartNetLog, | 177 base::Bind(&NetExportMessageHandler::OnStartNetLog, |
| 177 base::Unretained(this))); | 178 base::Unretained(this))); |
| 178 web_ui()->RegisterMessageCallback( | 179 web_ui()->RegisterMessageCallback( |
| 179 net_log::kStopNetLogHandler, | 180 net_log::kStopNetLogHandler, |
| 180 base::Bind(&NetExportMessageHandler::OnStopNetLog, | 181 base::Bind(&NetExportMessageHandler::OnStopNetLog, |
| 181 base::Unretained(this))); | 182 base::Unretained(this))); |
| 182 web_ui()->RegisterMessageCallback( | 183 web_ui()->RegisterMessageCallback( |
| 183 net_log::kSendNetLogHandler, | 184 net_log::kSendNetLogHandler, |
| 184 base::Bind(&NetExportMessageHandler::OnSendNetLog, | 185 base::Bind(&NetExportMessageHandler::OnSendNetLog, |
| 185 base::Unretained(this))); | 186 base::Unretained(this))); |
| 186 } | 187 } |
| 187 | 188 |
| 188 void NetExportMessageHandler::OnGetExportNetLogInfo( | 189 void NetExportMessageHandler::OnGetExportNetLogInfo( |
| 189 const base::ListValue* list) { | 190 const base::ListValue* list) { |
| 190 BrowserThread::PostTask( | 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 191 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | 192 net_log_file_writer_->GetState( |
| 192 base::Bind(&NetExportMessageHandler::SendExportNetLogInfo, | 193 base::Bind(&NetExportMessageHandler::NotifyUIWithNetLogFileWriterState, |
| 193 weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_)); | 194 weak_ptr_factory_.GetWeakPtr())); |
| 194 } | 195 } |
| 195 | 196 |
| 196 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { | 197 void NetExportMessageHandler::OnStartNetLog(const base::ListValue* list) { |
| 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 198 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 198 bool result = list->GetString(0, &log_mode_); | 199 std::string capture_mode_string; |
| 200 bool result = list->GetString(0, &capture_mode_string); | |
| 199 DCHECK(result); | 201 DCHECK(result); |
| 200 | 202 |
| 203 if (capture_mode_string == "LOG_BYTES") { | |
|
eroman
2017/01/06 21:46:22
Can we internalize the conversion from "capture mo
wangyix1
2017/01/10 22:20:14
Done.
| |
| 204 capture_mode_ = net::NetLogCaptureMode::IncludeSocketBytes(); | |
| 205 } else if (capture_mode_string == "NORMAL") { | |
| 206 capture_mode_ = net::NetLogCaptureMode::IncludeCookiesAndCredentials(); | |
| 207 } else { | |
| 208 DCHECK_EQ("STRIP_PRIVATE_DATA", capture_mode_string); | |
| 209 capture_mode_ = net::NetLogCaptureMode::Default(); | |
| 210 } | |
| 211 | |
| 201 if (UsingMobileUI()) { | 212 if (UsingMobileUI()) { |
| 202 StartNetLog(); | 213 StartNetLogAndNotifyUI(base::FilePath(), capture_mode_); |
| 203 } else { | 214 } else { |
| 204 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? | 215 base::FilePath initial_dir = last_save_dir.Pointer()->empty() ? |
| 205 DownloadPrefs::FromBrowserContext( | 216 DownloadPrefs::FromBrowserContext( |
| 206 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : | 217 web_ui()->GetWebContents()->GetBrowserContext())->DownloadPath() : |
| 207 *last_save_dir.Pointer(); | 218 *last_save_dir.Pointer(); |
| 208 base::FilePath initial_path = | 219 base::FilePath initial_path = |
| 209 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); | 220 initial_dir.Append(FILE_PATH_LITERAL("chrome-net-export-log.json")); |
| 210 ShowSelectFileDialog(initial_path); | 221 ShowSelectFileDialog(initial_path); |
| 211 } | 222 } |
| 212 } | 223 } |
| 213 | 224 |
| 214 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) { | 225 void NetExportMessageHandler::OnStopNetLog(const base::ListValue* list) { |
| 215 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_, | 226 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 216 net_log::NetLogFileWriter::DO_STOP); | 227 StopNetLogAndNotifyUI(); |
| 217 } | 228 } |
| 218 | 229 |
| 219 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) { | 230 void NetExportMessageHandler::OnSendNetLog(const base::ListValue* list) { |
| 220 content::BrowserThread::PostTaskAndReplyWithResult( | 231 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 221 content::BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | 232 net_log_file_writer_->GetFilePath( |
| 222 base::Bind(&NetExportMessageHandler::GetNetLogFileName, | |
| 223 base::Unretained(net_log_file_writer_)), | |
| 224 base::Bind(&NetExportMessageHandler::SendEmail)); | 233 base::Bind(&NetExportMessageHandler::SendEmail)); |
| 225 } | 234 } |
| 226 | 235 |
| 227 void NetExportMessageHandler::StartNetLog() { | 236 void NetExportMessageHandler::StartNetLogAndNotifyUI( |
| 228 net_log::NetLogFileWriter::Command command; | 237 const base::FilePath& log_path, |
| 229 if (log_mode_ == "LOG_BYTES") { | 238 net::NetLogCaptureMode capture_mode) { |
| 230 command = net_log::NetLogFileWriter::DO_START_LOG_BYTES; | 239 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 231 } else if (log_mode_ == "NORMAL") { | |
| 232 command = net_log::NetLogFileWriter::DO_START; | |
| 233 } else { | |
| 234 DCHECK_EQ("STRIP_PRIVATE_DATA", log_mode_); | |
| 235 command = net_log::NetLogFileWriter::DO_START_STRIP_PRIVATE_DATA; | |
| 236 } | |
| 237 | 240 |
| 238 ProcessNetLogCommand(weak_ptr_factory_.GetWeakPtr(), net_log_file_writer_, | 241 if (!log_path.empty()) |
| 239 command); | 242 net_log_file_writer_->SetUpNetExportLogPath(log_path); |
|
eroman
2017/01/06 21:46:22
Rather than a separate method SetUpNetExportLogPat
wangyix1
2017/01/10 22:20:14
Done.
| |
| 243 | |
| 244 net_log_file_writer_->StartNetLog(capture_mode, | |
| 245 base::Bind(&NetExportMessageHandler::NotifyUIWithNetLogFileWriterState, | |
| 246 weak_ptr_factory_.GetWeakPtr())); | |
| 247 } | |
| 248 | |
| 249 void NetExportMessageHandler::StopNetLogAndNotifyUI() { | |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 251 | |
| 252 std::unique_ptr<base::DictionaryValue> polled_data; | |
| 253 | |
| 254 // fill polled_data with browser-specific polled data. | |
|
eroman
2017/01/06 21:46:22
Please add a "TODO(crbug.com/438656)" here
wangyix1
2017/01/10 22:20:14
Done.
| |
| 255 | |
| 256 net_log_file_writer_->StopNetLog( | |
| 257 std::move(polled_data), | |
| 258 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 259 base::Bind(&NetExportMessageHandler::NotifyUIWithNetLogFileWriterState, | |
| 260 weak_ptr_factory_.GetWeakPtr())); | |
| 240 } | 261 } |
| 241 | 262 |
| 242 // static | 263 // static |
| 243 void NetExportMessageHandler::ProcessNetLogCommand( | |
| 244 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, | |
| 245 net_log::NetLogFileWriter* net_log_file_writer, | |
| 246 net_log::NetLogFileWriter::Command command) { | |
| 247 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE_USER_BLOCKING)) { | |
| 248 BrowserThread::PostTask( | |
| 249 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | |
| 250 base::Bind(&NetExportMessageHandler::ProcessNetLogCommand, | |
| 251 net_export_message_handler, net_log_file_writer, command)); | |
| 252 return; | |
| 253 } | |
| 254 | |
| 255 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | |
| 256 net_log_file_writer->ProcessCommand(command); | |
| 257 SendExportNetLogInfo(net_export_message_handler, net_log_file_writer); | |
| 258 } | |
| 259 | |
| 260 // static | |
| 261 base::FilePath NetExportMessageHandler::GetNetLogFileName( | |
| 262 net_log::NetLogFileWriter* net_log_file_writer) { | |
| 263 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | |
| 264 base::FilePath net_export_file_path; | |
| 265 net_log_file_writer->GetFilePath(&net_export_file_path); | |
| 266 return net_export_file_path; | |
| 267 } | |
| 268 | |
| 269 // static | |
| 270 void NetExportMessageHandler::SendExportNetLogInfo( | |
| 271 base::WeakPtr<NetExportMessageHandler> net_export_message_handler, | |
| 272 net_log::NetLogFileWriter* net_log_file_writer) { | |
| 273 DCHECK_CURRENTLY_ON(BrowserThread::FILE_USER_BLOCKING); | |
| 274 base::DictionaryValue* dict = net_log_file_writer->GetState(); | |
| 275 dict->SetBoolean("useMobileUI", UsingMobileUI()); | |
| 276 base::Value* value = dict; | |
| 277 if (!BrowserThread::PostTask( | |
| 278 BrowserThread::UI, FROM_HERE, | |
| 279 base::Bind(&NetExportMessageHandler::OnExportNetLogInfoChanged, | |
| 280 net_export_message_handler, | |
| 281 value))) { | |
| 282 // Failed posting the task, avoid leaking. | |
| 283 delete value; | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 // static | |
| 288 void NetExportMessageHandler::SendEmail(const base::FilePath& file_to_send) { | 264 void NetExportMessageHandler::SendEmail(const base::FilePath& file_to_send) { |
| 289 if (file_to_send.empty()) | 265 if (file_to_send.empty()) |
| 290 return; | 266 return; |
| 291 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 267 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 292 | 268 |
| 293 #if BUILDFLAG(ANDROID_JAVA_UI) | 269 #if BUILDFLAG(ANDROID_JAVA_UI) |
| 294 std::string email; | 270 std::string email; |
| 295 std::string subject = "net_internals_log"; | 271 std::string subject = "net_internals_log"; |
| 296 std::string title = "Issue number: "; | 272 std::string title = "Issue number: "; |
| 297 std::string body = | 273 std::string body = |
| 298 "Please add some informative text about the network issues."; | 274 "Please add some informative text about the network issues."; |
| 299 base::FilePath::StringType file_to_attach(file_to_send.value()); | 275 base::FilePath::StringType file_to_attach(file_to_send.value()); |
| 300 chrome::android::SendEmail( | 276 chrome::android::SendEmail( |
| 301 base::UTF8ToUTF16(email), base::UTF8ToUTF16(subject), | 277 base::UTF8ToUTF16(email), base::UTF8ToUTF16(subject), |
| 302 base::UTF8ToUTF16(body), base::UTF8ToUTF16(title), | 278 base::UTF8ToUTF16(body), base::UTF8ToUTF16(title), |
| 303 base::UTF8ToUTF16(file_to_attach)); | 279 base::UTF8ToUTF16(file_to_attach)); |
| 304 #endif | 280 #endif |
| 305 } | 281 } |
| 306 | 282 |
| 307 // static | 283 // static |
| 308 bool NetExportMessageHandler::UsingMobileUI() { | 284 bool NetExportMessageHandler::UsingMobileUI() { |
| 309 #if defined(OS_ANDROID) || defined(OS_IOS) | 285 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 310 return true; | 286 return true; |
| 311 #else | 287 #else |
| 312 return false; | 288 return false; |
| 313 #endif | 289 #endif |
| 314 } | 290 } |
| 315 | 291 |
| 316 void NetExportMessageHandler::OnExportNetLogInfoChanged(base::Value* arg) { | 292 void NetExportMessageHandler::NotifyUIWithNetLogFileWriterState( |
| 317 std::unique_ptr<base::Value> value(arg); | 293 std::unique_ptr<base::DictionaryValue> file_writer_state) { |
| 318 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 294 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 295 file_writer_state->SetBoolean("useMobileUI", UsingMobileUI()); | |
| 319 web_ui()->CallJavascriptFunctionUnsafe(net_log::kOnExportNetLogInfoChanged, | 296 web_ui()->CallJavascriptFunctionUnsafe(net_log::kOnExportNetLogInfoChanged, |
| 320 *arg); | 297 *file_writer_state); |
| 321 } | 298 } |
| 322 | 299 |
| 323 void NetExportMessageHandler::ShowSelectFileDialog( | 300 void NetExportMessageHandler::ShowSelectFileDialog( |
| 324 const base::FilePath& default_path) { | 301 const base::FilePath& default_path) { |
| 325 // User may have clicked more than once before the save dialog appears. | 302 // User may have clicked more than once before the save dialog appears. |
| 326 // This prevents creating more than one save dialog. | 303 // This prevents creating more than one save dialog. |
| 327 if (select_file_dialog_) | 304 if (select_file_dialog_) |
| 328 return; | 305 return; |
| 329 | 306 |
| 330 WebContents* webcontents = web_ui()->GetWebContents(); | 307 WebContents* webcontents = web_ui()->GetWebContents(); |
| 331 | 308 |
| 332 select_file_dialog_ = ui::SelectFileDialog::Create( | 309 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 333 this, new ChromeSelectFilePolicy(webcontents)); | 310 this, new ChromeSelectFilePolicy(webcontents)); |
| 334 ui::SelectFileDialog::FileTypeInfo file_type_info; | 311 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 335 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; | 312 file_type_info.extensions = {{FILE_PATH_LITERAL("json")}}; |
| 336 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); | 313 gfx::NativeWindow owning_window = webcontents->GetTopLevelNativeWindow(); |
| 337 select_file_dialog_->SelectFile( | 314 select_file_dialog_->SelectFile( |
| 338 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, | 315 ui::SelectFileDialog::SELECT_SAVEAS_FILE, base::string16(), default_path, |
| 339 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); | 316 &file_type_info, 0, base::FilePath::StringType(), owning_window, nullptr); |
| 340 } | 317 } |
| 341 | 318 |
| 342 void NetExportMessageHandler::FileSelected(const base::FilePath& path, | 319 void NetExportMessageHandler::FileSelected(const base::FilePath& path, |
| 343 int index, | 320 int index, |
| 344 void* params) { | 321 void* params) { |
| 345 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 322 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 346 DCHECK(select_file_dialog_); | 323 DCHECK(select_file_dialog_); |
| 347 select_file_dialog_ = nullptr; | 324 select_file_dialog_ = nullptr; |
| 325 *last_save_dir.Pointer() = path.DirName(); | |
| 348 | 326 |
| 349 *last_save_dir.Pointer() = path.DirName(); | 327 StartNetLogAndNotifyUI(path, capture_mode_); |
| 350 BrowserThread::PostTaskAndReply( | |
| 351 BrowserThread::FILE_USER_BLOCKING, FROM_HERE, | |
| 352 base::Bind(&net_log::NetLogFileWriter::SetUpNetExportLogPath, | |
| 353 base::Unretained(net_log_file_writer_), path), | |
| 354 // NetExportMessageHandler is tied to the lifetime of the tab | |
| 355 // so it cannot be assured that it will be valid when this | |
| 356 // StartNetLog is called. Instead of using base::Unretained a | |
| 357 // weak pointer is used to adjust for this. | |
| 358 base::Bind(&NetExportMessageHandler::StartNetLog, | |
| 359 weak_ptr_factory_.GetWeakPtr())); | |
| 360 } | 328 } |
| 361 | 329 |
| 362 void NetExportMessageHandler::FileSelectionCanceled(void* params) { | 330 void NetExportMessageHandler::FileSelectionCanceled(void* params) { |
| 363 DCHECK(select_file_dialog_); | 331 DCHECK(select_file_dialog_); |
| 364 select_file_dialog_ = nullptr; | 332 select_file_dialog_ = nullptr; |
| 365 } | 333 } |
| 366 | 334 |
| 367 } // namespace | 335 } // namespace |
| 368 | 336 |
| 369 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 337 NetExportUI::NetExportUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
| 370 web_ui->AddMessageHandler(new NetExportMessageHandler()); | 338 web_ui->AddMessageHandler(new NetExportMessageHandler()); |
| 371 | 339 |
| 372 // Set up the chrome://net-export/ source. | 340 // Set up the chrome://net-export/ source. |
| 373 Profile* profile = Profile::FromWebUI(web_ui); | 341 Profile* profile = Profile::FromWebUI(web_ui); |
| 374 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); | 342 content::WebUIDataSource::Add(profile, CreateNetExportHTMLSource()); |
| 375 } | 343 } |
| OLD | NEW |