What's new? | Help | Directory | Sign in
Google
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of Google Inc. nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#import <assert.h>

#import "gears/blob/blob_interface.h"
#import "gears/localserver/common/http_constants.h"
#import "gears/localserver/common/http_cookies.h"
#import "gears/localserver/safari/async_task_sf.h"

const char16 *AsyncTask::kCookieRequiredErrorMessage =
STRING16(L"Required cookie is not present");

//------------------------------------------------------------------------------
// AsyncTask
//------------------------------------------------------------------------------
AsyncTask::AsyncTask(BrowsingContext *browsing_context) :
is_aborted_(false),
is_initialized_(false),
browsing_context_(browsing_context),
is_destructing_(false),
delete_when_done_(false),
thread_(NULL),
listener_(NULL),
msg_port_(NULL) {
}

//------------------------------------------------------------------------------
// ~AsyncTask
//------------------------------------------------------------------------------
AsyncTask::~AsyncTask() {
if (thread_) {
LOG(("~AsyncTask - thread is still running: %p\n", this));
// extra scope to lock our monitor
{
CritSecLock locker(lock_);
is_destructing_ = true;
locker.Unlock();
Abort();
}
pthread_join(thread_, NULL);
thread_ = NULL;
}
}

//------------------------------------------------------------------------------
// Init
//------------------------------------------------------------------------------
bool AsyncTask::Init() {
if (is_initialized_) {
assert(!is_initialized_);
return false;
}

is_aborted_ = false;
is_initialized_ = true;

// Create a mach port to communicate between the threads
CFMachPortContext context;
context.version = 0;
context.info = this;
context.retain = NULL;
context.release = NULL;
context.copyDescription = NULL;
Boolean should_free = false; // Don't free context.info on dealloc
msg_port_.reset(CFMachPortCreate(kCFAllocatorDefault, OnAsyncCall,
&context, &should_free));

if (!msg_port_.get() || should_free) {
LOG(("Couldn't make mach port\n"));
return false;
}

// Create a RL source and add it to the current runloop. This is so that
// the thread can send a message to the main thread via OnAsyncCall()
scoped_CFRunLoopSource
msg_source(CFMachPortCreateRunLoopSource(kCFAllocatorDefault,
msg_port_.get(), 0));
CFRunLoopAddSource(CFRunLoopGetCurrent(), msg_source.get(),
kCFRunLoopCommonModes);

return true;
}

//------------------------------------------------------------------------------
// SetListener
//------------------------------------------------------------------------------
void AsyncTask::SetListener(Listener *listener) {
listener_ = listener;
}

//------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------
bool AsyncTask::Start() {
if (!is_initialized_) {
assert(is_initialized_);
return false;
}

is_aborted_ = false;
if (pthread_create(&thread_, NULL, ThreadEntry, this))
return false;

return (thread_ != NULL);
}

//------------------------------------------------------------------------------
// Abort
//------------------------------------------------------------------------------
void AsyncTask::Abort() {
CritSecLock locker(lock_);
if (thread_ && !is_aborted_) {
is_aborted_ = true;
}
}

//------------------------------------------------------------------------------
// DeleteWhenDone
//------------------------------------------------------------------------------
void AsyncTask::DeleteWhenDone() {
CritSecLock locker(lock_);
assert(!delete_when_done_);
if (!delete_when_done_) {
if (!thread_) {
// In this particular code path, we have to call unlock prior to delete
// otherwise the locker would try to access deleted memory, &lock_,
// after it's been freed.
locker.Unlock();
delete this;
} else {
delete_when_done_ = true;
}
}
}

//------------------------------------------------------------------------------
// ThreadEntry - Our worker thread's entry procedure
//------------------------------------------------------------------------------
void *AsyncTask::ThreadEntry(void *task) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

AsyncTask *asyncTask = reinterpret_cast<AsyncTask*>(task);
asyncTask->Run();
asyncTask->NotifyListener(kThreadDoneMessageCode, 0);

[pool release];

return NULL;
}

