1 // Copyright 2006, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_HTTPREQUEST_IE_HTTPREQUEST_IE_H__
27 #define GEARS_HTTPREQUEST_IE_HTTPREQUEST_IE_H__
29 #include "gears/base/common/base_class.h"
30 #include "gears/base/common/common.h"
31 #include "gears/base/common/js_runner.h"
32 #include "gears/localserver/common/http_request.h"
33 #include "gears/third_party/scoped_ptr/scoped_ptr.h"
34 #include "ie/genfiles/interfaces.h" // from OUTDIR
36 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 class ATL_NO_VTABLE GearsHttpRequest
40 : public ModuleImplBaseClass
,
41 public CComObjectRootEx
<CComMultiThreadModel
>,
42 public CComCoClass
<GearsHttpRequest
>,
43 public IDispatchImpl
<GearsHttpRequestInterface
>,
44 public HttpRequest::ReadyStateListener
,
45 public JsEventHandlerInterface
{
47 BEGIN_COM_MAP(GearsHttpRequest
)
48 COM_INTERFACE_ENTRY(GearsHttpRequestInterface
)
49 COM_INTERFACE_ENTRY(IDispatch
)
52 DECLARE_NOT_AGGREGATABLE(GearsHttpRequest
)
53 DECLARE_PROTECT_FINAL_CONSTRUCT()
54 // End boilerplate code. Begin interface.
56 // need a default constructor to CreateInstance objects in IE
58 virtual ~GearsHttpRequest();
60 // GearsHttpRequestInterface
61 // This is the interface we expose to JavaScript.
62 // Note: lifted from the midl generated .h file
64 virtual /* [propput] */ HRESULT STDMETHODCALLTYPE
put_onreadystatechange(
65 /* [in] */ VARIANT
*handler
);
67 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_onreadystatechange(
68 /* [retval][out] */ VARIANT
*handler
);
70 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_readyState(
71 /* [retval][out] */ int *state
);
73 virtual HRESULT STDMETHODCALLTYPE
open(
74 /* [in] */ const BSTR method
,
75 /* [in] */ const BSTR url
,
76 /* [optional][in] */ const VARIANT
*async
);
78 virtual HRESULT STDMETHODCALLTYPE
setRequestHeader(
79 /* [in] */ const BSTR header
,
80 /* [in] */ const BSTR value
);
82 virtual HRESULT STDMETHODCALLTYPE
send(
83 /* [optional][in] */ const VARIANT
*data
);
85 virtual HRESULT STDMETHODCALLTYPE
abort(void);
87 virtual HRESULT STDMETHODCALLTYPE
getAllResponseHeaders(
88 /* [retval][out] */ BSTR
*headers
);
90 virtual HRESULT STDMETHODCALLTYPE
getResponseHeader(
91 /* [in] */ const BSTR headerName
,
92 /* [retval][out] */ BSTR
*headerValues
);
94 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_responseText(
95 /* [retval][out] */ BSTR
*body
);
98 // Blob support is not ready for prime time yet
100 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_responseBlob(
101 /* [retval][out] */ IUnknown
**blob
);
104 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_status(
105 /* [retval][out] */ int *statusCode
);
107 virtual /* [propget] */ HRESULT STDMETHODCALLTYPE
get_statusText(
108 /* [retval][out] */ BSTR
*statusText
);
111 CComPtr
<IDispatch
> onreadystatechangehandler_
;
112 HttpRequest
*request_
;
114 // Null if response_text_ has not yet been cached
115 scoped_ptr
<std::string16
> response_text_
;
117 // Valid only after a successful get_responseBlob()
118 CComPtr
<GearsBlobInterface
> response_blob_
;
120 bool content_type_header_was_set_
;
121 bool has_fired_completion_event_
;
122 scoped_ptr
<JsEventMonitor
> unload_monitor_
;
124 void CreateRequest();
125 void ReleaseRequest();
127 HttpRequest::ReadyState
GetState();
128 bool IsUninitialized() { return GetState() == HttpRequest::UNINITIALIZED
; }
129 bool IsOpen() { return GetState() == HttpRequest::OPEN
; }
130 bool IsSent() { return GetState() == HttpRequest::SENT
; }
131 bool IsInteractive() { return GetState() == HttpRequest::INTERACTIVE
; }
132 bool IsComplete() { return GetState() == HttpRequest::COMPLETE
; }
133 bool IsValidResponse();
135 bool ResolveUrl(const char16
*url
, std::string16
*resolved_url
,
136 std::string16
*exception_message
);
138 // HttpRequest::ReadyStateListener impl
139 virtual void DataAvailable(HttpRequest
*source
);
140 virtual void ReadyStateChanged(HttpRequest
*source
);
142 // JsEventHandlerInterface impl
143 virtual void HandleEvent(JsEventType event_type
);
145 DISALLOW_EVIL_CONSTRUCTORS(GearsHttpRequest
);
149 #endif // GEARS_HTTPREQUEST_IE_HTTPREQUEST_IE_H__