Gnash  0.8.10
MovieLoader.h
Go to the documentation of this file.
00001 // 
00002 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00003 //   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 #ifndef GNASH_MOVIE_LOADER_H
00020 #define GNASH_MOVIE_LOADER_H
00021 
00022 #include <boost/intrusive_ptr.hpp>
00023 #include <list>
00024 #include <string>
00025 #include <boost/ptr_container/ptr_list.hpp>
00026 #include <boost/noncopyable.hpp>
00027 #include <boost/thread/thread.hpp>
00028 #include <boost/thread/condition.hpp>
00029 #include <boost/thread/barrier.hpp>
00030 
00031 #include "URL.h"
00032 #include "MovieClip.h" 
00033 
00034 // Forward declarations
00035 namespace gnash {
00036     class movie_root;
00037     class movie_definition;
00038     class as_object;
00039 }
00040 
00041 namespace gnash {
00042 
00044 //
00050 class DSOEXPORT MovieLoader : boost::noncopyable {
00051 
00052 public:
00053 
00054     MovieLoader(movie_root& mr);
00055 
00056     ~MovieLoader();
00057 
00059     //
00063     //
00075     void loadMovie(const std::string& url, const std::string& target,
00076             const std::string& data, MovieClip::VariablesMethod method,
00077             as_object* handler=0);
00078 
00080     void clear();
00081 
00083     void processCompletedRequests();
00084 
00085     void setReachable() const;
00086 
00087 private:
00088 
00090     class Request : boost::noncopyable {
00091     public:
00095         Request(const URL& u, const std::string& t,
00096                 const std::string* postdata, as_object* handler)
00097                 :
00098                 _target(t),
00099                 _url(u),
00100                 _usePost(false),
00101                 _mdef(0),
00102                 _mutex(),
00103                 _handler(handler),
00104                 _completed(false)
00105         {
00106             if (postdata) {
00107                 _postData = *postdata;
00108                 _usePost = true;
00109             }
00110         }
00111 
00112         const std::string& getTarget() const { return _target; }
00113         const URL& getURL() const { return _url; }
00114         const std::string& getPostData() const { return _postData; }
00115         bool usePost() const { return _usePost; }
00116         as_object* getHandler() const { return _handler; }
00117         void setReachable() const {
00118             if (_handler) _handler->setReachable();
00119         }
00120 
00122         //
00134         bool getCompleted(boost::intrusive_ptr<movie_definition>& md) const
00135         {
00136             boost::mutex::scoped_lock lock(_mutex);
00137             md = _mdef;
00138             return _completed;
00139         }
00140 
00142         bool pending() const
00143         {
00144             boost::mutex::scoped_lock lock(_mutex);
00145             return !_completed;
00146         }
00147 
00149         bool completed() const
00150         {
00151             boost::mutex::scoped_lock lock(_mutex);
00152             return _completed;
00153         }
00154 
00156         //
00162         void setCompleted(boost::intrusive_ptr<movie_definition> md)
00163         {
00164             boost::mutex::scoped_lock lock(_mutex);
00165             _mdef = md;
00166             _completed = true;
00167         }
00168 
00169     private:
00170         std::string _target;
00171         URL _url;
00172         bool _usePost;
00173         std::string _postData;
00174         boost::intrusive_ptr<movie_definition> _mdef;
00175         mutable boost::mutex _mutex;
00176         as_object* _handler;
00177         bool _completed;
00178     };
00179 
00181     typedef boost::ptr_list<Request> Requests;
00182     Requests _requests;
00183 
00184         mutable boost::mutex _requestsMutex;
00185 
00186     void processRequests();
00187     void processRequest(Request& r);
00188     void clearRequests();
00189 
00191     //
00194     bool processCompletedRequest(const Request& r);
00195 
00197     bool killed();
00198 
00199     bool _killed;
00200 
00201         boost::mutex _killMutex;
00202 
00203     boost::condition _wakeup;
00204 
00206     movie_root& _movieRoot;
00207 
00208     std::auto_ptr<boost::thread> _thread;
00209 
00210         // Barrier to ensure that _thread
00211         // is initialized before the loader thread
00212         // continues execution
00213         boost::barrier _barrier;
00214 
00215 };
00216 
00217 } // namespace gnash
00218 
00219 #endif // GNASH_MOVIE_LOADER_H