Commit 3cb2e28e authored by Mark Pilgrim's avatar Mark Pilgrim Committed by Commit Bot

Migrate WebRtcLogUploader to SimpleURLLoader

Bug: 844914
Change-Id: Ie38db098ec0e540b4425a42500c43263ed57119f
Reviewed-on: https://chromium-review.googlesource.com/1102492Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Mark Pilgrim <pilgrim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568076}
parent da1aa26f
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "components/version_info/version_info.h" #include "components/version_info/version_info.h"
#include "components/webrtc_logging/browser/log_cleanup.h" #include "components/webrtc_logging/browser/log_cleanup.h"
#include "components/webrtc_logging/browser/log_list.h" #include "components/webrtc_logging/browser/log_list.h"
...@@ -27,7 +28,9 @@ ...@@ -27,7 +28,9 @@
#include "net/base/mime_util.h" #include "net/base/mime_util.h"
#include "net/http/http_status_code.h" #include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h" #include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h" #include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "third_party/zlib/zlib.h" #include "third_party/zlib/zlib.h"
using content::BrowserThread; using content::BrowserThread;
...@@ -88,7 +91,7 @@ WebRtcLogUploader::WebRtcLogUploader() ...@@ -88,7 +91,7 @@ WebRtcLogUploader::WebRtcLogUploader()
WebRtcLogUploader::~WebRtcLogUploader() { WebRtcLogUploader::~WebRtcLogUploader() {
DCHECK_CALLED_ON_VALID_THREAD(create_thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(create_thread_checker_);
DCHECK(upload_done_data_.empty()); DCHECK(pending_uploads_.empty());
DCHECK(shutting_down_); DCHECK(shutting_down_);
} }
...@@ -292,39 +295,37 @@ void WebRtcLogUploader::StartShutdown() { ...@@ -292,39 +295,37 @@ void WebRtcLogUploader::StartShutdown() {
base::Unretained(this))); base::Unretained(this)));
} }
void WebRtcLogUploader::OnURLFetchComplete(const net::URLFetcher* source) { void WebRtcLogUploader::OnSimpleLoaderComplete(
SimpleURLLoaderList::iterator it,
WebRtcLogUploadDoneData upload_done_data,
std::unique_ptr<std::string> response_body) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(upload_done_data_.find(source) != upload_done_data_.end());
DCHECK(!shutting_down_); DCHECK(!shutting_down_);
int response_code = source->GetResponseCode(); network::SimpleURLLoader* loader = it->get();
UploadDoneDataMap::iterator it = upload_done_data_.find(source); int response_code = -1;
if (it != upload_done_data_.end()) { if (loader->ResponseInfo() && loader->ResponseInfo()->headers) {
// The log path can be empty here if we failed getting it before. We still response_code = loader->ResponseInfo()->headers->response_code();
// upload the log if that's the case.
std::string report_id;
if (response_code == net::HTTP_OK &&
source->GetResponseAsString(&report_id) &&
!it->second.log_path.empty()) {
// TODO(jiayl): Add the RTP dump records to chrome://webrtc-logs.
base::FilePath log_list_path =
webrtc_logging::LogList::GetWebRtcLogListFileForDirectory(
it->second.log_path);
background_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&WebRtcLogUploader::AddUploadedLogInfoToUploadListFile,
log_list_path, it->second.local_log_id, report_id));
}
NotifyUploadDone(response_code, report_id, it->second);
upload_done_data_.erase(it);
} }
pending_uploads_.erase(it);
delete source; std::string report_id;
if (response_body)
report_id = std::move(*response_body);
// The log path can be empty here if we failed getting it before. We still
// upload the log if that's the case.
if (!upload_done_data.log_path.empty()) {
// TODO(jiayl): Add the RTP dump records to chrome://webrtc-logs.
base::FilePath log_list_path =
webrtc_logging::LogList::GetWebRtcLogListFileForDirectory(
upload_done_data.log_path);
background_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&WebRtcLogUploader::AddUploadedLogInfoToUploadListFile,
log_list_path, upload_done_data.local_log_id,
report_id));
}
NotifyUploadDone(response_code, report_id, upload_done_data);
} }
void WebRtcLogUploader::OnURLFetchUploadProgress(const net::URLFetcher* source,
int64_t current,
int64_t total) {}
void WebRtcLogUploader::SetupMultipart( void WebRtcLogUploader::SetupMultipart(
std::string* post_data, std::string* post_data,
const std::string& compressed_log, const std::string& compressed_log,
...@@ -474,35 +475,24 @@ void WebRtcLogUploader::UploadCompressedLog( ...@@ -474,35 +475,24 @@ void WebRtcLogUploader::UploadCompressedLog(
})"); })");
constexpr char kUploadURL[] = "https://clients2.google.com/cr/report"; constexpr char kUploadURL[] = "https://clients2.google.com/cr/report";
std::unique_ptr<net::URLFetcher> url_fetcher(net::URLFetcher::Create( auto resource_request = std::make_unique<network::ResourceRequest>();
GURL(kUploadURL), net::URLFetcher::POST, this, traffic_annotation)); resource_request->url = GURL(kUploadURL);
url_fetcher->SetUploadData(content_type, *post_data); resource_request->load_flags =
url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES;
net::LOAD_DO_NOT_SAVE_COOKIES); resource_request->method = "POST";
BrowserThread::PostTask( std::unique_ptr<network::SimpleURLLoader> simple_url_loader =
BrowserThread::UI, FROM_HERE, network::SimpleURLLoader::Create(std::move(resource_request),
base::BindOnce(&WebRtcLogUploader::SetRequestContextOnUIThread, traffic_annotation);
base::Unretained(this), std::move(url_fetcher), simple_url_loader->AttachStringForUpload(*post_data, content_type);
upload_done_data)); auto it = pending_uploads_.insert(pending_uploads_.begin(),
} std::move(simple_url_loader));
network::SimpleURLLoader* raw_loader = it->get();
void WebRtcLogUploader::SetRequestContextOnUIThread( raw_loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
std::unique_ptr<net::URLFetcher> url_fetcher, g_browser_process->system_network_context_manager()
const WebRtcLogUploadDoneData& data) { ->GetSharedURLLoaderFactory()
DCHECK_CURRENTLY_ON(BrowserThread::UI); .get(),
url_fetcher->SetRequestContext(g_browser_process->system_request_context()); base::BindOnce(&WebRtcLogUploader::OnSimpleLoaderComplete,
BrowserThread::PostTask( base::Unretained(this), std::move(it), upload_done_data));
BrowserThread::IO, FROM_HERE,
base::BindOnce(&WebRtcLogUploader::StartAndTrackRequestContext,
base::Unretained(this), std::move(url_fetcher), data));
}
void WebRtcLogUploader::StartAndTrackRequestContext(
std::unique_ptr<net::URLFetcher> url_fetcher,
const WebRtcLogUploadDoneData& data) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
url_fetcher->Start();
upload_done_data_[url_fetcher.release()] = data;
} }
void WebRtcLogUploader::DecreaseLogCount() { void WebRtcLogUploader::DecreaseLogCount() {
...@@ -514,11 +504,8 @@ void WebRtcLogUploader::ShutdownOnIOThread() { ...@@ -514,11 +504,8 @@ void WebRtcLogUploader::ShutdownOnIOThread() {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!shutting_down_); DCHECK(!shutting_down_);
// Delete all URLFetchers first and clear the upload done map. // Clear the pending uploads list, which will reset all URL loaders.
for (const auto& it : upload_done_data_) pending_uploads_.clear();
delete it.first;
upload_done_data_.clear();
shutting_down_ = true; shutting_down_ = true;
} }
......
...@@ -7,20 +7,18 @@ ...@@ -7,20 +7,18 @@
#include <stdint.h> #include <stdint.h>
#include <map> #include <list>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/threading/thread_checker.h" #include "base/threading/thread_checker.h"
#include "chrome/browser/media/webrtc/webrtc_logging_handler_host.h" #include "chrome/browser/media/webrtc/webrtc_logging_handler_host.h"
#include "net/url_request/url_fetcher_delegate.h"
namespace net { namespace network {
class URLFetcher; class SimpleURLLoader;
} }
typedef struct z_stream_s z_stream; typedef struct z_stream_s z_stream;
...@@ -41,10 +39,10 @@ struct WebRtcLogUploadDoneData : public WebRtcLogPaths { ...@@ -41,10 +39,10 @@ struct WebRtcLogUploadDoneData : public WebRtcLogPaths {
// been started and denies further logs if a limit is reached. It also adds // been started and denies further logs if a limit is reached. It also adds
// the timestamp and report ID of the uploded log to a text file. There must // the timestamp and report ID of the uploded log to a text file. There must
// only be one object of this type. // only be one object of this type.
class WebRtcLogUploader : public net::URLFetcherDelegate { class WebRtcLogUploader {
public: public:
WebRtcLogUploader(); WebRtcLogUploader();
~WebRtcLogUploader() override; ~WebRtcLogUploader();
// Returns true is number of logs limit is not reached yet. Increases log // Returns true is number of logs limit is not reached yet. Increases log
// count if true is returned. Must be called before UploadLog(). // count if true is returned. Must be called before UploadLog().
...@@ -106,12 +104,6 @@ class WebRtcLogUploader : public net::URLFetcherDelegate { ...@@ -106,12 +104,6 @@ class WebRtcLogUploader : public net::URLFetcherDelegate {
FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest, FRIEND_TEST_ALL_PREFIXES(WebRtcLogUploaderTest,
AddUploadedLogInfoToUploadListFile); AddUploadedLogInfoToUploadListFile);
// net::URLFetcherDelegate implementation.
void OnURLFetchComplete(const net::URLFetcher* source) override;
void OnURLFetchUploadProgress(const net::URLFetcher* source,
int64_t current,
int64_t total) override;
// Sets up a multipart body to be uploaded. The body is produced according // Sets up a multipart body to be uploaded. The body is produced according
// to RFC 2046. // to RFC 2046.
void SetupMultipart(std::string* post_data, void SetupMultipart(std::string* post_data,
...@@ -127,13 +119,6 @@ class WebRtcLogUploader : public net::URLFetcherDelegate { ...@@ -127,13 +119,6 @@ class WebRtcLogUploader : public net::URLFetcherDelegate {
void UploadCompressedLog(const WebRtcLogUploadDoneData& upload_done_data, void UploadCompressedLog(const WebRtcLogUploadDoneData& upload_done_data,
std::unique_ptr<std::string> post_data); std::unique_ptr<std::string> post_data);
// A couple of helper functions due to having to hop to the UI thread
// to fetch the system_request_context and back again to the IO thread.
void SetRequestContextOnUIThread(std::unique_ptr<net::URLFetcher>,
const WebRtcLogUploadDoneData& data);
void StartAndTrackRequestContext(std::unique_ptr<net::URLFetcher>,
const WebRtcLogUploadDoneData& data);
void DecreaseLogCount(); void DecreaseLogCount();
void ShutdownOnIOThread(); void ShutdownOnIOThread();
...@@ -175,6 +160,13 @@ class WebRtcLogUploader : public net::URLFetcherDelegate { ...@@ -175,6 +160,13 @@ class WebRtcLogUploader : public net::URLFetcherDelegate {
const std::string& report_id, const std::string& report_id,
const WebRtcLogUploadDoneData& upload_done_data); const WebRtcLogUploadDoneData& upload_done_data);
using SimpleURLLoaderList =
std::list<std::unique_ptr<network::SimpleURLLoader>>;
void OnSimpleLoaderComplete(SimpleURLLoaderList::iterator it,
WebRtcLogUploadDoneData upload_done_data,
std::unique_ptr<std::string> response_body);
// This is the UI thread for Chromium. Some other thread for tests. // This is the UI thread for Chromium. Some other thread for tests.
THREAD_CHECKER(create_thread_checker_); THREAD_CHECKER(create_thread_checker_);
...@@ -190,12 +182,10 @@ class WebRtcLogUploader : public net::URLFetcherDelegate { ...@@ -190,12 +182,10 @@ class WebRtcLogUploader : public net::URLFetcherDelegate {
// on the FILE thread. // on the FILE thread.
std::string* post_data_; std::string* post_data_;
typedef std::map<const net::URLFetcher*, WebRtcLogUploadDoneData>
UploadDoneDataMap;
// Only accessed on the IO thread. // Only accessed on the IO thread.
UploadDoneDataMap upload_done_data_; SimpleURLLoaderList pending_uploads_;
// When shutting down, don't create new URLFetchers. // When shutting down, don't create new URL loaders.
bool shutting_down_; bool shutting_down_;
DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader); DISALLOW_COPY_AND_ASSIGN(WebRtcLogUploader);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment