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.
5 #ifndef CONTENT_PUBLIC_RENDERER_RESOURCE_FETCHER_H_
6 #define CONTENT_PUBLIC_RENDERER_RESOURCE_FETCHER_H_
10 #include "base/callback.h"
11 #include "content/common/content_export.h"
12 #include "third_party/WebKit/public/platform/WebURLRequest.h"
23 struct WebURLLoaderOptions
;
28 // Interface to download resources asynchronously.
29 class CONTENT_EXPORT ResourceFetcher
{
32 PLATFORM_LOADER
, // uses Platform::createURLLoader
33 FRAME_ASSOCIATED_LOADER
, // uses WebFrame::createAssociatedURLLoader
36 virtual ~ResourceFetcher() {}
38 // This will be called asynchronously after the URL has been fetched,
39 // successfully or not. If there is a failure, response and data will both be
40 // empty. |response| and |data| are both valid until the URLFetcher instance
42 typedef base::Callback
<void(const blink::WebURLResponse
& response
,
43 const std::string
& data
)> Callback
;
45 // Creates a ResourceFetcher for the specified resource. Caller takes
46 // ownership of the returned object. Deleting the ResourceFetcher will cancel
47 // the request, and the callback will never be run.
48 static ResourceFetcher
* Create(const GURL
& url
);
50 // Set the corresponding parameters of the request. Must be called before
51 // Start. By default, requests are GETs with no body and respect the default
53 virtual void SetMethod(const std::string
& method
) = 0;
54 virtual void SetBody(const std::string
& body
) = 0;
55 virtual void SetHeader(const std::string
& header
,
56 const std::string
& value
) = 0;
57 virtual void SetSkipServiceWorker(bool skip_service_worker
) = 0;
58 virtual void SetCachePolicy(blink::WebURLRequest::CachePolicy policy
) = 0;
60 // Associate the corresponding WebURLLoaderOptions to the loader. Must be
61 // called before Start. Used if the LoaderType is FRAME_ASSOCIATED_LOADER.
62 virtual void SetLoaderOptions(const blink::WebURLLoaderOptions
& options
) = 0;
64 // Starts the request using the specified frame. Calls |callback| when
66 virtual void Start(blink::WebFrame
* frame
,
67 blink::WebURLRequest::RequestContext request_context
,
68 blink::WebURLRequest::FrameType frame_type
,
69 LoaderType loader_type
,
70 const Callback
& callback
) = 0;
72 // Sets how long to wait for the server to reply. By default, there is no
73 // timeout. Must be called after a request is started.
74 virtual void SetTimeout(const base::TimeDelta
& timeout
) = 0;
76 // Manually cancel the request.
77 virtual void Cancel() = 0;
80 } // namespace content
82 #endif // CONTENT_PUBLIC_RENDERER_RESOURCE_FETCHER_H_