~ubuntu-branches/ubuntu/karmic/gears/karmic

« back to all changes in this revision

Viewing changes to gears/localserver/firefox/async_task_ff.h

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Lesicnik
  • Date: 2009-04-30 19:15:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090430191525-0790sb5wzg8ou0xb
Tags: upstream-0.5.21.0~svn3334+dfsg
ImportĀ upstreamĀ versionĀ 0.5.21.0~svn3334+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006, Google Inc.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without 
 
4
// modification, are permitted provided that the following conditions are met:
 
5
//
 
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.
 
14
//
 
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.
 
25
 
 
26
#ifndef GEARS_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__
 
27
#define GEARS_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__
 
28
 
 
29
#include <vector>
 
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"
 
37
 
 
38
class BlobInterface;
 
39
 
 
40
//------------------------------------------------------------------------------
 
41
// AsyncTask
 
42
//------------------------------------------------------------------------------
 
43
class AsyncTask : protected HttpRequest::HttpListener,
 
44
                  private RefCounted {
 
45
 public:
 
46
  // Starts a worker thread which will call the Run method
 
47
  bool Start();
 
48
 
 
49
  // Gracefully aborts a worker thread that was previously started
 
50
  void Abort();
 
51
 
 
52
  // Instructs the AsyncTask to delete itself when the worker thread terminates
 
53
  void DeleteWhenDone();
 
54
 
 
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
 
59
  class Listener {
 
60
   public:
 
61
    virtual void HandleAsyncTaskEvent(int msg_code,
 
62
                                      int msg_param,
 
63
                                      AsyncTask *source) = 0;
 
64
  };
 
65
  void SetListener(Listener* listener);
 
66
 
 
67
 protected:
 
68
  // Members that derived classes in common code depend on
 
69
  // TODO(michaeln): perhaps define these in a interface IAsyncTask
 
70
 
 
71
  static const char16 *kCookieRequiredErrorMessage;
 
72
 
 
73
  AsyncTask(BrowsingContext *browsing_context);
 
74
  virtual ~AsyncTask();
 
75
 
 
76
  bool Init();
 
77
  virtual void Run() = 0;
 
78
 
 
79
  // Posts a message to our listener
 
80
  void NotifyListener(int code, int param);
 
81
 
 
82
  // Synchronously fetches a url on the worker thread.  This should only
 
83
  // be called on the Run method's thread of control.
 
84
  //
 
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.
 
88
  //
 
89
  // If 'if_mode_since_date' is not null and not an empty string, the
 
90
  // request will be a conditional HTTP GET.
 
91
  //
 
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.
 
96
  //
 
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.
 
103
  //
 
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.
 
107
 
 
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,
 
113
               bool is_capturing,
 
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);
 
122
 
 
123
  // As HttpGet, but for POST.
 
124
  bool HttpPost(const char16 *full_url,
 
125
                bool is_capturing,
 
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);
 
137
 
 
138
  CriticalSection lock_;
 
139
  bool is_aborted_;
 
140
  bool is_initialized_;
 
141
  scoped_refptr<BrowsingContext> browsing_context_;
 
142
 
 
143
 private:
 
144
  bool MakeHttpRequest(const char16 *method,
 
145
                       const char16 *full_url,
 
146
                       bool is_capturing,
 
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);
 
158
 
 
159
  struct HttpRequestParameters;
 
160
 
 
161
  static const int kStartHttpGetMessageCode = -1;
 
162
  static const int kAbortHttpGetMessageCode = -2;
 
163
 
 
164
  // worker thread methods
 
165
 
 
166
  static void ThreadEntry(void *self);
 
167
  nsresult CallAsync(ThreadId thread_id, int msg_code, int msg_param);
 
168
 
 
169
  // listener and main thread methods
 
170
 
 
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);
 
176
 
 
177
  // Returns true if the currently executing thread is our listener thread
 
178
  bool IsListenerThread() {
 
179
    return listener_thread_id_ ==
 
180
        ThreadMessageQueue::GetInstance()->GetCurrentThreadId();
 
181
  }
 
182
 
 
183
  // Returns true if the currently executing thread is our task thread
 
184
  bool IsTaskThread() {
 
185
    return thread_id_ ==
 
186
        ThreadMessageQueue::GetInstance()->GetCurrentThreadId();
 
187
  }
 
188
 
 
189
  bool delete_when_done_;
 
190
  Listener *listener_;
 
191
  bool thread_running_;
 
192
  ThreadId thread_id_;
 
193
  ThreadId listener_thread_id_;
 
194
  scoped_refptr<HttpRequest> http_request_;
 
195
  HttpRequestParameters *params_;
 
196
 
 
197
  class AsyncCallEvent;
 
198
};
 
199
 
 
200
#endif  // GEARS_LOCALSERVER_FIREFOX_ASYNC_TASK_FF_H__