OSDN Git Service

jQueryのアップデートに伴う変更。
[nacltest/xmplayer.git] / geturl_handler.h
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.
4
5 #ifndef EXAMPLES_GETURL_GETURL_HANDLER_H_
6 #define EXAMPLES_GETURL_GETURL_HANDLER_H_
7
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>
13
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
17 // self-destroys.
18 //
19 // EXAMPLE USAGE:
20 // GetURLHandler* handler* = GetURLHandler::Create(instance,url);
21 // if (!handler->Start())
22 //   delete handler;
23 //
24 //
25 class GetURLHandler {
26  public:
27
28    enum  status_id {
29       open,
30       loading,
31       complete,
32       end,
33        init
34    };
35
36    typedef boost::function<void (status_id,bool,GetURLHandler&) > complete_func_type;
37    static void null_func(status_id,bool,GetURLHandler&) { } ;
38
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.
46   bool Start();
47
48   int32_t GetReceivedBytes() const
49   {
50     return (int32_t)received_bytes_;
51   }
52
53   int32_t GetTotalBytes() const 
54   {
55     return (int32_t)total_bytes_;
56   }
57
58   ~GetURLHandler();
59   status_id GetStatus() const { return status_;};
60 private:
61   static const int kBufferSize = 4096;
62
63   GetURLHandler(pp::Instance* instance_, const std::string& url,std::vector<char>& result,complete_func_type func = complete_func_type(&null_func));
64
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);
71
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);
76
77   // Reads the response body (asynchronously) into this->buffer_.
78   // OnRead() will be called when bytes are received or when an error occurs.
79   void ReadBody();
80
81   void CallbackEventJS(status_id id,bool status,const pp::Var& reason = std::string(""));
82
83   //// Calls JS reportResult() defined in geturl.html
84   //void ReportResult(const std::string& fname,
85   //                  const std::string& text,
86   //                  bool success);
87   //// Calls JS reportResult() and self-destroy (aka delete this;).
88   //void ReportResultAndDie(const std::string& fname,
89   //                        const std::string& text,
90   //                        bool success);
91
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_;
99   uint32_t result_;
100   std::string result_string_;
101   bool load_success_;
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_;
107   status_id status_;
108 };
109
110 #endif  // EXAMPLES_GETURL_GETURL_HANDLER_H_
111