Drop the bool operator for ObjectURI, to avoid getting the kind of side-effect that...
[gnash.git] / libcore / MovieLoader.h
blobb539a27aeae2b8968e33cc1142ae06c5a7cc8d73
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
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.
9 //
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.
14 //
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 "URL.h"
23 #include "as_object.h"
24 #include "MovieClip.h"
26 #include <boost/intrusive_ptr.hpp>
27 #include <list>
28 #include <string>
29 #include <boost/noncopyable.hpp>
30 #include <boost/thread/thread.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <boost/thread/barrier.hpp>
34 // Forward declarations
35 namespace gnash {
36 class movie_root;
37 class movie_definition;
40 namespace gnash {
42 /// Movie loader
44 /// All public functions are intended to be called by the main thread
45 /// Hide the asynchonous mechanism of movies loading.
46 /// Currently implemented using threads, could be refactored to use
47 /// non-blocking reads.
48 ///
49 class DSOEXPORT MovieLoader : boost::noncopyable {
51 public:
53 MovieLoader(movie_root& mr);
55 ~MovieLoader();
57 /// Queue a request for loading a movie
59 /// This function constructs the URL and, if required, the postdata
60 /// from the arguments. The variables to send should *not* be appended
61 /// to @param urlstr before calling this function.
63 /// @param urlstr The url exactly as requested. This may already
64 /// contain a query string.
65 /// @param target Target for request.
66 /// @param data The variables data to send, URL encoded in
67 /// key/value pairs
68 /// @param method The VariablesMethod to use for sending the data. If
69 /// MovieClip::METHOD_NONE, no data will be sent.
70 /// @param handler An object which will be signalled of load
71 /// events (onLoadStart, onLoadComplete, onLoadInit,
72 /// onLoadError). Can be null if caller doesn't care.
73 ///
74 void loadMovie(const std::string& url, const std::string& target,
75 const std::string& data, MovieClip::VariablesMethod method,
76 as_object* handler=0);
78 /// Drop all requests and kill the thread
79 void clear();
81 /// Process all completed movie load requests.
82 void processCompletedRequests();
84 void setReachable() const;
86 private:
88 /// A movie load request
89 class Request : boost::noncopyable {
90 public:
91 /// @param postdata
92 /// If not null POST method will be used for HTTP.
93 ///
94 Request(const URL& u, const std::string& t,
95 const std::string* postdata, as_object* handler)
97 _target(t),
98 _url(u),
99 _usePost(false),
100 _mdef(0),
101 _mutex(),
102 _handler(handler),
103 _completed(false)
105 if ( postdata )
107 _postData = *postdata;
108 _usePost = true;
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.
132 /// locks _mutex
134 bool getCompleted(boost::intrusive_ptr<movie_definition>& md) const
136 boost::mutex::scoped_lock lock(_mutex);
137 md = _mdef;
138 return _completed;
141 /// Only check if request is completed
142 bool pending() const
144 boost::mutex::scoped_lock lock(_mutex);
145 return !_completed;
148 /// Only check if request is completed
149 bool completed() const
151 boost::mutex::scoped_lock lock(_mutex);
152 return _completed;
155 /// Complete the request
157 /// @param md the loaded movie_definition, or 0 if
158 /// it was impossible to create one.
160 /// locks _mutex
162 void setCompleted(boost::intrusive_ptr<movie_definition> md)
164 boost::mutex::scoped_lock lock(_mutex);
165 _mdef = md;
166 _completed = true;
169 private:
170 std::string _target;
171 URL _url;
172 bool _usePost;
173 std::string _postData;
174 boost::intrusive_ptr<movie_definition> _mdef;
175 mutable boost::mutex _mutex;
176 as_object* _handler;
177 bool _completed;
180 /// Load requests
181 typedef std::list<Request*> Requests;
182 Requests _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 ?
197 bool killed();
199 bool _killed;
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;
217 } // namespace gnash
219 #endif // GNASH_MOVIE_LOADER_H