Commit 8a0e9ba9 authored by Eugene But's avatar Eugene But Committed by Commit Bot

Added DownloadTask::GetHttpCode() method.

This method will be user for reporting Download.IOSDownloadPassKitResult
UMA metric.

Bug: 789735
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ic39d5ea1869dfb4e7f45e0ffa2902829a2e09a6d
Reviewed-on: https://chromium-review.googlesource.com/817950
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: default avatarGregory Chatzinoff <gchatz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523232}
parent e9d9b2bc
......@@ -98,6 +98,7 @@ TEST_F(DownloadTest, SucessfullDownload) {
EXPECT_EQ(0, task->GetErrorCode());
EXPECT_EQ(static_cast<int64_t>(strlen(kContent)), task->GetTotalBytes());
EXPECT_EQ(100, task->GetPercentComplete());
EXPECT_EQ(200, task->GetHttpCode());
EXPECT_EQ(kContent, task->GetResponseWriter()->AsStringWriter()->data());
}
......
......@@ -61,6 +61,7 @@ class DownloadTaskImpl : public DownloadTask {
const GURL& GetOriginalUrl() const override;
bool IsDone() const override;
int GetErrorCode() const override;
int GetHttpCode() const override;
int64_t GetTotalBytes() const override;
int GetPercentComplete() const override;
std::string GetContentDisposition() const override;
......@@ -102,6 +103,7 @@ class DownloadTaskImpl : public DownloadTask {
GURL original_url_;
bool is_done_ = false;
int error_code_ = 0;
int http_code_ = -1;
int64_t total_bytes_ = -1;
int percent_complete_ = -1;
std::string content_disposition_;
......
......@@ -195,6 +195,11 @@ int DownloadTaskImpl::GetErrorCode() const {
return error_code_;
}
int DownloadTaskImpl::GetHttpCode() const {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
return http_code_;
}
int64_t DownloadTaskImpl::GetTotalBytes() const {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
return total_bytes_;
......@@ -249,8 +254,13 @@ NSURLSession* DownloadTaskImpl::CreateSession(NSString* identifier) {
error_code_ = GetNetErrorCodeFromNSError(error);
percent_complete_ = GetTaskPercentComplete(task);
total_bytes_ = task.countOfBytesExpectedToReceive;
if (task.response.MIMEType)
if (task.response.MIMEType) {
mime_type_ = base::SysNSStringToUTF8(task.response.MIMEType);
}
if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
http_code_ =
static_cast<NSHTTPURLResponse*>(task.response).statusCode;
}
if (task.state != NSURLSessionTaskStateCompleted) {
OnDownloadUpdated();
......
......@@ -197,6 +197,7 @@ TEST_F(DownloadTaskImplTest, DefaultState) {
EXPECT_EQ(kUrl, task_->GetOriginalUrl());
EXPECT_FALSE(task_->IsDone());
EXPECT_EQ(0, task_->GetErrorCode());
EXPECT_EQ(-1, task_->GetHttpCode());
EXPECT_EQ(-1, task_->GetTotalBytes());
EXPECT_EQ(-1, task_->GetPercentComplete());
EXPECT_EQ(kContentDisposition, task_->GetContentDisposition());
......@@ -486,6 +487,32 @@ TEST_F(DownloadTaskImplTest, MimeTypeChange) {
EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}
// Tests updating HTTP response code.
TEST_F(DownloadTaskImplTest, HttpResponseCode) {
EXPECT_CALL(task_observer_, OnDownloadUpdated(task_.get()));
CRWFakeNSURLSessionTask* session_task = Start();
ASSERT_TRUE(session_task);
testing::Mock::VerifyAndClearExpectations(&task_observer_);
// Download has finished with a different MIME type.
ASSERT_EQ(kMimeType, task_->GetMimeType());
EXPECT_CALL(task_observer_, OnDownloadUpdated(task_.get()));
int kHttpCode = 303;
session_task.response =
[[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@(kUrl)]
statusCode:303
HTTPVersion:nil
headerFields:nil];
SimulateDownloadCompletion(session_task);
testing::Mock::VerifyAndClearExpectations(&task_observer_);
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForDownloadTimeout, ^{
return task_->IsDone();
}));
EXPECT_EQ(kHttpCode, task_->GetHttpCode());
EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}
// Tests that destructing DownloadTaskImpl calls -[NSURLSessionDataTask cancel]
// and OnTaskDestroyed().
TEST_F(DownloadTaskImplTest, DownloadTaskDestruction) {
......
......@@ -54,6 +54,10 @@ class DownloadTask {
// possible error codes.
virtual int GetErrorCode() const = 0;
// HTTP response code for this download task. -1 the response has not been
// received yet or the response not an HTTP response.
virtual int GetHttpCode() const = 0;
// Total number of expected bytes (a best-guess upper-bound). Returns -1 if
// the total size is unknown.
virtual int64_t GetTotalBytes() const = 0;
......
......@@ -27,6 +27,7 @@ class FakeDownloadTask : public DownloadTask {
const GURL& GetOriginalUrl() const override;
bool IsDone() const override;
int GetErrorCode() const override;
int GetHttpCode() const override;
int64_t GetTotalBytes() const override;
int GetPercentComplete() const override;
std::string GetContentDisposition() const override;
......@@ -38,6 +39,7 @@ class FakeDownloadTask : public DownloadTask {
// Setters for task properties. Setters invoke OnDownloadUpdated callback.
void SetDone(bool done);
void SetErrorCode(int error_code);
void SetHttpCode(int http_code);
void SetTotalBytes(int64_t total_bytes);
void SetPercentComplete(int percent_complete);
void SetContentDisposition(const std::string& content_disposition);
......@@ -54,6 +56,7 @@ class FakeDownloadTask : public DownloadTask {
GURL original_url_;
bool is_done_ = false;
int error_code_ = 0;
int http_code_ = -1;
std::string content_disposition_;
int64_t total_bytes_ = -1;
int percent_complete_ = -1;
......
......@@ -45,6 +45,10 @@ int FakeDownloadTask::GetErrorCode() const {
return error_code_;
}
int FakeDownloadTask::GetHttpCode() const {
return http_code_;
}
int64_t FakeDownloadTask::GetTotalBytes() const {
return total_bytes_;
}
......@@ -85,6 +89,11 @@ void FakeDownloadTask::SetErrorCode(int error_code) {
OnDownloadUpdated();
}
void FakeDownloadTask::SetHttpCode(int http_code) {
http_code_ = http_code;
OnDownloadUpdated();
}
void FakeDownloadTask::SetTotalBytes(int64_t total_bytes) {
total_bytes_ = total_bytes;
OnDownloadUpdated();
......
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