Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(633)

Side by Side Diff: native_client_sdk/src/examples/api/url_loader/geturl_handler.h

Issue 14607005: [NaCl SDK] Cleanup examples. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: feedback Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 EXAMPLES_GETURL_GETURL_HANDLER_H_
6 #define EXAMPLES_GETURL_GETURL_HANDLER_H_
7
8 #include <string>
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/url_loader.h"
11 #include "ppapi/cpp/url_request_info.h"
12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/utility/completion_callback_factory.h"
14 #define READ_BUFFER_SIZE 32768
15
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
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 // GetURLHandler* handler* = GetURLHandler::Create(instance,url);
30 // handler->Start();
31 //
32 class GetURLHandler {
33 public:
34 // Creates instance of GetURLHandler on the heap.
35 // GetURLHandler objects shall be created only on the heap (they
36 // self-destroy when all data is in).
37 static GetURLHandler* Create(pp::Instance* instance_, const std::string& url);
38 // Initiates page (URL) download.
39 void Start();
40
41 private:
42 GetURLHandler(pp::Instance* instance_, const std::string& url);
43 ~GetURLHandler();
44
45 // Callback for the pp::URLLoader::Open().
46 // 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 // Look at <ppapi/c/ppb_url_loader.h> and
49 // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader.
50 void OnOpen(int32_t result);
51
52 // Callback for the pp::URLLoader::ReadResponseBody().
53 // |result| contains the number of bytes read or an error code.
54 // Appends data from this->buffer_ to this->url_response_body_.
55 void OnRead(int32_t result);
56
57 // Reads the response body (asynchronously) into this->buffer_.
58 // OnRead() will be called when bytes are received or when an error occurs.
59 void ReadBody();
60
61 // Append data bytes read from the URL onto the internal buffer. Does
62 // nothing if |num_bytes| is 0.
63 void AppendDataBytes(const char* buffer, int32_t num_bytes);
64
65 // Post a message back to the browser with the download results.
66 void ReportResult(const std::string& fname,
67 const std::string& text,
68 bool success);
69 // Post a message back to the browser with the download results and
70 // self-destroy. |this| is no longer valid when this method returns.
71 void ReportResultAndDie(const std::string& fname,
72 const std::string& text,
73 bool success);
74
75 pp::Instance* instance_; // Weak pointer.
76 std::string url_; // URL to be downloaded.
77 pp::URLRequestInfo url_request_;
78 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs.
79 char* buffer_; // Temporary buffer for reads.
80 std::string url_response_body_; // Contains accumulated downloaded data.
81 pp::CompletionCallbackFactory<GetURLHandler> cc_factory_;
82
83 GetURLHandler(const GetURLHandler&);
84 void operator=(const GetURLHandler&);
85 };
86
87 #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698