1/* 2 * Copyright 2010 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Christophe Huriaux, c.huriaux@gmail.com 7 */ 8 9 10#include <new> 11 12#include <AppKit.h> 13#include <UrlProtocolAsynchronousListener.h> 14#include <Debug.h> 15 16extern const char* kUrlProtocolMessageType; 17extern const char* kUrlProtocolCaller; 18 19BUrlProtocolAsynchronousListener::BUrlProtocolAsynchronousListener( 20 bool transparent) 21 : 22 BHandler("UrlProtocolAsynchronousListener"), 23 fSynchronousListener(NULL) 24{ 25 if (be_app->Lock()) { 26 be_app->AddHandler(this); 27 be_app->Unlock(); 28 } 29 else 30 PRINT(("Cannot lock be_app\n")); 31 32 if (transparent) 33 fSynchronousListener 34 = new(std::nothrow) BUrlProtocolDispatchingListener(this); 35} 36 37 38BUrlProtocolAsynchronousListener::~BUrlProtocolAsynchronousListener() 39{ 40 if (be_app->Lock()) { 41 be_app->RemoveHandler(this); 42 be_app->Unlock(); 43 } 44 delete fSynchronousListener; 45} 46 47 48void 49BUrlProtocolAsynchronousListener::ConnectionOpened(BUrlProtocol*) 50{ 51} 52 53 54void 55BUrlProtocolAsynchronousListener::HostnameResolved(BUrlProtocol*, const char*) 56{ 57} 58 59 60void 61BUrlProtocolAsynchronousListener::ResponseStarted(BUrlProtocol*) 62{ 63} 64 65 66void 67BUrlProtocolAsynchronousListener::HeadersReceived(BUrlProtocol*) 68{ 69} 70 71 72void 73BUrlProtocolAsynchronousListener::DataReceived(BUrlProtocol*, const char*, 74 ssize_t) 75{ 76} 77 78 79void 80BUrlProtocolAsynchronousListener::DownloadProgress(BUrlProtocol*, ssize_t, 81 ssize_t) 82{ 83} 84 85 86void 87BUrlProtocolAsynchronousListener::UploadProgress(BUrlProtocol*, ssize_t, 88 ssize_t) 89{ 90} 91 92 93void 94BUrlProtocolAsynchronousListener::RequestCompleted(BUrlProtocol*, bool) 95{ 96} 97 98 99// #pragma mark Synchronous listener access 100 101 102BUrlProtocolListener* 103BUrlProtocolAsynchronousListener::SynchronousListener() 104{ 105 return fSynchronousListener; 106} 107 108 109void 110BUrlProtocolAsynchronousListener::MessageReceived(BMessage* message) 111{ 112 if (message->what != B_URL_PROTOCOL_NOTIFICATION) { 113 BHandler::MessageReceived(message); 114 return; 115 } 116 117 BUrlProtocol* caller; 118 if (message->FindPointer(kUrlProtocolCaller, 119 reinterpret_cast<void**>(&caller)) != B_OK) 120 return; 121 122 int8 notification; 123 if (message->FindInt8(kUrlProtocolMessageType, ¬ification) 124 != B_OK) 125 return; 126 127 switch (notification) { 128 case B_URL_PROTOCOL_CONNECTION_OPENED: 129 ConnectionOpened(caller); 130 break; 131 132 case B_URL_PROTOCOL_HOSTNAME_RESOLVED: 133 { 134 const char* ip; 135 message->FindString("url:ip", &ip); 136 137 HostnameResolved(caller, ip); 138 } 139 break; 140 141 case B_URL_PROTOCOL_RESPONSE_STARTED: 142 ResponseStarted(caller); 143 break; 144 145 case B_URL_PROTOCOL_HEADERS_RECEIVED: 146 HeadersReceived(caller); 147 break; 148 149 case B_URL_PROTOCOL_DATA_RECEIVED: 150 { 151 const char* data; 152 ssize_t size; 153 message->FindData("url:data", B_STRING_TYPE, 154 reinterpret_cast<const void**>(&data), &size); 155 156 DataReceived(caller, data, size); 157 } 158 break; 159 160 case B_URL_PROTOCOL_DOWNLOAD_PROGRESS: 161 { 162 int32 bytesReceived; 163 int32 bytesTotal; 164 message->FindInt32("url:bytesReceived", &bytesReceived); 165 message->FindInt32("url:bytesTotal", &bytesTotal); 166 167 DownloadProgress(caller, bytesReceived, bytesTotal); 168 } 169 break; 170 171 case B_URL_PROTOCOL_UPLOAD_PROGRESS: 172 { 173 int32 bytesSent; 174 int32 bytesTotal; 175 message->FindInt32("url:bytesSent", &bytesSent); 176 message->FindInt32("url:bytesTotal", &bytesTotal); 177 178 UploadProgress(caller, bytesSent, bytesTotal); 179 } 180 break; 181 182 case B_URL_PROTOCOL_REQUEST_COMPLETED: 183 { 184 bool success; 185 message->FindBool("url:success", &success); 186 187 RequestCompleted(caller, success); 188 } 189 break; 190 191 default: 192 PRINT(("BUrlProtocolAsynchronousListener: Unknown notification %d\n", 193 notification)); 194 break; 195 } 196} 197