| OLD | NEW | 
|   1 // Copyright 2015 The Chromium Authors. All rights reserved. |   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 |   2 // Use of this source code is governed by a BSD-style license that can be | 
|   3 // found in the LICENSE file. |   3 // found in the LICENSE file. | 
|   4  |   4  | 
|   5 #include "web/tests/sim/SimRequest.h" |   5 #include "web/tests/sim/SimRequest.h" | 
|   6  |   6  | 
|   7 #include "platform/weborigin/KURL.h" |   7 #include "platform/weborigin/KURL.h" | 
|   8 #include "public/platform/Platform.h" |   8 #include "public/platform/Platform.h" | 
|   9 #include "public/platform/WebURLLoaderClient.h" |   9 #include "public/platform/WebURLLoaderClient.h" | 
|  10 #include "public/platform/WebURLLoaderMockFactory.h" |  10 #include "public/platform/WebURLLoaderMockFactory.h" | 
|  11 #include "web/tests/sim/SimNetwork.h" |  11 #include "web/tests/sim/SimNetwork.h" | 
|  12  |  12  | 
|  13 namespace blink { |  13 namespace blink { | 
|  14  |  14  | 
|  15 SimRequest::SimRequest(String url, String mimeType) |  15 SimRequest::SimRequest(String url, String mimeType) | 
|  16     : m_url(url), |  16     : m_url(url), | 
|  17       m_loader(nullptr), |  | 
|  18       m_client(nullptr), |  17       m_client(nullptr), | 
|  19       m_totalEncodedDataLength(0), |  18       m_totalEncodedDataLength(0), | 
|  20       m_isReady(false) { |  19       m_isReady(false) { | 
|  21   KURL fullUrl(ParsedURLString, url); |  20   KURL fullUrl(ParsedURLString, url); | 
|  22   WebURLResponse response(fullUrl); |  21   WebURLResponse response(fullUrl); | 
|  23   response.setMIMEType(mimeType); |  22   response.setMIMEType(mimeType); | 
|  24   response.setHTTPStatusCode(200); |  23   response.setHTTPStatusCode(200); | 
|  25   Platform::current()->getURLLoaderMockFactory()->registerURL(fullUrl, response, |  24   Platform::current()->getURLLoaderMockFactory()->registerURL(fullUrl, response, | 
|  26                                                               ""); |  25                                                               ""); | 
|  27   SimNetwork::current().addRequest(*this); |  26   SimNetwork::current().addRequest(*this); | 
|  28 } |  27 } | 
|  29  |  28  | 
|  30 SimRequest::~SimRequest() { |  29 SimRequest::~SimRequest() { | 
|  31   DCHECK(!m_isReady); |  30   DCHECK(!m_isReady); | 
|  32 } |  31 } | 
|  33  |  32  | 
|  34 void SimRequest::didReceiveResponse(WebURLLoaderClient* client, |  33 void SimRequest::didReceiveResponse(WebURLLoaderClient* client, | 
|  35                                     WebURLLoader* loader, |  | 
|  36                                     const WebURLResponse& response) { |  34                                     const WebURLResponse& response) { | 
|  37   m_client = client; |  35   m_client = client; | 
|  38   m_loader = loader; |  | 
|  39   m_response = response; |  36   m_response = response; | 
|  40   m_isReady = true; |  37   m_isReady = true; | 
|  41 } |  38 } | 
|  42  |  39  | 
|  43 void SimRequest::didFail(const WebURLError& error) { |  40 void SimRequest::didFail(const WebURLError& error) { | 
|  44   m_error = error; |  41   m_error = error; | 
|  45 } |  42 } | 
|  46  |  43  | 
|  47 void SimRequest::start() { |  44 void SimRequest::start() { | 
|  48   SimNetwork::current().servePendingRequests(); |  45   SimNetwork::current().servePendingRequests(); | 
|  49   DCHECK(m_isReady); |  46   DCHECK(m_isReady); | 
|  50   m_client->didReceiveResponse(m_loader, m_response); |  47   m_client->didReceiveResponse(m_response); | 
|  51 } |  48 } | 
|  52  |  49  | 
|  53 void SimRequest::write(const String& data) { |  50 void SimRequest::write(const String& data) { | 
|  54   DCHECK(m_isReady); |  51   DCHECK(m_isReady); | 
|  55   DCHECK(!m_error.reason); |  52   DCHECK(!m_error.reason); | 
|  56   m_totalEncodedDataLength += data.length(); |  53   m_totalEncodedDataLength += data.length(); | 
|  57   m_client->didReceiveData(m_loader, data.utf8().data(), data.length(), |  54   m_client->didReceiveData(data.utf8().data(), data.length(), data.length()); | 
|  58                            data.length()); |  | 
|  59 } |  55 } | 
|  60  |  56  | 
|  61 void SimRequest::finish() { |  57 void SimRequest::finish() { | 
|  62   DCHECK(m_isReady); |  58   DCHECK(m_isReady); | 
|  63   if (m_error.reason) { |  59   if (m_error.reason) { | 
|  64     m_client->didFail(m_loader, m_error, m_totalEncodedDataLength, |  60     m_client->didFail(m_error, m_totalEncodedDataLength, | 
|  65                       m_totalEncodedDataLength); |  61                       m_totalEncodedDataLength); | 
|  66   } else { |  62   } else { | 
|  67     // TODO(esprehn): Is claiming a request time of 0 okay for tests? |  63     // TODO(esprehn): Is claiming a request time of 0 okay for tests? | 
|  68     m_client->didFinishLoading(m_loader, 0, m_totalEncodedDataLength, |  64     m_client->didFinishLoading(0, m_totalEncodedDataLength, | 
|  69                                m_totalEncodedDataLength); |  65                                m_totalEncodedDataLength); | 
|  70   } |  66   } | 
|  71   reset(); |  67   reset(); | 
|  72 } |  68 } | 
|  73  |  69  | 
|  74 void SimRequest::complete(const String& data) { |  70 void SimRequest::complete(const String& data) { | 
|  75   start(); |  71   start(); | 
|  76   if (!data.isEmpty()) |  72   if (!data.isEmpty()) | 
|  77     write(data); |  73     write(data); | 
|  78   finish(); |  74   finish(); | 
|  79 } |  75 } | 
|  80  |  76  | 
|  81 void SimRequest::reset() { |  77 void SimRequest::reset() { | 
|  82   m_isReady = false; |  78   m_isReady = false; | 
|  83   m_client = nullptr; |  79   m_client = nullptr; | 
|  84   m_loader = nullptr; |  | 
|  85   SimNetwork::current().removeRequest(*this); |  80   SimNetwork::current().removeRequest(*this); | 
|  86 } |  81 } | 
|  87  |  82  | 
|  88 }  // namespace blink |  83 }  // namespace blink | 
| OLD | NEW |