Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / sim / SimNetwork.cpp
blobdbf3324ec1926f1068fe55af0eaf03e435567af5
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "config.h"
6 #include "web/tests/sim/SimNetwork.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/WebURLError.h"
10 #include "public/platform/WebURLLoader.h"
11 #include "public/platform/WebURLLoaderClient.h"
12 #include "public/platform/WebURLResponse.h"
13 #include "public/platform/WebUnitTestSupport.h"
14 #include "web/tests/sim/SimRequest.h"
16 namespace blink {
18 static SimNetwork* s_network = nullptr;
20 SimNetwork::SimNetwork()
21 : m_currentRequest(nullptr)
23 Platform::current()->unitTestSupport()->setLoaderDelegate(this);
24 ASSERT(!s_network);
25 s_network = this;
28 SimNetwork::~SimNetwork()
30 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr);
31 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
32 s_network = nullptr;
35 SimNetwork& SimNetwork::current()
37 ASSERT(s_network);
38 return *s_network;
41 void SimNetwork::servePendingRequests()
43 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
46 void SimNetwork::didReceiveResponse(WebURLLoaderClient* client, WebURLLoader* loader, const WebURLResponse& response)
48 auto it = m_requests.find(response.url().string());
49 if (it == m_requests.end()) {
50 client->didReceiveResponse(loader, response);
51 return;
53 ASSERT(it->value);
54 m_currentRequest = it->value;
55 m_currentRequest->didReceiveResponse(client, loader, response);
58 void SimNetwork::didReceiveData(WebURLLoaderClient* client, WebURLLoader* loader, const char* data, int dataLength, int encodedDataLength)
60 if (!m_currentRequest)
61 client->didReceiveData(loader, data, dataLength, encodedDataLength);
64 void SimNetwork::didFail(WebURLLoaderClient* client, WebURLLoader* loader, const WebURLError& error)
66 if (!m_currentRequest) {
67 client->didFail(loader, error);
68 return;
70 m_currentRequest->didFail(error);
73 void SimNetwork::didFinishLoading(WebURLLoaderClient* client, WebURLLoader* loader, double finishTime, int64_t totalEncodedDataLength)
75 if (!m_currentRequest) {
76 client->didFinishLoading(loader, finishTime, totalEncodedDataLength);
77 return;
79 m_currentRequest = nullptr;
82 void SimNetwork::addRequest(SimRequest& request)
84 m_requests.add(request.url(), &request);
87 void SimNetwork::removeRequest(SimRequest& request)
89 m_requests.remove(request.url());
92 } // namespace blink