//------------------------------------------------------------------------------
// NotifyListener
//------------------------------------------------------------------------------
void AsyncTask::NotifyListener(int code, int param) {
ThreadMessage msg = {0};

// Post a Mach message to be recieved by the main thread via OnAsyncCall()
msg.header.msgh_size = sizeof(ThreadMessage);
msg.header.msgh_remote_port = CFMachPortGetPort(msg_port_.get());
msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND,
MACH_MSG_TYPE_MAKE_SEND_ONCE);
msg.code = code;
msg.param = param;
mach_msg(&(msg.header),
MACH_SEND_MSG | MACH_SEND_TIMEOUT,
msg.header.msgh_size, 0, 0, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
}

//------------------------------------------------------------------------------
// OnThreadDone
//------------------------------------------------------------------------------
void AsyncTask::OnThreadDone() {
CritSecLock locker(lock_);
if (delete_when_done_) {
thread_ = NULL;
// In this particular code path, we have to call unlock prior to delete
// otherwise the locker would try to access deleted memory, &lock_,
// after it's been freed.
locker.Unlock();
delete this;
}
}

//------------------------------------------------------------------------------
// HttpGet
//------------------------------------------------------------------------------
bool AsyncTask::HttpGet(const char16 *full_url,
bool is_capturing,
const char16 *reason_header_value,
const char16 *if_mod_since_date,
const char16 *required_cookie,
WebCacheDB::PayloadInfo *payload,
scoped_refptr<BlobInterface> *payload_data,
bool *was_redirected,
std::string16 *full_redirect_url,
std::string16 *error_message) {
return MakeHttpRequest(HttpConstants::kHttpGET,
full_url,
is_capturing,
reason_header_value,
NULL, // content_type_header_value
if_mod_since_date,
required_cookie,
false,
NULL,
payload,
payload_data,
was_redirected,
full_redirect_url,
error_message);
}

//------------------------------------------------------------------------------
// HttpPost
//------------------------------------------------------------------------------
bool AsyncTask::HttpPost(const char16 *full_url,
bool is_capturing,
const char16 *reason_header_value,
const char16 *content_type_header_value,
const char16 *if_mod_since_date,
const char16 *required_cookie,
bool disable_browser_cookies,
BlobInterface *post_body,
WebCacheDB::PayloadInfo *payload,
scoped_refptr<BlobInterface> *payload_data,
bool *was_redirected,
std::string16 *full_redirect_url,
std::string16 *error_message) {
return MakeHttpRequest(HttpConstants::kHttpPOST,
full_url,
is_capturing,
reason_header_value,
content_type_header_value,
if_mod_since_date,
required_cookie,
disable_browser_cookies,
post_body,
payload,
payload_data,
was_redirected,
full_redirect_url,
error_message);
}

