1 // Copyright 2010 The Native Client SDK Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can
3 // be found in the LICENSE file.
5 #ifndef EXAMPLES_GETURL_GETURL_HANDLER_H_
6 #define EXAMPLES_GETURL_GETURL_HANDLER_H_
8 #include <ppapi/cpp/completion_callback.h>
9 #include <ppapi/cpp/url_loader.h>
10 #include <ppapi/cpp/url_request_info.h>
11 #include <ppapi/cpp/instance.h>
12 #include <boost/function.hpp>
14 // GetURLHandler is used to download data from |url|. When download is
15 // finished or when an error occurs, it calls JavaScript function
16 // reportResult(url, result, success) (defined in geturl.html) and
20 // GetURLHandler* handler* = GetURLHandler::Create(instance,url);
21 // if (!handler->Start())
36 typedef boost::function<void (status_id,bool,GetURLHandler&) > complete_func_type;
37 static void null_func(status_id,bool,GetURLHandler&) { } ;
39 // Creates instance of GetURLHandler on the heap.
40 // GetURLHandler objects shall be created only on the heap (they
41 // self-destroy when all data is in).
42 static GetURLHandler* Create(pp::Instance* instance_,
43 const std::string& url,std::vector<char>& result,complete_func_type func = complete_func_type(&null_func));
44 // Initiates page (URL) download.
45 // Returns false in case of internal error, and self-destroys.
48 int32_t GetReceivedBytes() const
50 return (int32_t)received_bytes_;
53 int32_t GetTotalBytes() const
55 return (int32_t)total_bytes_;
59 status_id GetStatus() const { return status_;};
61 static const int kBufferSize = 4096;
63 GetURLHandler(pp::Instance* instance_, const std::string& url,std::vector<char>& result,complete_func_type func = complete_func_type(&null_func));
65 // Callback fo the pp::URLLoader::Open().
66 // Called by pp::URLLoader when response headers are received or when an
67 // error occurs (in response to the call of pp::URLLoader::Open()).
68 // Look at <ppapi/c/ppb_url_loader.h> and
69 // <ppapi/cpp/url_loader.h> for more information about pp::URLLoader.
70 void OnOpen(int32_t result);
72 // Callback fo the pp::URLLoader::ReadResponseBody().
73 // |result| contains the number of bytes read or an error code.
74 // Appends data from this->buffer_ to this->url_response_body_.
75 void OnRead(int32_t result);
77 // Reads the response body (asynchronously) into this->buffer_.
78 // OnRead() will be called when bytes are received or when an error occurs.
81 void CallbackEventJS(status_id id,bool status,const pp::Var& reason = std::string(""));
83 //// Calls JS reportResult() defined in geturl.html
84 //void ReportResult(const std::string& fname,
85 // const std::string& text,
87 //// Calls JS reportResult() and self-destroy (aka delete this;).
88 //void ReportResultAndDie(const std::string& fname,
89 // const std::string& text,
92 pp::Instance* instance_;
93 std::string url_; // URL to be downloaded.
94 pp::URLRequestInfo url_request_;
95 pp::URLLoader url_loader_; // URLLoader provides an API to download URLs.
96 char buffer_[kBufferSize]; // buffer for pp::URLLoader::ReadResponseBody().
97 std::vector<char>& url_response_body_; // Contains downloaded data.
98 pp::CompletionCallbackFactory<GetURLHandler> cc_factory_;
100 std::string result_string_;
102 GetURLHandler(const GetURLHandler&);
103 void operator=(const GetURLHandler&);
104 static std::string js_empty_;
105 int64_t received_bytes_,total_bytes_;
106 complete_func_type func_;
110 #endif // EXAMPLES_GETURL_GETURL_HANDLER_H_