enable docbook, install info pages
[gnash.git] / libcore / MovieLoader.h
blobb20e155459786bf56e4df43f9fda269e6b7c13b3
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software 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 <boost/intrusive_ptr.hpp>
23 #include <list>
24 #include <string>
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>
31 #include "URL.h"
32 #include "MovieClip.h"
34 // Forward declarations
35 namespace gnash {
36 class movie_root;
37 class movie_definition;
38 class as_object;
41 namespace gnash {
43 /// Movie loader
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.
49 ///
50 class DSOEXPORT MovieLoader : boost::noncopyable {
52 public:
54 MovieLoader(movie_root& mr);
56 ~MovieLoader();
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
68 /// key/value pairs
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.
74 ///
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
80 void clear();
82 /// Process all completed movie load requests.
83 void processCompletedRequests();
85 void setReachable() const;
87 private:
89 /// A movie load request
90 class Request : boost::noncopyable {
91 public:
92 /// @param postdata
93 /// If not null POST method will be used for HTTP.
94 ///
95 Request(const URL& u, const std::string& t,
96 const std::string* postdata, as_object* handler)
98 _target(t),
99 _url(u),
100 _usePost(false),
101 _mdef(0),
102 _mutex(),
103 _handler(handler),
104 _completed(false)
106 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 boost::ptr_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