| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXAMPLES_GETURL_GETURL_HANDLER_H_ | 5 #ifndef URL_LOADER_HANDLER_H_ |
| 6 #define EXAMPLES_GETURL_GETURL_HANDLER_H_ | 6 #define URL_LOADER_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include "ppapi/cpp/completion_callback.h" | 9 #include "ppapi/cpp/completion_callback.h" |
| 10 #include "ppapi/cpp/instance.h" |
| 10 #include "ppapi/cpp/url_loader.h" | 11 #include "ppapi/cpp/url_loader.h" |
| 11 #include "ppapi/cpp/url_request_info.h" | 12 #include "ppapi/cpp/url_request_info.h" |
| 12 #include "ppapi/cpp/instance.h" | |
| 13 #include "ppapi/utility/completion_callback_factory.h" | 13 #include "ppapi/utility/completion_callback_factory.h" |
| 14 #define READ_BUFFER_SIZE 32768 | 14 #define READ_BUFFER_SIZE 32768 |
| 15 | 15 |
| 16 // GetURLHandler is used to download data from |url|. When download is | 16 // URLLoaderHandler is used to download data from |url|. When download is |
| 17 // finished or when an error occurs, it posts a message back to the browser | 17 // finished or when an error occurs, it posts a message back to the browser |
| 18 // with the results encoded in the message as a string and self-destroys. | 18 // with the results encoded in the message as a string and self-destroys. |
| 19 // | 19 // |
| 20 // pp::URLLoader.GetDownloadProgress() is used to to allocate the memory | 20 // pp::URLLoader.GetDownloadProgress() is used to to allocate the memory |
| 21 // required for url_response_body_ before the download starts. (This is not so | 21 // required for url_response_body_ before the download starts. (This is not so |
| 22 // much of a performance improvement, but it saves some memory since | 22 // much of a performance improvement, but it saves some memory since |
| 23 // std::string.insert() typically grows the string's capacity by somewhere | 23 // std::string.insert() typically grows the string's capacity by somewhere |
| 24 // between 50% to 100% when it needs more memory, depending on the | 24 // between 50% to 100% when it needs more memory, depending on the |
| 25 // implementation.) Other performance improvements made as outlined in this | 25 // implementation.) Other performance improvements made as outlined in this |
| 26 // bug: http://code.google.com/p/chromium/issues/detail?id=103947 | 26 // bug: http://code.google.com/p/chromium/issues/detail?id=103947 |
| 27 // | 27 // |
| 28 // EXAMPLE USAGE: | 28 // EXAMPLE USAGE: |
| 29 // GetURLHandler* handler* = GetURLHandler::Create(instance,url); | 29 // URLLoaderHandler* handler* = URLLoaderHandler::Create(instance,url); |
| 30 // handler->Start(); | 30 // handler->Start(); |
| 31 // | 31 // |
| 32 class GetURLHandler { | 32 class URLLoaderHandler { |
| 33 public: | 33 public: |
| 34 // Creates instance of GetURLHandler on the heap. | 34 // Creates instance of URLLoaderHandler on the heap. |
| 35 // GetURLHandler objects shall be created only on the heap (they | 35 // URLLoaderHandler objects shall be created only on the heap (they |
| 36 // self-destroy when all data is in). | 36 // self-destroy when all data is in). |
| 37 static GetURLHandler* Create(pp::Instance* instance_, const std::string& url); | 37 static URLLoaderHandler* Create(pp::Instance* instance_, |
| 38 const std::string& url); |
| 38 // Initiates page (URL) download. | 39 // Initiates page (URL) download. |
| 39 void Start(); | 40 void Start(); |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 GetURLHandler(pp::Instance* instance_, const std::string& url); | 43 URLLoaderHandler(pp::Instance* instance_, const std::string& url); |
| 43 ~GetURLHandler(); | 44 ~URLLoaderHandler(); |
| 44 | 45 |
| 45 // Callback for the pp::URLLoader::Open(). | 46 // Callback for the pp::URLLoader::Open(). |
| 46 // Called by pp::URLLoader when response headers are received or when an | 47 // Called by pp::URLLoader when response headers are received or when an |
| 47 // error occurs (in response to the call of pp::URLLoader::Open()). | 48 // error occurs (in response to the call of pp::URLLoader::Open()). |
| 48 // Look at <ppapi/c/ppb_url_loader.h> and | 49 // Look at <ppapi/c/ppb_url_loader.h> and |
| 49 // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader. | 50 // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader. |
| 50 void OnOpen(int32_t result); | 51 void OnOpen(int32_t result); |
| 51 | 52 |
| 52 // Callback for the pp::URLLoader::ReadResponseBody(). | 53 // Callback for the pp::URLLoader::ReadResponseBody(). |
| 53 // |result| contains the number of bytes read or an error code. | 54 // |result| contains the number of bytes read or an error code. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 71 void ReportResultAndDie(const std::string& fname, | 72 void ReportResultAndDie(const std::string& fname, |
| 72 const std::string& text, | 73 const std::string& text, |
| 73 bool success); | 74 bool success); |
| 74 | 75 |
| 75 pp::Instance* instance_; // Weak pointer. | 76 pp::Instance* instance_; // Weak pointer. |
| 76 std::string url_; // URL to be downloaded. | 77 std::string url_; // URL to be downloaded. |
| 77 pp::URLRequestInfo url_request_; | 78 pp::URLRequestInfo url_request_; |
| 78 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs. | 79 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs. |
| 79 char* buffer_; // Temporary buffer for reads. | 80 char* buffer_; // Temporary buffer for reads. |
| 80 std::string url_response_body_; // Contains accumulated downloaded data. | 81 std::string url_response_body_; // Contains accumulated downloaded data. |
| 81 pp::CompletionCallbackFactory<GetURLHandler> cc_factory_; | 82 pp::CompletionCallbackFactory<URLLoaderHandler> cc_factory_; |
| 82 | 83 |
| 83 GetURLHandler(const GetURLHandler&); | 84 URLLoaderHandler(const URLLoaderHandler&); |
| 84 void operator=(const GetURLHandler&); | 85 void operator=(const URLLoaderHandler&); |
| 85 }; | 86 }; |
| 86 | 87 |
| 87 #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_ | 88 #endif // URL_LOADER_HANDLER_H_ |
| OLD | NEW |