summaryrefslogtreecommitdiffstats
path: root/ios/web/webui/url_fetcher_block_adapter.h
blob: 095240b404dd253ae837b33680511df6f46eddda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_
#define IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_

#import <Foundation/Foundation.h>

#include "base/mac/scoped_block.h"
#include "base/memory/scoped_ptr.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "url/gurl.h"

namespace net {
class URLFetcher;
class URLRequestContextGetter;
}  // namespace net

namespace web {

// Class for use of URLFetcher from Objective-C with a completion handler block.
class URLFetcherBlockAdapter;
// Block type for URLFetcherBlockAdapter callbacks.
typedef void (^URLFetcherBlockAdapterCompletion)(NSData*,
                                                 URLFetcherBlockAdapter*);

// Class to manage retrieval of WebUI resources.
class URLFetcherBlockAdapter : public net::URLFetcherDelegate {
 public:
  // Creates URLFetcherBlockAdapter for resource at |url| with
  // |request_context|.
  // |completion_handler| is called with results of the fetch.
  URLFetcherBlockAdapter(
      const GURL& url,
      net::URLRequestContextGetter* request_context,
      web::URLFetcherBlockAdapterCompletion completion_handler);
  ~URLFetcherBlockAdapter() override;
  // Starts the fetch.
  virtual void Start();

 protected:
  // net::URLFetcherDelegate implementation.
  void OnURLFetchComplete(const net::URLFetcher* source) override;

 private:
  // The URL to fetch.
  const GURL url_;
  // The request context.
  net::URLRequestContextGetter* request_context_;
  // Callback for resource load.
  base::mac::ScopedBlock<web::URLFetcherBlockAdapterCompletion>
      completion_handler_;
  // URLFetcher for retrieving data from net stack.
  scoped_ptr<net::URLFetcher> fetcher_;
};

}  // namespace web

#endif  // IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_