Commit fe20f163 authored by Eugene But's avatar Eugene But Committed by Commit Bot

Allow changing DownloadTask's mime type during the download.

Response's MIME type can change during the download and DownloadTask
should support the change. This will allow to report
DOWNLOAD_PASS_KIT_WRONG_MIME_TYPE_FAILURE metric for PassKit downloads.

Bug: 787943
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I137f9aa4ff966c140a9a6cb5b03dc026062bc0b7
Reviewed-on: https://chromium-review.googlesource.com/786265Reviewed-by: default avatarGregory Chatzinoff <gchatz@chromium.org>
Commit-Queue: Eugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#519521}
parent 66bf5121
......@@ -8,6 +8,7 @@
#import <WebKit/WebKit.h>
#import "base/mac/bind_objc_block.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/web/net/cookies/wk_cookie_util.h"
#include "ios/web/public/browser_state.h"
#import "ios/web/public/download/download_task_observer.h"
......@@ -248,6 +249,8 @@ NSURLSession* DownloadTaskImpl::CreateSession(NSString* identifier) {
error_code_ = GetNetErrorCodeFromNSError(error);
percent_complete_ = GetTaskPercentComplete(task);
total_bytes_ = task.countOfBytesExpectedToReceive;
if (task.response.MIMEType)
mime_type_ = base::SysNSStringToUTF8(task.response.MIMEType);
if (task.state != NSURLSessionTaskStateCompleted) {
OnDownloadUpdated();
......
......@@ -460,6 +460,32 @@ TEST_F(DownloadTaskImplTest, FileDeletion) {
EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}
// Tests changing MIME type during the download.
TEST_F(DownloadTaskImplTest, MimeTypeChange) {
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()));
const char kOtherMimeType[] = "application/foo";
session_task.response =
[[NSURLResponse alloc] initWithURL:[NSURL URLWithString:@(kUrl)]
MIMEType:@(kOtherMimeType)
expectedContentLength:0
textEncodingName:nil];
SimulateDownloadCompletion(session_task);
testing::Mock::VerifyAndClearExpectations(&task_observer_);
ASSERT_TRUE(WaitUntilConditionOrTimeout(kWaitForDownloadTimeout, ^{
return task_->IsDone();
}));
EXPECT_EQ(kOtherMimeType, task_->GetMimeType());
EXPECT_CALL(task_delegate_, OnTaskDestroyed(task_.get()));
}
// Tests that destructing DownloadTaskImpl calls -[NSURLSessionDataTask cancel]
// and OnTaskDestroyed().
TEST_F(DownloadTaskImplTest, DownloadTaskDestruction) {
......
......@@ -18,6 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
@property(nonatomic) int64_t countOfBytesReceived;
@property(nonatomic) int64_t countOfBytesExpectedToReceive;
@property(nonatomic) NSURLSessionTaskState state;
@property(nonatomic, nullable, copy) NSURLResponse* response;
- (nullable instancetype)initWithURL:(NSURL*)URL NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)init NS_UNAVAILABLE;
......
......@@ -21,6 +21,7 @@
@synthesize state = _state;
@synthesize originalRequest = _originalRequest;
@synthesize currentRequest = _currentRequest;
@synthesize response = _response;
- (instancetype)initWithURL:(NSURL*)URL {
if ((self = [super init])) {
......
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