• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. 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 ScriptStreamerThread_h
6 #define ScriptStreamerThread_h
7 
8 #include "platform/TaskSynchronizer.h"
9 #include "public/platform/WebThread.h"
10 #include "wtf/OwnPtr.h"
11 
12 #include <v8.h>
13 
14 namespace blink {
15 
16 class ScriptStreamer;
17 
18 // A singleton thread for running background tasks for script streaming.
19 class ScriptStreamerThread {
20     WTF_MAKE_NONCOPYABLE(ScriptStreamerThread);
21 public:
22     static void init();
23     static void shutdown();
24     static ScriptStreamerThread* shared();
25 
26     void postTask(WebThread::Task*);
27 
isRunningTask()28     bool isRunningTask() const
29     {
30         MutexLocker locker(m_mutex);
31         return m_runningTask;
32     }
33 
34     void taskDone();
35 
36 private:
ScriptStreamerThread()37     ScriptStreamerThread()
38         : m_runningTask(false) { }
39 
isRunning()40     bool isRunning() const
41     {
42         return !!m_thread;
43     }
44 
markAsCompleted(TaskSynchronizer * taskSynchronizer)45     void markAsCompleted(TaskSynchronizer* taskSynchronizer)
46     {
47         taskSynchronizer->taskCompleted();
48     }
49 
50     blink::WebThread& platformThread();
51 
52     // At the moment, we only use one thread, so we can only stream one script
53     // at a time. FIXME: Use a thread pool and stream multiple scripts.
54     WTF::OwnPtr<blink::WebThread> m_thread;
55     bool m_runningTask;
56     mutable Mutex m_mutex; // Guards m_runningTask.
57 };
58 
59 class ScriptStreamingTask : public WebThread::Task {
60     WTF_MAKE_NONCOPYABLE(ScriptStreamingTask);
61 public:
62     ScriptStreamingTask(v8::ScriptCompiler::ScriptStreamingTask*, ScriptStreamer*);
63     virtual void run() OVERRIDE;
64 
65 private:
66     WTF::OwnPtr<v8::ScriptCompiler::ScriptStreamingTask> m_v8Task;
67     ScriptStreamer* m_streamer;
68 };
69 
70 
71 } // namespace blink
72 
73 #endif // ScriptStreamerThread_h
74