blob: ca0226711bf987b7da35c4a9f5fd74ee13991831 [file] [log] [blame]
initial.commit09911bf2008-07-26 23:55:291// 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
41class SkBitmap;
42class Task;
43class VisitedLinkSlave;
44struct WebPreferences;
45class RenderDnsMaster;
46
47// The RenderThread class represents a background thread where RenderView
48// instances live. The RenderThread supports an API that is used by its
49// consumer to talk indirectly to the RenderViews and supporting objects.
50// Likewise, it provides an API for the RenderViews to talk back to the main
51// process (i.e., their corresponding WebContents).
52//
53// Most of the communication occurs in the form of IPC messages. They are
54// routed to the RenderThread according to the routing IDs of the messages.
55// The routing IDs correspond to RenderView instances.
56
57class RenderThread : public IPC::Channel::Listener,
58 public IPC::Message::Sender,
59 public Thread {
60 public:
61 RenderThread(const std::wstring& channel_name);
62 ~RenderThread();
63
64 // IPC::Channel::Listener implementation:
65 virtual void OnMessageReceived(const IPC::Message& msg);
66 virtual void OnChannelError();
67
68 // IPC::Message::Sender implementation:
69 virtual bool Send(IPC::Message* msg);
70
71 void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
72 void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter);
73
74 // The RenderThread instance for the current thread.
75 static RenderThread* current() {
76 return static_cast<RenderThread*>(ThreadLocalStorage::Get(tls_index_));
77 }
78
79 VisitedLinkSlave* visited_link_slave() const { return visited_link_slave_; }
80
81 // Do DNS prefetch resolution of a hostname.
82 void Resolve(const char* name, size_t length);
83
84 // See documentation on MessageRouter for AddRoute and RemoveRoute
85 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
86 void RemoveRoute(int32 routing_id);
87
88 // Invokes InformHostOfCacheStats after a short delay. Used to move this
89 // bookkeeping operation off the critical latency path.
90 void InformHostOfCacheStatsLater();
91
92 MessageLoop* owner_loop() { return owner_loop_; }
93
94 // Indicates if RenderThread::Send() is on the call stack.
95 bool in_send() const { return in_send_ != 0; }
96
97 protected:
98 // Called by the thread base class
99 virtual void Init();
100 virtual void CleanUp();
101
102 private:
103 void OnUpdateVisitedLinks(SharedMemoryHandle table);
104
105 void OnSetNextPageID(int32 next_page_id);
106 void OnCreateNewView(HWND parent_hwnd,
107 HANDLE modal_dialog_event,
108 const WebPreferences& webkit_prefs,
109 int32 view_id);
110 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
111 void OnSetCacheCapacities(size_t min_dead_capacity,
112 size_t max_dead_capacity,
113 size_t capacity);
114 void OnGetCacheResourceStats();
115
116 // Gather usage statistics from the in-memory cache and inform our host.
117 // These functions should be call periodically so that the host can make
118 // decisions about how to allocation resources using current information.
119 void InformHostOfCacheStats();
120
121 static DWORD tls_index_;
122
123 // The message loop used to run tasks on the thread that started this thread.
124 MessageLoop* owner_loop_;
125
126 // Used only on the background render thread to implement message routing
127 // functionality to the consumers of the RenderThread.
128 MessageRouter router_;
129
130 std::wstring channel_name_;
131 scoped_ptr<IPC::SyncChannel> channel_;
132
133 // These objects live solely on the render thread.
134 VisitedLinkSlave* visited_link_slave_;
135
136 scoped_ptr<RenderDnsMaster> render_dns_master_;
137
138 scoped_ptr<ScopedRunnableMethodFactory<RenderThread> > cache_stats_factory_;
139
140 int in_send_;
141
142 DISALLOW_EVIL_CONSTRUCTORS(RenderThread);
143};
144
145#endif // CHROME_RENDERER_RENDER_THREAD_H__