//------------------------------------------------------------------------------
// MakeHttpRequest
//------------------------------------------------------------------------------
bool AsyncTask::MakeHttpRequest(const char16 *method,
const char16 *full_url,
bool is_capturing,
const char16 *reason_header_value,
const char16 *content_type_header_value,
const char16 *if_mod_since_date,
const char16 *required_cookie,
bool disable_browser_cookies,
BlobInterface *post_body,
WebCacheDB::PayloadInfo *payload,
scoped_refptr<BlobInterface> *payload_data,
bool *was_redirected,
std::string16 *full_redirect_url,
std::string16 *error_message) {
assert(payload);
if (was_redirected) {
*was_redirected = false;
}
if (full_redirect_url) {
full_redirect_url->clear();
}
if (error_message) {
error_message->clear();
}

if (required_cookie && required_cookie[0]) {
std::string16 required_cookie_str(required_cookie);
std::string16 name, value;
ParseCookieNameAndValue(required_cookie_str, &name, &value);
if (value != kNegatedRequiredCookieValue) {
CookieMap cookie_map;
cookie_map.LoadMapForUrl(full_url, browsing_context_.get());
if (!cookie_map.HasLocalServerRequiredCookie(required_cookie_str)) {
if (error_message) {
*(error_message) = kCookieRequiredErrorMessage;
}
return false;
}
}
}

scoped_refptr<HttpRequest> http_request;
if (!HttpRequest::Create(&http_request))
return false;

http_request->SetCachingBehavior(HttpRequest::BYPASS_ALL_CACHES);

if (!http_request->Open(method, full_url, true, browsing_context_.get())) {
return false;
}

if (is_capturing) {
http_request->SetRedirectBehavior(HttpRequest::FOLLOW_NONE);
if (!http_request->SetRequestHeader(HttpConstants::kXGoogleGearsHeader,
STRING16(L"1"))) {
return false;
}
}

if (!http_request->SetRequestHeader(HttpConstants::kCacheControlHeader,
HttpConstants::kNoCache)) {
return false;
}

if (!http_request->SetRequestHeader(HttpConstants::kPragmaHeader,
HttpConstants::kNoCache)) {
return false;
}

if (reason_header_value && reason_header_value[0]) {
if (!http_request->SetRequestHeader(HttpConstants::kXGearsReasonHeader,
reason_header_value)) {
return false;
}
}

if (content_type_header_value && content_type_header_value[0]) {
if (!http_request->SetRequestHeader(HttpConstants::kContentTypeHeader,
content_type_header_value)) {
return false;
}
}

if (if_mod_since_date && if_mod_since_date[0]) {
if (!http_request->SetRequestHeader(HttpConstants::kIfModifiedSinceHeader,
if_mod_since_date)) {
return false;
}
}

if (disable_browser_cookies) {
if (!http_request->SetCookieBehavior(
HttpRequest::DO_NOT_SEND_BROWSER_COOKIES)) {
return false;
}
}

payload->data.reset();

// Rely on logic inside HttpRequest to check for valid combinations of
// method and presence of body.
bool result = http_request->Send(post_body);
if (!result) {
return false;
}

// Wait for the data to come back from the HTTP request
while (1) {
HttpRequest::ReadyState ready_state;
http_request->GetReadyState(&ready_state);

if (ready_state == HttpRequest::COMPLETE) {
break;
}

if (is_aborted_) {
http_request->Abort();
break;
}

// Run for a second in the run loop
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1, true);
}

// Extract the status & data
int status;
if (http_request->GetStatus(&status)) {
payload->status_code = status;
if (http_request->GetStatusLine(&payload->status_line)) {
if (http_request->GetAllResponseHeaders(&payload->headers)) {
// TODO(bgarcia): Make WebCacheDB::PayloadInfo.data a Blob.
http_request->GetResponseBody(payload_data);
}
}
}

// If we were redirected during the load, setup the return variables
if (http_request->WasRedirected()) {
if (was_redirected)
*was_redirected = true;

if (full_redirect_url)
http_request->GetFinalUrl(full_redirect_url);
}

return !is_aborted_ && payload_data->get();
}

//------------------------------------------------------------------------------
// static OnAsyncCall
//------------------------------------------------------------------------------
void AsyncTask::OnAsyncCall(CFMachPortRef port, void *mach_msg, CFIndex size,
void *info) {
ThreadMessage *msg = (ThreadMessage *)mach_msg;
AsyncTask *asyncTask = (AsyncTask *)info;

if (msg->code == kThreadDoneMessageCode) {
asyncTask->OnThreadDone();
return;
}

if (asyncTask->listener_)
asyncTask->listener_->HandleEvent(msg->code, msg->param, asyncTask);
}
Show details Hide details

Change log

r2848 by gears.daemon on Sep 17, 2008   Diff
[Author: steveblock]

Sets Content-Type header for Geolocation
network requests.

R=andreip
CC=gears-eng@googlegroups.com
APPROVED=playmobil
DELTA=60  (59 added, 0 deleted, 1 changed)
OCL=8255929
SCL=8303317
Go to: 
Project members, sign in to write a code review

Older revisions

r2577 by gears.daemon on Aug 04, 2008   Diff
[Author: steveblock]

Adds to HttpRequest and
AsyncTask::HttpPost the ability to
disable sending browser cookies with
...
r2369 by gears.daemon on Jul 15, 2008   Diff
[Author: andreip]

AsyncTask returns the response body as
a BlobInterface.

...
r2364 by gears.daemon on Jul 15, 2008   Diff
[Author: playmobil]

* #include -> #import in all obj-c
files.
* Added include guards to all obj-c
...
All revisions of this file

File info

Size: 15629 bytes, 434 lines