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_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__
27
#define GEARS_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__
30
#include "gears/base/common/async_router.h"
31
#include "gears/base/common/browsing_context.h"
32
#include "gears/base/common/common.h"
33
#include "gears/base/common/scoped_refptr.h"
34
#include "gears/localserver/common/critical_section.h"
35
#include "gears/localserver/common/http_request.h"
36
#include "gears/localserver/common/localserver_db.h"
40
//------------------------------------------------------------------------------
42
//------------------------------------------------------------------------------
43
class AsyncTask : protected HttpRequest::HttpListener,
46
// Starts a worker thread which will call the Run method
49
// Gracefully aborts a worker thread that was previously started
52
// Instructs the AsyncTask to delete itself when the worker thread terminates
53
void DeleteWhenDone();
55
// Firefox specific API
56
// Sets where notification messages will be sent. Notifications will be
57
// delivered on thread of control that the AsyncTask is initialized on.
58
// Firefox specific API utilized by clients of derived classes
61
virtual void HandleAsyncTaskEvent(int msg_code,
63
AsyncTask *source) = 0;
65
void SetListener(Listener* listener);
68
// Members that derived classes in common code depend on
69
// TODO(michaeln): perhaps define these in a interface IAsyncTask
71
static const char16 *kCookieRequiredErrorMessage;
73
AsyncTask(BrowsingContext *browsing_context);
77
virtual void Run() = 0;
79
// Posts a message to our listener
80
void NotifyListener(int code, int param);
82
// Synchronously fetches a url on the worker thread. This should only
83
// be called on the Run method's thread of control.
85
// If 'is_capturing' is true, the custom "X-Gears-Google" header is
86
// added, and redirects will not be followed, rather the 30x response
87
// will be directly returned.
89
// If 'if_mode_since_date' is not null and not an empty string, the
90
// request will be a conditional HTTP GET.
92
// If 'required_cookie' is not null or empty, the presense of the
93
// cookie is tested prior to issueing the request. If the test fails
94
// the return value is false and 'error_message' will contain
95
// kCookieRequiredErrorMessage.
97
// Upon successful return, the 'payload' structure will contain the
98
// response. If redirection was followed, 'was_redirected' will be set
99
// to true, 'full_redirect_url' will contain the final location in
100
// the chain of redirects, and the 'payload' will contain the response
101
// for the final location. The 'was_redirected', 'full_redirect_url',
102
// and 'error_message' parameters may be null.
104
// If Abort() is called from another thread of control while a request
105
// is pending, the request is cancelled and HttpGet will return
106
// shortly thereafter.
108
// 'payload.data' will not be initialized upon return. Instead, the
109
// response body will be contained by the 'payload_data' Blob.
110
// TODO(andreip): remove payload_data argument once
111
// WebCacheDB::PayloadInfo.data is a Blob.
112
bool HttpGet(const char16 *full_url,
114
const char16 *reason_header_value,
115
const char16 *if_mod_since_date,
116
const char16 *required_cookie,
117
WebCacheDB::PayloadInfo *payload,
118
scoped_refptr<BlobInterface> *payload_data,
119
bool *was_redirected,
120
std::string16 *full_redirect_url,
121
std::string16 *error_message);
123
// As HttpGet, but for POST.
124
bool HttpPost(const char16 *full_url,
126
const char16 *reason_header_value,
127
const char16 *content_type_header_value,
128
const char16 *if_mod_since_date,
129
const char16 *required_cookie,
130
bool disable_browser_cookies,
131
BlobInterface *post_body,
132
WebCacheDB::PayloadInfo *payload,
133
scoped_refptr<BlobInterface> *payload_data,
134
bool *was_redirected,
135
std::string16 *full_redirect_url,
136
std::string16 *error_message);
138
CriticalSection lock_;
140
bool is_initialized_;
141
scoped_refptr<BrowsingContext> browsing_context_;
144
bool MakeHttpRequest(const char16 *method,
145
const char16 *full_url,
147
const char16 *reason_header_value,
148
const char16 *content_type_header_value,
149
const char16 *if_mod_since_date,
150
const char16 *required_cookie,
151
bool disable_browser_cookies,
152
BlobInterface *post_body,
153
WebCacheDB::PayloadInfo *payload,
154
scoped_refptr<BlobInterface> *payload_data,
155
bool *was_redirected,
156
std::string16 *full_redirect_url,
157
std::string16 *error_message);
159
struct HttpRequestParameters;
161
static const int kStartHttpGetMessageCode = -1;
162
static const int kAbortHttpGetMessageCode = -2;
164
// worker thread methods
166
static void ThreadEntry(void *self);
167
nsresult CallAsync(ThreadId thread_id, int msg_code, int msg_param);
169
// listener and main thread methods
171
void OnAsyncCall(int msg_code, int msg_param);
172
void OnAbortHttpGet();
173
bool OnStartHttpGet();
174
void OnListenerEvent(int msg_code, int msg_param);
175
void ReadyStateChanged(HttpRequest *source);
177
// Returns true if the currently executing thread is our listener thread
178
bool IsListenerThread() {
179
return listener_thread_id_ ==
180
ThreadMessageQueue::GetInstance()->GetCurrentThreadId();
183
// Returns true if the currently executing thread is our task thread
184
bool IsTaskThread() {
186
ThreadMessageQueue::GetInstance()->GetCurrentThreadId();
189
bool delete_when_done_;
191
bool thread_running_;
193
ThreadId listener_thread_id_;
194
scoped_refptr<HttpRequest> http_request_;
195
HttpRequestParameters *params_;
197
class AsyncCallEvent;
200
#endif // GEARS_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__