Gnash  0.8.11dev
MovieLoader.h
Go to the documentation of this file.
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
18 
19 #ifndef GNASH_MOVIE_LOADER_H
20 #define GNASH_MOVIE_LOADER_H
21 
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>
30 
31 #include "URL.h"
32 #include "MovieClip.h"
33 
34 // Forward declarations
35 namespace gnash {
36  class movie_root;
37  class movie_definition;
38  class as_object;
39 }
40 
41 namespace gnash {
42 
44 //
50 class DSOEXPORT MovieLoader : boost::noncopyable {
51 
52 public:
53 
55 
56  ~MovieLoader();
57 
59  //
63  //
75  void loadMovie(const std::string& url, const std::string& target,
76  const std::string& data, MovieClip::VariablesMethod method,
77  as_object* handler=0);
78 
80  void clear();
81 
83  void processCompletedRequests();
84 
85  void setReachable() const;
86 
87 private:
88 
90  class Request : boost::noncopyable {
91  public:
95  Request(const URL& u, const std::string& t,
96  const std::string* postdata, as_object* handler)
97  :
98  _target(t),
99  _url(u),
100  _usePost(false),
101  _mdef(0),
102  _mutex(),
103  _handler(handler),
104  _completed(false)
105  {
106  if (postdata) {
107  _postData = *postdata;
108  _usePost = true;
109  }
110  }
111 
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();
119  }
120 
122  //
134  bool getCompleted(boost::intrusive_ptr<movie_definition>& md) const
135  {
136  boost::mutex::scoped_lock lock(_mutex);
137  md = _mdef;
138  return _completed;
139  }
140 
142  bool pending() const
143  {
144  boost::mutex::scoped_lock lock(_mutex);
145  return !_completed;
146  }
147 
149  bool completed() const
150  {
151  boost::mutex::scoped_lock lock(_mutex);
152  return _completed;
153  }
154 
156  //
162  void setCompleted(boost::intrusive_ptr<movie_definition> md)
163  {
164  boost::mutex::scoped_lock lock(_mutex);
165  _mdef = md;
166  _completed = true;
167  }
168 
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;
177  bool _completed;
178  };
179 
181  typedef boost::ptr_list<Request> Requests;
182  Requests _requests;
183 
184  mutable boost::mutex _requestsMutex;
185 
186  void processRequests();
187  void processRequest(Request& r);
188  void clearRequests();
189 
191  //
194  bool processCompletedRequest(const Request& r);
195 
197  bool killed();
198 
199  bool _killed;
200 
201  boost::mutex _killMutex;
202 
203  boost::condition _wakeup;
204 
206  movie_root& _movieRoot;
207 
208  std::auto_ptr<boost::thread> _thread;
209 
210  // Barrier to ensure that _thread
211  // is initialized before the loader thread
212  // continues execution
213  boost::barrier _barrier;
214 
215 };
216 
217 } // namespace gnash
218 
219 #endif // GNASH_MOVIE_LOADER_H