Home | History | Annotate | Download | only in test
      1 // Copyright 2013 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.
      4 
      5 #include "content/test/weburl_loader_mock.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/test/weburl_loader_mock_factory.h"
      9 #include "third_party/WebKit/public/platform/WebData.h"
     10 #include "third_party/WebKit/public/platform/WebURLError.h"
     11 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
     12 
     13 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory* factory,
     14                                    blink::WebURLLoader* default_loader)
     15     : factory_(factory),
     16       client_(NULL),
     17       default_loader_(default_loader),
     18       using_default_loader_(false),
     19       is_deferred_(false),
     20       this_deleted_(NULL) {
     21 }
     22 
     23 WebURLLoaderMock::~WebURLLoaderMock() {
     24   // When |this_deleted_| is not null, there is someone interested to know if
     25   // |this| got deleted. We notify them by setting the pointed value to true.
     26   if (this_deleted_)
     27     *this_deleted_ = true;
     28 }
     29 
     30 void WebURLLoaderMock::ServeAsynchronousRequest(
     31     const blink::WebURLResponse& response,
     32     const blink::WebData& data,
     33     const blink::WebURLError& error) {
     34   DCHECK(!using_default_loader_);
     35   if (!client_)
     36     return;
     37 
     38   bool this_deleted = false;
     39   this_deleted_ = &this_deleted;
     40   client_->didReceiveResponse(this, response);
     41 
     42   // didReceiveResponse might end up getting ::cancel() to be called which will
     43   // make the ResourceLoader to delete |this|. If that happens, |this_deleted|,
     44   // created on the stack, will be set to true.
     45   if (this_deleted)
     46     return;
     47   this_deleted_ = NULL;
     48 
     49   if (error.reason) {
     50     client_->didFail(this, error);
     51     return;
     52   }
     53   client_->didReceiveData(this, data.data(), data.size(), data.size());
     54   client_->didFinishLoading(this, 0, data.size());
     55 }
     56 
     57 blink::WebURLRequest WebURLLoaderMock::ServeRedirect(
     58     const blink::WebURLResponse& redirectResponse) {
     59   blink::WebURLRequest newRequest;
     60   newRequest.initialize();
     61   newRequest.setRequestContext(blink::WebURLRequest::RequestContextInternal);
     62   GURL redirectURL(redirectResponse.httpHeaderField("Location"));
     63   newRequest.setURL(redirectURL);
     64   client_->willSendRequest(this, newRequest, redirectResponse);
     65   return newRequest;
     66 }
     67 
     68 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest& request,
     69                                          blink::WebURLResponse& response,
     70                                          blink::WebURLError& error,
     71                                          blink::WebData& data) {
     72   if (factory_->IsMockedURL(request.url())) {
     73     factory_->LoadSynchronously(request, &response, &error, &data);
     74     return;
     75   }
     76   DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
     77       << "loadSynchronously shouldn't be falling back: "
     78       << request.url().spec().data();
     79   using_default_loader_ = true;
     80   default_loader_->loadSynchronously(request, response, error, data);
     81 }
     82 
     83 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest& request,
     84                                           blink::WebURLLoaderClient* client) {
     85   if (factory_->IsMockedURL(request.url())) {
     86     client_ = client;
     87     factory_->LoadAsynchronouly(request, this);
     88     return;
     89   }
     90   DCHECK(static_cast<const GURL&>(request.url()).SchemeIs("data"))
     91       << "loadAsynchronously shouldn't be falling back: "
     92       << request.url().spec().data();
     93   using_default_loader_ = true;
     94   default_loader_->loadAsynchronously(request, client);
     95 }
     96 
     97 void WebURLLoaderMock::cancel() {
     98   if (using_default_loader_) {
     99     default_loader_->cancel();
    100     return;
    101   }
    102   client_ = NULL;
    103   factory_->CancelLoad(this);
    104 }
    105 
    106 void WebURLLoaderMock::setDefersLoading(bool deferred) {
    107   is_deferred_ = deferred;
    108   if (using_default_loader_) {
    109     default_loader_->setDefersLoading(deferred);
    110     return;
    111   }
    112   NOTIMPLEMENTED();
    113 }
    114