geturl_handler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* geturl_handler.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef EXAMPLES_GETURL_GETURL_HANDLER_H_
  30. #define EXAMPLES_GETURL_GETURL_HANDLER_H_
  31. #include "core/ustring.h"
  32. #include "core/vector.h"
  33. #include "ppapi/cpp/completion_callback.h"
  34. #include "ppapi/cpp/url_loader.h"
  35. #include "ppapi/cpp/url_request_info.h"
  36. #include "ppapi/cpp/instance.h"
  37. #include "ppapi/utility/completion_callback_factory.h"
  38. #define READ_BUFFER_SIZE 32768
  39. // GetURLHandler is used to download data from |url|. When download is
  40. // finished or when an error occurs, it posts a message back to the browser
  41. // with the results encoded in the message as a string and self-destroys.
  42. //
  43. // EXAMPLE USAGE:
  44. // GetURLHandler* handler* = GetURLHandler::Create(instance,url);
  45. // handler->Start();
  46. //
  47. class GetURLHandler {
  48. public:
  49. enum Status {
  50. STATUS_NONE,
  51. STATUS_IN_PROGRESS,
  52. STATUS_COMPLETED,
  53. STATUS_ERROR,
  54. };
  55. private:
  56. Status status;
  57. // Callback fo the pp::URLLoader::Open().
  58. // Called by pp::URLLoader when response headers are received or when an
  59. // error occurs (in response to the call of pp::URLLoader::Open()).
  60. // Look at <ppapi/c/ppb_url_loader.h> and
  61. // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader.
  62. void OnOpen(int32_t result);
  63. // Callback fo the pp::URLLoader::ReadResponseBody().
  64. // |result| contains the number of bytes read or an error code.
  65. // Appends data from this->buffer_ to this->url_response_body_.
  66. void OnRead(int32_t result);
  67. // Reads the response body (asynchronously) into this->buffer_.
  68. // OnRead() will be called when bytes are received or when an error occurs.
  69. void ReadBody();
  70. // Append data bytes read from the URL onto the internal buffer. Does
  71. // nothing if |num_bytes| is 0.
  72. void AppendDataBytes(const char* buffer, int32_t num_bytes);
  73. pp::Instance* instance_; // Weak pointer.
  74. String url_; // URL to be downloaded.
  75. pp::URLRequestInfo url_request_;
  76. pp::URLLoader url_loader_; // URLLoader provides an API to download URLs.
  77. char buffer_[READ_BUFFER_SIZE]; // Temporary buffer for reads.
  78. Vector<uint8_t> data; // Contains accumulated downloaded data.
  79. pp::CompletionCallbackFactory<GetURLHandler> cc_factory_;
  80. bool complete;
  81. GetURLHandler(const GetURLHandler&);
  82. void operator=(const GetURLHandler&);
  83. public:
  84. // Creates instance of GetURLHandler on the heap.
  85. // GetURLHandler objects shall be created only on the heap (they
  86. // self-destroy when all data is in).
  87. // Initiates page (URL) download.
  88. void Start();
  89. Status get_status() const;
  90. Vector<uint8_t> get_data() const;
  91. int get_bytes_read() const;
  92. GetURLHandler(pp::Instance* instance_, const String& url);
  93. ~GetURLHandler();
  94. };
  95. #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_