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 3 // found in the LICENSE file. 4 5 #ifndef URL_LOADER_HANDLER_H_ 6 #define URL_LOADER_HANDLER_H_ 7 8 #include <string> 9 #include "ppapi/cpp/completion_callback.h" 10 #include "ppapi/cpp/instance.h" 11 #include "ppapi/cpp/url_loader.h" 12 #include "ppapi/cpp/url_request_info.h" 13 #include "ppapi/utility/completion_callback_factory.h" 14 #define READ_BUFFER_SIZE 32768 15 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 18 // with the results encoded in the message as a string and self-destroys. 19 // 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 22 // much of a performance improvement, but it saves some memory since 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 25 // implementation.) Other performance improvements made as outlined in this 26 // bug: http://code.google.com/p/chromium/issues/detail?id=103947 27 // 28 // EXAMPLE USAGE: 29 // URLLoaderHandler* handler* = URLLoaderHandler::Create(instance,url); 30 // handler->Start(); 31 // 32 class URLLoaderHandler { 33 public: 34 // Creates instance of URLLoaderHandler on the heap. 35 // URLLoaderHandler objects shall be created only on the heap (they 36 // self-destroy when all data is in). 37 static URLLoaderHandler* Create(pp::Instance* instance_, 38 const std::string& url); 39 // Initiates page (URL) download. 40 void Start(); 41 42 private: 43 URLLoaderHandler(pp::Instance* instance_, const std::string& url); 44 ~URLLoaderHandler(); 45 46 // Callback for the pp::URLLoader::Open(). 47 // Called by pp::URLLoader when response headers are received or when an 48 // error occurs (in response to the call of pp::URLLoader::Open()). 49 // Look at <ppapi/c/ppb_url_loader.h> and 50 // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader. 51 void OnOpen(int32_t result); 52 53 // Callback for the pp::URLLoader::ReadResponseBody(). 54 // |result| contains the number of bytes read or an error code. 55 // Appends data from this->buffer_ to this->url_response_body_. 56 void OnRead(int32_t result); 57 58 // Reads the response body (asynchronously) into this->buffer_. 59 // OnRead() will be called when bytes are received or when an error occurs. 60 void ReadBody(); 61 62 // Append data bytes read from the URL onto the internal buffer. Does 63 // nothing if |num_bytes| is 0. 64 void AppendDataBytes(const char* buffer, int32_t num_bytes); 65 66 // Post a message back to the browser with the download results. 67 void ReportResult(const std::string& fname, 68 const std::string& text, 69 bool success); 70 // Post a message back to the browser with the download results and 71 // self-destroy. |this| is no longer valid when this method returns. 72 void ReportResultAndDie(const std::string& fname, 73 const std::string& text, 74 bool success); 75 76 pp::Instance* instance_; // Weak pointer. 77 std::string url_; // URL to be downloaded. 78 pp::URLRequestInfo url_request_; 79 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs. 80 char* buffer_; // Temporary buffer for reads. 81 std::string url_response_body_; // Contains accumulated downloaded data. 82 pp::CompletionCallbackFactory<URLLoaderHandler> cc_factory_; 83 84 URLLoaderHandler(const URLLoaderHandler&); 85 void operator=(const URLLoaderHandler&); 86 }; 87 88 #endif // URL_LOADER_HANDLER_H_ 89