1 // Copyright (c) 2012 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.
5 #include "content/browser/appcache/appcache_dispatcher_host.h"
8 #include "base/bind_helpers.h"
9 #include "content/browser/appcache/chrome_appcache_service.h"
10 #include "content/browser/bad_message.h"
11 #include "content/common/appcache_messages.h"
12 #include "content/public/browser/user_metrics.h"
16 AppCacheDispatcherHost::AppCacheDispatcherHost(
17 ChromeAppCacheService
* appcache_service
,
19 : BrowserMessageFilter(AppCacheMsgStart
),
20 appcache_service_(appcache_service
),
21 frontend_proxy_(this),
22 process_id_(process_id
) {
25 void AppCacheDispatcherHost::OnChannelConnected(int32 peer_pid
) {
26 if (appcache_service_
.get()) {
27 backend_impl_
.Initialize(
28 appcache_service_
.get(), &frontend_proxy_
, process_id_
);
29 get_status_callback_
=
30 base::Bind(&AppCacheDispatcherHost::GetStatusCallback
,
31 base::Unretained(this));
32 start_update_callback_
=
33 base::Bind(&AppCacheDispatcherHost::StartUpdateCallback
,
34 base::Unretained(this));
35 swap_cache_callback_
=
36 base::Bind(&AppCacheDispatcherHost::SwapCacheCallback
,
37 base::Unretained(this));
41 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message
& message
) {
43 IPC_BEGIN_MESSAGE_MAP(AppCacheDispatcherHost
, message
)
44 IPC_MESSAGE_HANDLER(AppCacheHostMsg_RegisterHost
, OnRegisterHost
)
45 IPC_MESSAGE_HANDLER(AppCacheHostMsg_UnregisterHost
, OnUnregisterHost
)
46 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SetSpawningHostId
, OnSetSpawningHostId
)
47 IPC_MESSAGE_HANDLER(AppCacheHostMsg_GetResourceList
, OnGetResourceList
)
48 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCache
, OnSelectCache
)
49 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForWorker
,
50 OnSelectCacheForWorker
)
51 IPC_MESSAGE_HANDLER(AppCacheHostMsg_SelectCacheForSharedWorker
,
52 OnSelectCacheForSharedWorker
)
53 IPC_MESSAGE_HANDLER(AppCacheHostMsg_MarkAsForeignEntry
,
55 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_GetStatus
, OnGetStatus
)
56 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_StartUpdate
, OnStartUpdate
)
57 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheHostMsg_SwapCache
, OnSwapCache
)
58 IPC_MESSAGE_UNHANDLED(handled
= false)
64 AppCacheDispatcherHost::~AppCacheDispatcherHost() {}
66 void AppCacheDispatcherHost::OnRegisterHost(int host_id
) {
67 if (appcache_service_
.get()) {
68 if (!backend_impl_
.RegisterHost(host_id
)) {
69 bad_message::ReceivedBadMessage(this, bad_message::ACDH_REGISTER
);
74 void AppCacheDispatcherHost::OnUnregisterHost(int host_id
) {
75 if (appcache_service_
.get()) {
76 if (!backend_impl_
.UnregisterHost(host_id
)) {
77 bad_message::ReceivedBadMessage(this, bad_message::ACDH_UNREGISTER
);
82 void AppCacheDispatcherHost::OnSetSpawningHostId(
83 int host_id
, int spawning_host_id
) {
84 if (appcache_service_
.get()) {
85 if (!backend_impl_
.SetSpawningHostId(host_id
, spawning_host_id
))
86 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SET_SPAWNING
);
90 void AppCacheDispatcherHost::OnSelectCache(
91 int host_id
, const GURL
& document_url
,
92 int64 cache_document_was_loaded_from
,
93 const GURL
& opt_manifest_url
) {
94 if (appcache_service_
.get()) {
95 if (!backend_impl_
.SelectCache(host_id
,
97 cache_document_was_loaded_from
,
99 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SELECT_CACHE
);
102 frontend_proxy_
.OnCacheSelected(host_id
, AppCacheInfo());
106 void AppCacheDispatcherHost::OnSelectCacheForWorker(
107 int host_id
, int parent_process_id
, int parent_host_id
) {
108 if (appcache_service_
.get()) {
109 if (!backend_impl_
.SelectCacheForWorker(
110 host_id
, parent_process_id
, parent_host_id
)) {
111 bad_message::ReceivedBadMessage(
112 this, bad_message::ACDH_SELECT_CACHE_FOR_WORKER
);
115 frontend_proxy_
.OnCacheSelected(host_id
, AppCacheInfo());
119 void AppCacheDispatcherHost::OnSelectCacheForSharedWorker(
120 int host_id
, int64 appcache_id
) {
121 if (appcache_service_
.get()) {
122 if (!backend_impl_
.SelectCacheForSharedWorker(host_id
, appcache_id
))
123 bad_message::ReceivedBadMessage(
124 this, bad_message::ACDH_SELECT_CACHE_FOR_SHARED_WORKER
);
126 frontend_proxy_
.OnCacheSelected(host_id
, AppCacheInfo());
130 void AppCacheDispatcherHost::OnMarkAsForeignEntry(
131 int host_id
, const GURL
& document_url
,
132 int64 cache_document_was_loaded_from
) {
133 if (appcache_service_
.get()) {
134 if (!backend_impl_
.MarkAsForeignEntry(
135 host_id
, document_url
, cache_document_was_loaded_from
)) {
136 bad_message::ReceivedBadMessage(this,
137 bad_message::ACDH_MARK_AS_FOREIGN_ENTRY
);
142 void AppCacheDispatcherHost::OnGetResourceList(
143 int host_id
, std::vector
<AppCacheResourceInfo
>* params
) {
144 if (appcache_service_
.get())
145 backend_impl_
.GetResourceList(host_id
, params
);
148 void AppCacheDispatcherHost::OnGetStatus(int host_id
, IPC::Message
* reply_msg
) {
149 if (pending_reply_msg_
) {
150 bad_message::ReceivedBadMessage(
151 this, bad_message::ACDH_PENDING_REPLY_IN_GET_STATUS
);
156 pending_reply_msg_
.reset(reply_msg
);
157 if (appcache_service_
.get()) {
158 if (!backend_impl_
.GetStatusWithCallback(
159 host_id
, get_status_callback_
, reply_msg
)) {
160 bad_message::ReceivedBadMessage(this, bad_message::ACDH_GET_STATUS
);
165 GetStatusCallback(APPCACHE_STATUS_UNCACHED
, reply_msg
);
168 void AppCacheDispatcherHost::OnStartUpdate(int host_id
,
169 IPC::Message
* reply_msg
) {
170 if (pending_reply_msg_
) {
171 bad_message::ReceivedBadMessage(
172 this, bad_message::ACDH_PENDING_REPLY_IN_START_UPDATE
);
177 pending_reply_msg_
.reset(reply_msg
);
178 if (appcache_service_
.get()) {
179 if (!backend_impl_
.StartUpdateWithCallback(
180 host_id
, start_update_callback_
, reply_msg
)) {
181 bad_message::ReceivedBadMessage(this, bad_message::ACDH_START_UPDATE
);
186 StartUpdateCallback(false, reply_msg
);
189 void AppCacheDispatcherHost::OnSwapCache(int host_id
, IPC::Message
* reply_msg
) {
190 if (pending_reply_msg_
) {
191 bad_message::ReceivedBadMessage(
192 this, bad_message::ACDH_PENDING_REPLY_IN_SWAP_CACHE
);
197 pending_reply_msg_
.reset(reply_msg
);
198 if (appcache_service_
.get()) {
199 if (!backend_impl_
.SwapCacheWithCallback(
200 host_id
, swap_cache_callback_
, reply_msg
)) {
201 bad_message::ReceivedBadMessage(this, bad_message::ACDH_SWAP_CACHE
);
206 SwapCacheCallback(false, reply_msg
);
209 void AppCacheDispatcherHost::GetStatusCallback(
210 AppCacheStatus status
, void* param
) {
211 IPC::Message
* reply_msg
= reinterpret_cast<IPC::Message
*>(param
);
212 DCHECK_EQ(pending_reply_msg_
.get(), reply_msg
);
213 AppCacheHostMsg_GetStatus::WriteReplyParams(reply_msg
, status
);
214 Send(pending_reply_msg_
.release());
217 void AppCacheDispatcherHost::StartUpdateCallback(bool result
, void* param
) {
218 IPC::Message
* reply_msg
= reinterpret_cast<IPC::Message
*>(param
);
219 DCHECK_EQ(pending_reply_msg_
.get(), reply_msg
);
220 AppCacheHostMsg_StartUpdate::WriteReplyParams(reply_msg
, result
);
221 Send(pending_reply_msg_
.release());
224 void AppCacheDispatcherHost::SwapCacheCallback(bool result
, void* param
) {
225 IPC::Message
* reply_msg
= reinterpret_cast<IPC::Message
*>(param
);
226 DCHECK_EQ(pending_reply_msg_
.get(), reply_msg
);
227 AppCacheHostMsg_SwapCache::WriteReplyParams(reply_msg
, result
);
228 Send(pending_reply_msg_
.release());
231 } // namespace content