| 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 #include "chrome/browser/chrome_thread.h" |
| 14 | 14 |
| 15 class BrowserWebKitClientImpl; | 15 class BrowserWebKitClientImpl; |
| 16 | 16 |
| 17 // 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 |
| 18 // process. It should be instantiated and destroyed on the UI thread | 18 // process. It should be instantiated and destroyed on the UI thread |
| 19 // before/after the IO thread is created/destroyed. All other usage should be | 19 // before/after the IO thread is created/destroyed. All other usage should be |
| 20 // on the IO thread. If the browser is being run in --single-process mode, a | 20 // on the IO thread. If the browser is being run in --single-process mode, a |
| 21 // thread will never be spun up, and GetMessageLoop() will always return NULL. | 21 // thread will never be spun up. |
| 22 class WebKitThread { | 22 class WebKitThread { |
| 23 public: | 23 public: |
| 24 // Called from the UI thread. | 24 // Called from the UI thread. |
| 25 WebKitThread(); | 25 WebKitThread(); |
| 26 ~WebKitThread(); | 26 ~WebKitThread(); |
| 27 | 27 |
| 28 // Returns the message loop for the WebKit thread unless we're in | 28 // Creates the WebKit thread if it hasn't been already created. Only call |
| 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 // from the IO thread. Only do fast-path work here. |
| 31 MessageLoop* GetMessageLoop() { | 30 void EnsureInitialized(); |
| 32 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
| 33 if (!webkit_thread_.get()) | |
| 34 return InitializeThread(); | |
| 35 return webkit_thread_->message_loop(); | |
| 36 } | |
| 37 | |
| 38 // Called from the IO thread. Notifies us that it's no longer safe to post | |
| 39 // tasks to the IO thread. | |
| 40 void Shutdown(); | |
| 41 | |
| 42 // Post a task to the IO thread if we haven't yet been told to shut down. | |
| 43 // Only call from the WebKit thread. | |
| 44 bool PostIOThreadTask(const tracked_objects::Location& from_here, | |
| 45 Task* task); | |
| 46 | 31 |
| 47 private: | 32 private: |
| 48 // Must be private so that we can carefully control its lifetime. | 33 // Must be private so that we can carefully control its lifetime. |
| 49 class InternalWebKitThread : public ChromeThread { | 34 class InternalWebKitThread : public ChromeThread { |
| 50 public: | 35 public: |
| 51 InternalWebKitThread(); | 36 InternalWebKitThread(); |
| 52 virtual ~InternalWebKitThread(); | 37 virtual ~InternalWebKitThread(); |
| 53 // Does the actual initialization and shutdown of WebKit. Called at the | 38 // Does the actual initialization and shutdown of WebKit. Called at the |
| 54 // beginning and end of the thread's lifetime. | 39 // beginning and end of the thread's lifetime. |
| 55 virtual void Init(); | 40 virtual void Init(); |
| 56 virtual void CleanUp(); | 41 virtual void CleanUp(); |
| 57 | 42 |
| 58 private: | 43 private: |
| 59 // The WebKitClient implementation. Only access on WebKit thread. | 44 // The WebKitClient implementation. Only access on WebKit thread. |
| 60 scoped_ptr<BrowserWebKitClientImpl> webkit_client_; | 45 scoped_ptr<BrowserWebKitClientImpl> webkit_client_; |
| 61 }; | 46 }; |
| 62 | 47 |
| 63 // Returns the WebKit thread's message loop or NULL if we're in | 48 // Returns the WebKit thread's message loop or NULL if we're in |
| 64 // --single-process mode. Do slow-path initialization work here. | 49 // --single-process mode. Do slow-path initialization work here. |
| 65 MessageLoop* InitializeThread(); | 50 MessageLoop* InitializeThread(); |
| 66 | 51 |
| 67 // Pointer to the actual WebKitThread. NULL if not yet started. Only modify | 52 // Pointer to the actual WebKitThread. NULL if not yet started. Only modify |
| 68 // from the IO thread while the WebKit thread is not running. | 53 // from the IO thread while the WebKit thread is not running. |
| 69 scoped_ptr<InternalWebKitThread> webkit_thread_; | 54 scoped_ptr<InternalWebKitThread> webkit_thread_; |
| 70 | 55 |
| 71 // A pointer to the IO message loop. This is nulled out when Shutdown() is | |
| 72 // called. Only access under the io_message_loop_lock_. | |
| 73 MessageLoop* io_message_loop_; | |
| 74 Lock io_message_loop_lock_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(WebKitThread); | 56 DISALLOW_COPY_AND_ASSIGN(WebKitThread); |
| 77 }; | 57 }; |
| 78 | 58 |
| 79 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ | 59 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_WEBKIT_THREAD_H_ |
| OLD | NEW |