OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ | 5 #ifndef CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ |
6 #define CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ | 6 #define CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ |
7 | 7 |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/lock.h" | 9 #include "base/lock.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
12 #include "base/thread.h" | 12 #include "base/thread.h" |
| 13 #include "chrome/browser/chrome_thread.h" |
13 | 14 |
14 class BrowserWebKitClientImpl; | 15 class BrowserWebKitClientImpl; |
15 | 16 |
16 // This is an object that represents WebKit's "main" thread within the browser | 17 // This is an object that represents WebKit's "main" thread within the browser |
17 // process. You can create as many instances of this class as you'd like; | 18 // process. It should be instantiated and destroyed on the UI thread |
18 // they'll all point to the same thread and you're guaranteed they'll | 19 // before/after the IO thread is created/destroyed. All other usage should be |
19 // initialize in a thread-safe way, though WebKitThread instances should | 20 // on the IO thread. If the browser is being run in --single-process mode, a |
20 // probably be shared when it's easy to do so. The first time you call | 21 // thread will never be spun up, and GetMessageLoop() will always return NULL. |
21 // GetMessageLoop() or EnsureWebKitInitialized() the thread will be created | 22 class WebKitThread { |
22 // and WebKit initialized. When the last instance of WebKitThread is | |
23 // destroyed, WebKit is shut down and the thread is stopped. | |
24 // THIS CLASS MUST NOT BE DEREFED TO 0 ON THE WEBKIT THREAD (for now). | |
25 class WebKitThread : public base::RefCountedThreadSafe<WebKitThread> { | |
26 public: | 23 public: |
| 24 // Called from the UI thread. |
27 WebKitThread(); | 25 WebKitThread(); |
| 26 ~WebKitThread(); |
28 | 27 |
| 28 // Returns the message loop for the WebKit thread unless we're in |
| 29 // --single-processuntil mode, in which case it'll return NULL. Only call |
| 30 // from the IO thread. Only do fast-path work here. |
29 MessageLoop* GetMessageLoop() { | 31 MessageLoop* GetMessageLoop() { |
30 if (!cached_webkit_thread_) | 32 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
31 InitializeThread(); | 33 if (!webkit_thread_.get()) |
32 return cached_webkit_thread_->message_loop(); | 34 return InitializeThread(); |
33 } | 35 return webkit_thread_->message_loop(); |
34 | |
35 void EnsureWebKitInitialized() { | |
36 if (!cached_webkit_thread_) | |
37 InitializeThread(); | |
38 } | 36 } |
39 | 37 |
40 private: | 38 private: |
41 // Must be private so that we can carefully control its lifetime. | 39 // Must be private so that we can carefully control its lifetime. |
42 class InternalWebKitThread : public base::Thread { | 40 class InternalWebKitThread : public ChromeThread { |
43 public: | 41 public: |
44 InternalWebKitThread(); | 42 InternalWebKitThread(); |
45 virtual ~InternalWebKitThread() { } | 43 virtual ~InternalWebKitThread() { } |
46 // Does the actual initialization and shutdown of WebKit. Called at the | 44 // Does the actual initialization and shutdown of WebKit. Called at the |
47 // beginning and end of the thread's lifetime. | 45 // beginning and end of the thread's lifetime. |
48 virtual void Init(); | 46 virtual void Init(); |
49 virtual void CleanUp(); | 47 virtual void CleanUp(); |
50 | 48 |
51 private: | 49 private: |
52 BrowserWebKitClientImpl* webkit_client_; | 50 BrowserWebKitClientImpl* webkit_client_; |
53 }; | 51 }; |
54 | 52 |
55 friend class base::RefCountedThreadSafe<WebKitThread>; | 53 // Returns the WebKit thread's message loop or NULL if we're in |
56 ~WebKitThread(); | 54 // --single-process mode. Do slow-path initialization work here. |
| 55 MessageLoop* InitializeThread(); |
57 | 56 |
58 void InitializeThread(); | 57 // Pointer to the actual WebKitThread. NULL if not yet started. Only modify |
59 | 58 // from the IO thread while the WebKit thread is not running. |
60 // If this is set, then this object has incremented the global WebKit ref | 59 scoped_ptr<InternalWebKitThread> webkit_thread_; |
61 // count and will shutdown the thread if it sees the ref count go to 0. | |
62 // It's assumed that once this is non-NULL, the pointer will be valid until | |
63 // destruction. | |
64 InternalWebKitThread* cached_webkit_thread_; | |
65 | |
66 // If there are multiple WebKitThread object (should only be possible in | |
67 // unittests at the moment), make sure they all share one real thread. | |
68 static base::LazyInstance<Lock> global_webkit_lock_; | |
69 static int global_webkit_ref_count_; | |
70 static InternalWebKitThread* global_webkit_thread_; | |
71 | 60 |
72 DISALLOW_COPY_AND_ASSIGN(WebKitThread); | 61 DISALLOW_COPY_AND_ASSIGN(WebKitThread); |
73 }; | 62 }; |
74 | 63 |
75 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ | 64 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ |
OLD | NEW |