url_fetcher_block_adapter.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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
  3. // found in the LICENSE file.
  4. #ifndef IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_
  5. #define IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_
  6. #import <Foundation/Foundation.h>
  7. #include <memory>
  8. #include "base/memory/scoped_refptr.h"
  9. #include "url/gurl.h"
  10. namespace network {
  11. class SharedURLLoaderFactory;
  12. class SimpleURLLoader;
  13. } // namespace network
  14. namespace web {
  15. // Class for use of URLLoader from Objective-C with a completion handler block.
  16. class URLFetcherBlockAdapter;
  17. // Block type for URLFetcherBlockAdapter callbacks.
  18. typedef void (^URLFetcherBlockAdapterCompletion)(NSData*,
  19. URLFetcherBlockAdapter*);
  20. // Class to manage retrieval of WebUI resources.
  21. class URLFetcherBlockAdapter {
  22. public:
  23. // Creates URLFetcherBlockAdapter for resource at |url| with
  24. // |request_context|.
  25. // |completion_handler| is called with results of the fetch.
  26. URLFetcherBlockAdapter(
  27. const GURL& url,
  28. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  29. web::URLFetcherBlockAdapterCompletion completion_handler);
  30. virtual ~URLFetcherBlockAdapter();
  31. // Starts the fetch.
  32. virtual void Start();
  33. GURL getUrl() { return url_; }
  34. protected:
  35. void OnURLLoadComplete(std::unique_ptr<std::string> response_body);
  36. private:
  37. // The URL to fetch.
  38. const GURL url_;
  39. // The URL loader factory.
  40. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  41. // Callback for resource load.
  42. __strong web::URLFetcherBlockAdapterCompletion completion_handler_;
  43. // URLLoader for retrieving data from net stack.
  44. std::unique_ptr<network::SimpleURLLoader> url_loader_;
  45. };
  46. } // namespace web
  47. #endif // IOS_WEB_WEBUI_URL_FETCHER_BLOCK_ADAPTER_H_