OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #ifndef CONTENT_RENDERER_FETCHERS_MANIFEST_FETCHER_H_ |
| 6 #define CONTENT_RENDERER_FETCHERS_MANIFEST_FETCHER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" |
| 14 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 15 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 16 |
| 17 class GURL; |
| 18 |
| 19 namespace blink { |
| 20 class WebFrame; |
| 21 class WebURLLoader; |
| 22 struct WebURLError; |
| 23 } |
| 24 |
| 25 namespace content { |
| 26 |
| 27 // Helper class to download a Web Manifest. When an instance is created, the |
| 28 // caller need to call Start() and wait for the passed callback to be executed. |
| 29 // If the fetch fails, the callback will be called with two empty objects. |
| 30 // |
| 31 // This class is not using ResourceFetcher because ResourceFetcherImpl is using |
| 32 // Platform::createURLLoader() instead of createAssociatedURLLoader(). The |
| 33 // former is too permissive with regards to CORS and mixed content. |
| 34 class ManifestFetcher : public blink::WebURLLoaderClient { |
| 35 public: |
| 36 // This will be called asynchronously after the URL has been fetched, |
| 37 // successfully or not. If there is a failure, response and data will both be |
| 38 // empty. |response| and |data| are both valid until the URLFetcher instance |
| 39 // is destroyed. |
| 40 typedef base::Callback<void(const blink::WebURLResponse& response, |
| 41 const std::string& data)> Callback; |
| 42 |
| 43 explicit ManifestFetcher(const GURL& url); |
| 44 virtual ~ManifestFetcher(); |
| 45 |
| 46 void Start(blink::WebFrame* frame, const Callback& callback); |
| 47 void Cancel(); |
| 48 |
| 49 private: |
| 50 void RunCallback(const blink::WebURLResponse& response, |
| 51 const std::string& data); |
| 52 |
| 53 // WebURLLoaderClient implementation. |
| 54 virtual void didReceiveResponse( |
| 55 blink::WebURLLoader* loader, const blink::WebURLResponse& response); |
| 56 virtual void didReceiveData( |
| 57 blink::WebURLLoader* loader, const char* data, int data_length, |
| 58 int encoded_data_length); |
| 59 virtual void didFinishLoading( |
| 60 blink::WebURLLoader* loader, double finishTime, |
| 61 int64_t total_encoded_data_length); |
| 62 virtual void didFail( |
| 63 blink::WebURLLoader* loader, const blink::WebURLError& error); |
| 64 |
| 65 scoped_ptr<blink::WebURLLoader> loader_; |
| 66 |
| 67 // Request to send. Released once Start() is called. |
| 68 blink::WebURLRequest request_; |
| 69 |
| 70 // Set to true once the request is complete. |
| 71 bool completed_; |
| 72 |
| 73 // Buffer to hold the content from the server. |
| 74 std::string data_; |
| 75 |
| 76 // A copy of the original resource response. |
| 77 blink::WebURLResponse response_; |
| 78 |
| 79 // Callback when we're done. |
| 80 Callback callback_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(ManifestFetcher); |
| 83 }; |
| 84 |
| 85 } // namespace content |
| 86 |
| 87 #endif // CONTENT_RENDERER_FETCHERS_MANIFEST_FETCHER_H_ |
OLD | NEW |