5bd72771b0b6e5514ddb973636034d0ac1771dba
[platform/framework/web/crosswalk-tizen.git] /
1 // Copyright 2016 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_MEDIA_PLAYER_TIZEN_ELEMENTARY_STREAM_LISTENER_PRIVATE_WRAPPER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_MEDIA_PLAYER_TIZEN_ELEMENTARY_STREAM_LISTENER_PRIVATE_WRAPPER_H_
7
8 #include "base/bind.h"
9 #include "base/callback.h"
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/lock.h"
14 #include "content/browser/renderer_host/pepper/media_player/elementary_stream_listener_private.h"
15 #include "ppapi/c/pp_time.h"
16
17 namespace tracked_objects {
18 class Location;
19 }  // namespace tracked_objects
20
21 namespace content {
22
23 // Helper class which dispatches all data related events to the main thread.
24 class ElementaryStreamListenerPrivateWrapper {
25  public:
26   ElementaryStreamListenerPrivateWrapper();
27
28   void SetListener(ElementaryStreamListenerPrivate* listener);
29   void RemoveListener(ElementaryStreamListenerPrivate* listener);
30
31   // Called on player deletion and clean-up
32   void ClearListener();
33
34   // Below 3 functions are called on player thread
35   // and deals with values passed by Platform Player API
36
37   void OnNeedData(unsigned bytes_max);
38   void OnEnoughData();
39   void OnSeekData(PP_TimeTicks offset);
40
41  private:
42   template <typename Method, typename... Args>
43   void DispatchLocked(const tracked_objects::Location& from_here,
44                       const Method& method,
45                       const Args&... args);
46
47   // NOLINTNEXTLINE(runtime/int)
48   void NeedData(unsigned bytes_max);
49
50   void EnoughData();
51
52   void SeekData(PP_TimeTicks offset);
53
54   ElementaryStreamListenerPrivate* listener_;
55   base::WeakPtrFactory<ElementaryStreamListenerPrivateWrapper>
56       weak_ptr_factory_;
57   base::WeakPtr<ElementaryStreamListenerPrivateWrapper> weak_ptr_;
58   base::Lock weak_ptr_lock_;
59   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
60
61   DISALLOW_COPY_AND_ASSIGN(ElementaryStreamListenerPrivateWrapper);
62 };
63
64 template <typename Method, typename... Args>
65 void ElementaryStreamListenerPrivateWrapper::DispatchLocked(
66     const tracked_objects::Location& from_here,
67     const Method& method,
68     const Args&... args) {
69   base::AutoLock guard(weak_ptr_lock_);
70   io_task_runner_->PostTask(from_here, base::Bind(method, weak_ptr_, args...));
71 }
72
73 }  // namespace content
74
75 #endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_MEDIA_PLAYER_TIZEN_ELEMENTARY_STREAM_LISTENER_PRIVATE_WRAPPER_H_