[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | |
| 33 | // This file contains the declaration of the ArchiveRequest class. |
| 34 | |
| 35 | #ifndef O3D_IMPORT_CROSS_ARCHIVE_REQUEST_H_ |
| 36 | #define O3D_IMPORT_CROSS_ARCHIVE_REQUEST_H_ |
| 37 | |
| 38 | #include <algorithm> |
| 39 | #include <string> |
| 40 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 41 | #include "base/memory/scoped_ptr.h" |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 42 | #include "core/cross/callback.h" |
| 43 | #include "core/cross/object_base.h" |
| 44 | #include "core/cross/pack.h" |
| 45 | #include "import/cross/targz_processor.h" |
| 46 | #include "import/cross/memory_buffer.h" |
| 47 | #include "import/cross/memory_stream.h" |
| 48 | #include "import/cross/raw_data.h" |
| 49 | #include "plugin/cross/download_stream.h" |
| 50 | |
| 51 | namespace o3d { |
| 52 | |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 53 | typedef Closure ArchiveReadyStateChangeCallback; |
| 54 | typedef Callback1<RawData*> ArchiveFileAvailableCallback; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 55 | |
| 56 | // An ArchiveRequest object is used to carry out an asynchronous request |
| 57 | // for a file to be loaded. |
| 58 | class ArchiveRequest : public ObjectBase, public ArchiveCallbackClient { |
| 59 | public: |
| 60 | enum ReadyState { // These are copied from XMLHttpRequest. |
| 61 | STATE_INIT = 0, |
| 62 | STATE_OPEN = 1, |
| 63 | STATE_SENT = 2, |
| 64 | STATE_RECEIVING = 3, |
| 65 | STATE_LOADED = 4, |
| 66 | }; |
| 67 | |
| 68 | // A file by this name must be the first file in the archive otherwise |
| 69 | // the archive is rejected. This is a security measure so that O3D |
| 70 | // can not be used to open arbitrary .tgz files but only those files someone |
| 71 | // has specifically prepared for O3D. This file will not be passed to |
| 72 | // the onfileavailable callback. |
[email protected] | 5edab80 | 2009-08-31 18:13:40 | [diff] [blame] | 73 | static const char* const kO3DMarker; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 74 | |
[email protected] | 5edab80 | 2009-08-31 18:13:40 | [diff] [blame] | 75 | // The contents of the O3D marker file. Arguably the content should not matter |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 76 | // but for the sake of completeness we define the content so there is no |
| 77 | // ambiguity. |
[email protected] | 5edab80 | 2009-08-31 18:13:40 | [diff] [blame] | 78 | static const char* const kO3DMarkerContent; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 79 | |
| 80 | // The size of the O3D_MARKER_CONTENT. |
[email protected] | 5edab80 | 2009-08-31 18:13:40 | [diff] [blame] | 81 | static const size_t kO3DMarkerContentLength; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 82 | |
| 83 | public: |
| 84 | typedef SmartPointer<ArchiveRequest> Ref; |
| 85 | |
| 86 | virtual ~ArchiveRequest(); |
| 87 | |
| 88 | static ArchiveRequest *Create(ServiceLocator* service_locator, |
| 89 | Pack *pack); |
| 90 | |
| 91 | |
| 92 | // Streaming callbacks |
| 93 | virtual void NewStreamCallback(glue::DownloadStream *stream); |
| 94 | virtual int32 WriteReadyCallback(glue::DownloadStream *stream); |
| 95 | virtual int32 WriteCallback(glue::DownloadStream *stream, |
| 96 | int32 offset, |
| 97 | int32 length, |
| 98 | void *data); |
| 99 | |
| 100 | virtual void FinishedCallback(glue::DownloadStream *stream, |
| 101 | bool success, |
| 102 | const std::string &filename, |
| 103 | const std::string &mime_type); |
| 104 | |
| 105 | // ArchiveCallbackClient methods |
| 106 | virtual void ReceiveFileHeader(const ArchiveFileInfo &file_info); |
[email protected] | d2fec01 | 2009-06-08 16:43:27 | [diff] [blame] | 107 | virtual bool ReceiveFileData(MemoryReadStream *stream, size_t nbytes); |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 108 | virtual void Close(bool success); |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 109 | |
| 110 | Pack *pack() { |
| 111 | return pack_.Get(); // Set at creation time and never changed. |
| 112 | } |
| 113 | |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 114 | ArchiveFileAvailableCallback *onfileavailable() { |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 115 | return onfileavailable_.get(); |
| 116 | } |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 117 | void set_onfileavailable(ArchiveFileAvailableCallback *onfileavailable) { |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 118 | onfileavailable_.reset(onfileavailable); |
| 119 | } |
| 120 | |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 121 | ArchiveReadyStateChangeCallback *onreadystatechange() { |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 122 | return onreadystatechange_.get(); |
| 123 | } |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 124 | void set_onreadystatechange( |
| 125 | ArchiveReadyStateChangeCallback *onreadystatechange) { |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 126 | onreadystatechange_.reset(onreadystatechange); |
| 127 | } |
| 128 | |
| 129 | // returns the "current" data object (used by the callback) |
| 130 | RawData *data() { |
| 131 | return raw_data_.Get(); |
| 132 | } |
| 133 | |
| 134 | const String& uri() { |
| 135 | return uri_; |
| 136 | } |
| 137 | void set_uri(const String& uri) { |
| 138 | uri_ = uri; |
| 139 | } |
| 140 | |
| 141 | bool done() { |
| 142 | return done_; |
| 143 | } |
| 144 | |
| 145 | bool success() { |
| 146 | return success_; |
| 147 | } |
| 148 | void set_success(bool success) { |
| 149 | success_ = success; |
| 150 | done_ = true; |
| 151 | pack_.Reset(); // Remove pack reference to allow garbage collection of pack. |
| 152 | } |
| 153 | |
| 154 | const String& error() const { |
| 155 | return error_; |
| 156 | } |
| 157 | void set_error(const String& error) { |
| 158 | error_ = error; |
| 159 | } |
| 160 | |
| 161 | int ready_state() { |
| 162 | return ready_state_; |
| 163 | } |
| 164 | void set_ready_state(int state) { |
| 165 | ready_state_ = state; |
| 166 | } |
| 167 | |
| 168 | int stream_length() { |
| 169 | return stream_length_; |
| 170 | } |
| 171 | |
| 172 | int bytes_received() { |
| 173 | return bytes_received_; |
| 174 | } |
| 175 | |
| 176 | protected: |
| 177 | ArchiveRequest(ServiceLocator* service_locator, Pack *pack); |
| 178 | |
| 179 | Pack::Ref pack_; |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 180 | scoped_ptr<ArchiveReadyStateChangeCallback> onreadystatechange_; |
| 181 | scoped_ptr<ArchiveFileAvailableCallback> onfileavailable_; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 182 | String uri_; |
| 183 | |
| 184 | // Request state |
| 185 | bool done_; // Set after completion/failure to indicate success_ is valid. |
| 186 | bool success_; // Set after completion/failure to indicate which it is. |
| 187 | int ready_state_; // Like the XMLHttpRequest variable of the same name. |
| 188 | String error_; // Set after completion on failure. |
| 189 | |
[email protected] | 91240c9 | 2009-08-03 19:49:35 | [diff] [blame] | 190 | StreamProcessor *archive_processor_; |
| 191 | StreamProcessor *extra_processor_; |
| 192 | ArchiveCallbackClient* main_thread_archive_callback_client_; |
[email protected] | 05b47f7 | 2009-05-27 23:15:42 | [diff] [blame] | 193 | std::vector<RawData::Ref> raw_data_list_; |
| 194 | RawData::Ref raw_data_; |
| 195 | MemoryBuffer<uint8> temp_buffer_; |
| 196 | MemoryWriteStream file_memory_stream_; |
| 197 | String current_filename_; |
| 198 | |
| 199 | int stream_length_; // total length of stream |
| 200 | int bytes_received_; // bytes received so far |
| 201 | |
| 202 | O3D_DECL_CLASS(ArchiveRequest, ObjectBase) |
| 203 | DISALLOW_IMPLICIT_CONSTRUCTORS(ArchiveRequest); |
| 204 | }; // ArchiveRequest |
| 205 | |
| 206 | } // namespace o3d |
| 207 | |
| 208 | #endif // O3D_IMPORT_CROSS_ARCHIVE_REQUEST_H_ |