forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_pac_file_fetcher.cc
81 lines (63 loc) · 2.16 KB
/
mock_pac_file_fetcher.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/proxy_resolution/mock_pac_file_fetcher.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/net_errors.h"
namespace net {
MockPacFileFetcher::MockPacFileFetcher()
: pending_request_text_(NULL),
is_shutdown_(false) {}
MockPacFileFetcher::~MockPacFileFetcher() = default;
// PacFileFetcher implementation.
int MockPacFileFetcher::Fetch(
const GURL& url,
base::string16* text,
CompletionOnceCallback callback,
const NetworkTrafficAnnotationTag traffic_annotation) {
DCHECK(!has_pending_request());
if (on_fetch_complete_)
std::move(on_fetch_complete_).Run();
if (is_shutdown_)
return ERR_CONTEXT_SHUT_DOWN;
// Save the caller's information, and have them wait.
pending_request_url_ = url;
pending_request_callback_ = std::move(callback);
pending_request_text_ = text;
return ERR_IO_PENDING;
}
void MockPacFileFetcher::NotifyFetchCompletion(int result,
const std::string& ascii_text) {
DCHECK(has_pending_request());
*pending_request_text_ = base::ASCIIToUTF16(ascii_text);
base::ResetAndReturn(&pending_request_callback_).Run(result);
}
void MockPacFileFetcher::Cancel() {
pending_request_callback_.Reset();
}
void MockPacFileFetcher::OnShutdown() {
is_shutdown_ = true;
if (pending_request_callback_) {
base::ResetAndReturn(&pending_request_callback_).Run(ERR_CONTEXT_SHUT_DOWN);
}
}
URLRequestContext* MockPacFileFetcher::GetRequestContext() const {
return NULL;
}
const GURL& MockPacFileFetcher::pending_request_url() const {
return pending_request_url_;
}
bool MockPacFileFetcher::has_pending_request() const {
return !pending_request_callback_.is_null();
}
void MockPacFileFetcher::WaitUntilFetch() {
DCHECK(!has_pending_request());
base::RunLoop run_loop;
on_fetch_complete_ = run_loop.QuitClosure();
run_loop.Run();
}
} // namespace net