2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_MOVIE_LOADER_H
20 #define GNASH_MOVIE_LOADER_H
22 #include <boost/intrusive_ptr.hpp>
25 #include <boost/ptr_container/ptr_list.hpp>
26 #include <boost/noncopyable.hpp>
27 #include <boost/thread/thread.hpp>
28 #include <boost/thread/condition.hpp>
29 #include <boost/thread/barrier.hpp>
32 #include "MovieClip.h"
34 // Forward declarations
37 class movie_definition
;
45 /// All public functions are intended to be called by the main thread
46 /// Hide the asynchonous mechanism of movies loading.
47 /// Currently implemented using threads, could be refactored to use
48 /// non-blocking reads.
50 class DSOEXPORT MovieLoader
: boost::noncopyable
{
54 MovieLoader(movie_root
& mr
);
58 /// Queue a request for loading a movie
60 /// This function constructs the URL and, if required, the postdata
61 /// from the arguments. The variables to send should *not* be appended
62 /// to @param urlstr before calling this function.
64 /// @param urlstr The url exactly as requested. This may already
65 /// contain a query string.
66 /// @param target Target for request.
67 /// @param data The variables data to send, URL encoded in
69 /// @param method The VariablesMethod to use for sending the data. If
70 /// MovieClip::METHOD_NONE, no data will be sent.
71 /// @param handler An object which will be signalled of load
72 /// events (onLoadStart, onLoadComplete, onLoadInit,
73 /// onLoadError). Can be null if caller doesn't care.
75 void loadMovie(const std::string
& url
, const std::string
& target
,
76 const std::string
& data
, MovieClip::VariablesMethod method
,
77 as_object
* handler
=0);
79 /// Drop all requests and kill the thread
82 /// Process all completed movie load requests.
83 void processCompletedRequests();
85 void setReachable() const;
89 /// A movie load request
90 class Request
: boost::noncopyable
{
93 /// If not null POST method will be used for HTTP.
95 Request(const URL
& u
, const std::string
& t
,
96 const std::string
* postdata
, as_object
* handler
)
107 _postData
= *postdata
;
112 const std::string
& getTarget() const { return _target
; }
113 const URL
& getURL() const { return _url
; }
114 const std::string
& getPostData() const { return _postData
; }
115 bool usePost() const { return _usePost
; }
116 as_object
* getHandler() const { return _handler
; }
117 void setReachable() const {
118 if (_handler
) _handler
->setReachable();
121 /// Get the loaded movie definition, if any
123 /// @param md the loaded movie_definition is copied here
124 /// if it was impossible to create one.
126 /// @return true if the request was completed, false otherwise.
128 /// RULE: if return is FALSE param 'md' will be set to 0.
129 /// RULE: if return is TRUE param 'md' may be set to 0 or non 0.
130 /// RULE: if parameter 'md' is set to non 0, TRUE must be returned.
134 bool getCompleted(boost::intrusive_ptr
<movie_definition
>& md
) const
136 boost::mutex::scoped_lock
lock(_mutex
);
141 /// Only check if request is completed
144 boost::mutex::scoped_lock
lock(_mutex
);
148 /// Only check if request is completed
149 bool completed() const
151 boost::mutex::scoped_lock
lock(_mutex
);
155 /// Complete the request
157 /// @param md the loaded movie_definition, or 0 if
158 /// it was impossible to create one.
162 void setCompleted(boost::intrusive_ptr
<movie_definition
> md
)
164 boost::mutex::scoped_lock
lock(_mutex
);
173 std::string _postData
;
174 boost::intrusive_ptr
<movie_definition
> _mdef
;
175 mutable boost::mutex _mutex
;
181 typedef boost::ptr_list
<Request
> Requests
;
184 mutable boost::mutex _requestsMutex
;
186 void processRequests();
187 void processRequest(Request
& r
);
188 void clearRequests();
190 /// Check a Request and process if completed.
192 /// @return true if the request was completely processed.
194 bool processCompletedRequest(const Request
& r
);
196 /// Was thread kill requested ?
201 boost::mutex _killMutex
;
203 boost::condition _wakeup
;
205 /// needed for some facilities like find_character_by_target
206 movie_root
& _movieRoot
;
208 std::auto_ptr
<boost::thread
> _thread
;
210 // Barrier to ensure that _thread
211 // is initialized before the loader thread
212 // continues execution
213 boost::barrier _barrier
;
219 #endif // GNASH_MOVIE_LOADER_H