Gnash 0.8.9
|
00001 // 00002 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 00003 // 2011 Free Software Foundation, Inc 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 // 00019 00020 00021 #ifndef GNASH_LOADVARIABLESTHREAD_H 00022 #define GNASH_LOADVARIABLESTHREAD_H 00023 00024 #include "StreamProvider.h" // for inlines 00025 #include "URL.h" // for inlines 00026 00027 00028 #include <string> 00029 #include <map> 00030 #include <memory> 00031 #include <boost/thread/thread.hpp> 00032 #include <boost/thread/mutex.hpp> 00033 #include <boost/bind.hpp> 00034 00035 // Forward declarations 00036 namespace gnash { 00037 //class URL; 00038 } 00039 00040 namespace gnash { 00041 00042 // Exception thrown by LoadVariablesThread constructor if unable to connect 00043 // to the stream input. 00044 class NetworkException {}; 00045 00047 // 00051 class LoadVariablesThread 00052 { 00053 public: 00054 typedef std::map<std::string, std::string> ValuesMap; 00055 00057 // 00063 LoadVariablesThread(const StreamProvider& sp, const URL& url); 00064 00068 // 00077 LoadVariablesThread(const StreamProvider& sp, const URL& url, 00078 const std::string& postdata); 00079 00081 ~LoadVariablesThread(); 00082 00084 ValuesMap& getValues() 00085 { 00086 return _vals; 00087 } 00088 00090 void process() 00091 { 00092 assert(!_thread.get()); 00093 assert(_stream.get()); 00094 _thread.reset(new boost::thread( 00095 boost::bind(LoadVariablesThread::execLoadingThread, this))); 00096 } 00097 00099 // 00102 void cancel(); 00103 00105 bool inProgress() 00106 { 00107 // TODO: should we mutex-protect this ? 00108 return ( _thread.get() != NULL ); 00109 } 00110 00112 // 00117 bool completed() 00118 { 00119 boost::mutex::scoped_lock lock(_mutex); 00120 if ( _completed && _thread.get() ) 00121 { 00122 _thread->join(); 00123 _thread.reset(); 00124 } 00125 return _completed; 00126 } 00127 00128 size_t getBytesLoaded() const 00129 { 00130 // TODO: should we mutex-protect this ? 00131 return _bytesLoaded; 00132 } 00133 00134 size_t getBytesTotal() const 00135 { 00136 // TODO: should we mutex-protect this ? 00137 return _bytesTotal; 00138 } 00139 00140 00141 private: 00142 00144 LoadVariablesThread& operator==(const LoadVariablesThread&); 00145 LoadVariablesThread(const LoadVariablesThread&); 00146 00151 static void execLoadingThread(LoadVariablesThread* ptr) 00152 { 00153 //log_debug("LoadVars loading thread started"); 00154 ptr->completeLoad(); 00155 //log_debug("LoadVars loading thread completed"); 00156 } 00157 00158 00160 void setCompleted() 00161 { 00162 boost::mutex::scoped_lock lock(_mutex); 00163 _completed = true; 00164 //log_debug("Completed"); 00165 } 00166 00167 00169 // 00172 void completeLoad(); 00173 00175 // 00185 size_t parse(const std::string& str) 00186 { 00187 URL::parse_querystring(str, _vals); 00188 return _vals.size(); 00189 } 00190 00192 // 00195 bool cancelRequested(); 00196 00197 size_t _bytesLoaded; 00198 00199 size_t _bytesTotal; 00200 00201 std::auto_ptr<IOChannel> _stream; 00202 00203 std::auto_ptr<boost::thread> _thread; 00204 00205 ValuesMap _vals; 00206 00207 bool _completed; 00208 00209 bool _canceled; 00210 00211 boost::mutex _mutex; 00212 }; 00213 00214 } // namespace gnash 00215 00216 #endif // GNASH_LOADVARIABLESTHREAD_H