OLD | NEW |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client 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 EXAMPLES_GETURL_GETURL_HANDLER_H_ |
6 #define EXAMPLES_GETURL_GETURL_HANDLER_H_ | 6 #define EXAMPLES_GETURL_GETURL_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/url_loader.h" | 10 #include "ppapi/cpp/url_loader.h" |
11 #include "ppapi/cpp/url_request_info.h" | 11 #include "ppapi/cpp/url_request_info.h" |
12 #include "ppapi/cpp/instance.h" | 12 #include "ppapi/cpp/instance.h" |
13 | 13 |
14 #define READ_BUFFER_SIZE 4096 | 14 #define READ_BUFFER_SIZE 32768 |
15 | 15 |
16 // GetURLHandler is used to download data from |url|. When download is | 16 // GetURLHandler 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 |
| 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 // |
20 // EXAMPLE USAGE: | 28 // EXAMPLE USAGE: |
21 // GetURLHandler* handler* = GetURLHandler::Create(instance,url); | 29 // GetURLHandler* handler* = GetURLHandler::Create(instance,url); |
22 // handler->Start(); | 30 // handler->Start(); |
23 // | 31 // |
24 class GetURLHandler { | 32 class GetURLHandler { |
25 public: | 33 public: |
26 // Creates instance of GetURLHandler on the heap. | 34 // Creates instance of GetURLHandler on the heap. |
27 // GetURLHandler objects shall be created only on the heap (they | 35 // GetURLHandler objects shall be created only on the heap (they |
28 // self-destroy when all data is in). | 36 // self-destroy when all data is in). |
29 static GetURLHandler* Create(pp::Instance* instance_, | 37 static GetURLHandler* Create(pp::Instance* instance_, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 // Post a message back to the browser with the download results and | 70 // Post a message back to the browser with the download results and |
63 // self-destroy. |this| is no longer valid when this method returns. | 71 // self-destroy. |this| is no longer valid when this method returns. |
64 void ReportResultAndDie(const std::string& fname, | 72 void ReportResultAndDie(const std::string& fname, |
65 const std::string& text, | 73 const std::string& text, |
66 bool success); | 74 bool success); |
67 | 75 |
68 pp::Instance* instance_; // Weak pointer. | 76 pp::Instance* instance_; // Weak pointer. |
69 std::string url_; // URL to be downloaded. | 77 std::string url_; // URL to be downloaded. |
70 pp::URLRequestInfo url_request_; | 78 pp::URLRequestInfo url_request_; |
71 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs. | 79 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs. |
72 char buffer_[READ_BUFFER_SIZE]; // Temporary buffer for reads. | 80 char* buffer_; // Temporary buffer for reads. |
73 std::string url_response_body_; // Contains accumulated downloaded data. | 81 std::string url_response_body_; // Contains accumulated downloaded data. |
74 pp::CompletionCallbackFactory<GetURLHandler> cc_factory_; | 82 pp::CompletionCallbackFactory<GetURLHandler> cc_factory_; |
75 | 83 |
76 GetURLHandler(const GetURLHandler&); | 84 GetURLHandler(const GetURLHandler&); |
77 void operator=(const GetURLHandler&); | 85 void operator=(const GetURLHandler&); |
78 }; | 86 }; |
79 | 87 |
80 #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_ | 88 #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_ |
81 | 89 |
OLD | NEW |