initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef CHROME_RENDERER_RENDER_THREAD_H__ |
| 31 | #define CHROME_RENDERER_RENDER_THREAD_H__ |
| 32 | |
| 33 | #include "base/ref_counted.h" |
| 34 | #include "base/shared_memory.h" |
| 35 | #include "base/task.h" |
| 36 | #include "base/thread.h" |
| 37 | #include "base/thread_local_storage.h" |
| 38 | #include "chrome/common/ipc_sync_channel.h" |
| 39 | #include "chrome/common/message_router.h" |
| 40 | |
| 41 | class SkBitmap; |
| 42 | class Task; |
| 43 | class VisitedLinkSlave; |
| 44 | struct WebPreferences; |
| 45 | class RenderDnsMaster; |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame^] | 46 | class NotificationService; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
| 48 | // The RenderThread class represents a background thread where RenderView |
| 49 | // instances live. The RenderThread supports an API that is used by its |
| 50 | // consumer to talk indirectly to the RenderViews and supporting objects. |
| 51 | // Likewise, it provides an API for the RenderViews to talk back to the main |
| 52 | // process (i.e., their corresponding WebContents). |
| 53 | // |
| 54 | // Most of the communication occurs in the form of IPC messages. They are |
| 55 | // routed to the RenderThread according to the routing IDs of the messages. |
| 56 | // The routing IDs correspond to RenderView instances. |
| 57 | |
| 58 | class RenderThread : public IPC::Channel::Listener, |
| 59 | public IPC::Message::Sender, |
| 60 | public Thread { |
| 61 | public: |
| 62 | RenderThread(const std::wstring& channel_name); |
| 63 | ~RenderThread(); |
| 64 | |
| 65 | // IPC::Channel::Listener implementation: |
| 66 | virtual void OnMessageReceived(const IPC::Message& msg); |
| 67 | virtual void OnChannelError(); |
| 68 | |
| 69 | // IPC::Message::Sender implementation: |
| 70 | virtual bool Send(IPC::Message* msg); |
| 71 | |
| 72 | void AddFilter(IPC::ChannelProxy::MessageFilter* filter); |
| 73 | void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter); |
| 74 | |
| 75 | // The RenderThread instance for the current thread. |
| 76 | static RenderThread* current() { |
| 77 | return static_cast<RenderThread*>(ThreadLocalStorage::Get(tls_index_)); |
| 78 | } |
| 79 | |
| 80 | VisitedLinkSlave* visited_link_slave() const { return visited_link_slave_; } |
| 81 | |
| 82 | // Do DNS prefetch resolution of a hostname. |
| 83 | void Resolve(const char* name, size_t length); |
| 84 | |
| 85 | // See documentation on MessageRouter for AddRoute and RemoveRoute |
| 86 | void AddRoute(int32 routing_id, IPC::Channel::Listener* listener); |
| 87 | void RemoveRoute(int32 routing_id); |
| 88 | |
| 89 | // Invokes InformHostOfCacheStats after a short delay. Used to move this |
| 90 | // bookkeeping operation off the critical latency path. |
| 91 | void InformHostOfCacheStatsLater(); |
| 92 | |
| 93 | MessageLoop* owner_loop() { return owner_loop_; } |
| 94 | |
| 95 | // Indicates if RenderThread::Send() is on the call stack. |
| 96 | bool in_send() const { return in_send_ != 0; } |
| 97 | |
| 98 | protected: |
| 99 | // Called by the thread base class |
| 100 | virtual void Init(); |
| 101 | virtual void CleanUp(); |
| 102 | |
| 103 | private: |
| 104 | void OnUpdateVisitedLinks(SharedMemoryHandle table); |
| 105 | |
| 106 | void OnSetNextPageID(int32 next_page_id); |
| 107 | void OnCreateNewView(HWND parent_hwnd, |
| 108 | HANDLE modal_dialog_event, |
| 109 | const WebPreferences& webkit_prefs, |
| 110 | int32 view_id); |
| 111 | void OnTransferBitmap(const SkBitmap& bitmap, int resource_id); |
| 112 | void OnSetCacheCapacities(size_t min_dead_capacity, |
| 113 | size_t max_dead_capacity, |
| 114 | size_t capacity); |
| 115 | void OnGetCacheResourceStats(); |
| 116 | |
| 117 | // Gather usage statistics from the in-memory cache and inform our host. |
| 118 | // These functions should be call periodically so that the host can make |
| 119 | // decisions about how to allocation resources using current information. |
| 120 | void InformHostOfCacheStats(); |
| 121 | |
| 122 | static DWORD tls_index_; |
| 123 | |
| 124 | // The message loop used to run tasks on the thread that started this thread. |
| 125 | MessageLoop* owner_loop_; |
| 126 | |
| 127 | // Used only on the background render thread to implement message routing |
| 128 | // functionality to the consumers of the RenderThread. |
| 129 | MessageRouter router_; |
| 130 | |
| 131 | std::wstring channel_name_; |
| 132 | scoped_ptr<IPC::SyncChannel> channel_; |
| 133 | |
| 134 | // These objects live solely on the render thread. |
| 135 | VisitedLinkSlave* visited_link_slave_; |
| 136 | |
| 137 | scoped_ptr<RenderDnsMaster> render_dns_master_; |
| 138 | |
| 139 | scoped_ptr<ScopedRunnableMethodFactory<RenderThread> > cache_stats_factory_; |
| 140 | |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame^] | 141 | scoped_ptr<NotificationService> notification_service_; |
| 142 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | int in_send_; |
| 144 | |
| 145 | DISALLOW_EVIL_CONSTRUCTORS(RenderThread); |
| 146 | }; |
| 147 | |
| 148 | #endif // CHROME_RENDERER_RENDER_THREAD_H__ |