| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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. | |
| 4 | |
| 5 #include "chrome/common/appcache/appcache_dispatcher_host.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "chrome/browser/renderer_host/browser_render_process_host.h" | |
| 9 // TODO(eroman): uh oh, depending on stuff outside of common/ | |
| 10 #include "chrome/browser/net/chrome_url_request_context.h" | |
| 11 #include "chrome/common/appcache/chrome_appcache_service.h" | |
| 12 #include "chrome/common/render_messages.h" | |
| 13 | |
| 14 AppCacheDispatcherHost::AppCacheDispatcherHost( | |
| 15 URLRequestContextGetter* request_context_getter) | |
| 16 : request_context_getter_(request_context_getter), | |
| 17 process_handle_(0) { | |
| 18 DCHECK(request_context_getter_.get()); | |
| 19 } | |
| 20 | |
| 21 void AppCacheDispatcherHost::Initialize(IPC::Message::Sender* sender, | |
| 22 int process_id, base::ProcessHandle process_handle) { | |
| 23 DCHECK(sender); | |
| 24 DCHECK(process_handle && !process_handle_); | |
| 25 DCHECK(request_context_getter_.get()); | |
| 26 | |
| 27 process_handle_ = process_handle; | |
| 28 | |
| 29 // Get the AppCacheService (it can only be accessed from IO thread). | |
| 30 URLRequestContext* context = request_context_getter_->GetURLRequestContext(); | |
| 31 appcache_service_ = | |
| 32 static_cast<ChromeURLRequestContext*>(context)->appcache_service(); | |
| 33 request_context_getter_ = NULL; | |
| 34 | |
| 35 frontend_proxy_.set_sender(sender); | |
| 36 if (appcache_service_.get()) { | |
| 37 backend_impl_.Initialize( | |
| 38 appcache_service_.get(), &frontend_proxy_, process_id); | |
| 39 get_status_callback_.reset( | |
| 40 NewCallback(this, &AppCacheDispatcherHost::GetStatusCallback)); | |
| 41 start_update_callback_.reset( | |
| 42 NewCallback(this, &AppCacheDispatcherHost::StartUpdateCallback)); | |
| 43 swap_cache_callback_.reset( | |
| 44 NewCallback(this, &AppCacheDispatcherHost::SwapCacheCallback)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 bool AppCacheDispatcherHost::OnMessageReceived(const IPC::Message& msg, | |
| 49 bool *msg_ok) { | |
| 50 DCHECK(process_handle_); | |
| 51 *msg_ok = true; | |
| 52 bool handled = true; | |
| 53 IPC_BEGIN_MESSAGE_MAP_EX(AppCacheDispatcherHost, msg, *msg_ok) | |
| 54 IPC_MESSAGE_HANDLER(AppCacheMsg_RegisterHost, OnRegisterHost); | |
| 55 IPC_MESSAGE_HANDLER(AppCacheMsg_UnregisterHost, OnUnregisterHost); | |
| 56 IPC_MESSAGE_HANDLER(AppCacheMsg_SelectCache, OnSelectCache); | |
| 57 IPC_MESSAGE_HANDLER(AppCacheMsg_MarkAsForeignEntry, OnMarkAsForeignEntry); | |
| 58 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_GetStatus, OnGetStatus); | |
| 59 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_StartUpdate, OnStartUpdate); | |
| 60 IPC_MESSAGE_HANDLER_DELAY_REPLY(AppCacheMsg_SwapCache, OnSwapCache); | |
| 61 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 62 IPC_END_MESSAGE_MAP_EX() | |
| 63 return handled; | |
| 64 } | |
| 65 | |
| 66 void AppCacheDispatcherHost::OnRegisterHost(int host_id) { | |
| 67 if (appcache_service_.get()) { | |
| 68 if (!backend_impl_.RegisterHost(host_id)) { | |
| 69 ReceivedBadMessage(AppCacheMsg_RegisterHost::ID); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 void AppCacheDispatcherHost::OnUnregisterHost(int host_id) { | |
| 75 if (appcache_service_.get()) { | |
| 76 if (!backend_impl_.UnregisterHost(host_id)) { | |
| 77 ReceivedBadMessage(AppCacheMsg_UnregisterHost::ID); | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void AppCacheDispatcherHost::OnSelectCache( | |
| 83 int host_id, const GURL& document_url, | |
| 84 int64 cache_document_was_loaded_from, | |
| 85 const GURL& opt_manifest_url) { | |
| 86 if (appcache_service_.get()) { | |
| 87 if (!backend_impl_.SelectCache(host_id, document_url, | |
| 88 cache_document_was_loaded_from, | |
| 89 opt_manifest_url)) { | |
| 90 ReceivedBadMessage(AppCacheMsg_SelectCache::ID); | |
| 91 } | |
| 92 } else { | |
| 93 frontend_proxy_.OnCacheSelected( | |
| 94 host_id, appcache::kNoCacheId, appcache::UNCACHED); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void AppCacheDispatcherHost::OnMarkAsForeignEntry( | |
| 99 int host_id, const GURL& document_url, | |
| 100 int64 cache_document_was_loaded_from) { | |
| 101 if (appcache_service_.get()) { | |
| 102 if (!backend_impl_.MarkAsForeignEntry(host_id, document_url, | |
| 103 cache_document_was_loaded_from)) { | |
| 104 ReceivedBadMessage(AppCacheMsg_MarkAsForeignEntry::ID); | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void AppCacheDispatcherHost::OnGetStatus(int host_id, | |
| 110 IPC::Message* reply_msg) { | |
| 111 if (pending_reply_msg_.get()) { | |
| 112 ReceivedBadMessage(AppCacheMsg_GetStatus::ID); | |
| 113 delete reply_msg; | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 pending_reply_msg_.reset(reply_msg); | |
| 118 if (appcache_service_.get()) { | |
| 119 if (!backend_impl_.GetStatusWithCallback( | |
| 120 host_id, get_status_callback_.get(), reply_msg)) { | |
| 121 ReceivedBadMessage(AppCacheMsg_GetStatus::ID); | |
| 122 } | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 GetStatusCallback(appcache::UNCACHED, reply_msg); | |
| 127 } | |
| 128 | |
| 129 void AppCacheDispatcherHost::OnStartUpdate(int host_id, | |
| 130 IPC::Message* reply_msg) { | |
| 131 if (pending_reply_msg_.get()) { | |
| 132 ReceivedBadMessage(AppCacheMsg_StartUpdate::ID); | |
| 133 delete reply_msg; | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 pending_reply_msg_.reset(reply_msg); | |
| 138 if (appcache_service_.get()) { | |
| 139 if (!backend_impl_.StartUpdateWithCallback( | |
| 140 host_id, start_update_callback_.get(), reply_msg)) { | |
| 141 ReceivedBadMessage(AppCacheMsg_StartUpdate::ID); | |
| 142 } | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 StartUpdateCallback(false, reply_msg); | |
| 147 } | |
| 148 | |
| 149 void AppCacheDispatcherHost::OnSwapCache(int host_id, | |
| 150 IPC::Message* reply_msg) { | |
| 151 if (pending_reply_msg_.get()) { | |
| 152 ReceivedBadMessage(AppCacheMsg_SwapCache::ID); | |
| 153 delete reply_msg; | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 pending_reply_msg_.reset(reply_msg); | |
| 158 if (appcache_service_.get()) { | |
| 159 if (!backend_impl_.SwapCacheWithCallback( | |
| 160 host_id, swap_cache_callback_.get(), reply_msg)) { | |
| 161 ReceivedBadMessage(AppCacheMsg_SwapCache::ID); | |
| 162 } | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 SwapCacheCallback(false, reply_msg); | |
| 167 } | |
| 168 | |
| 169 void AppCacheDispatcherHost::GetStatusCallback( | |
| 170 appcache::Status status, void* param) { | |
| 171 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); | |
| 172 DCHECK(reply_msg == pending_reply_msg_.get()); | |
| 173 AppCacheMsg_GetStatus::WriteReplyParams(reply_msg, status); | |
| 174 frontend_proxy_.sender()->Send(pending_reply_msg_.release()); | |
| 175 } | |
| 176 | |
| 177 void AppCacheDispatcherHost::StartUpdateCallback(bool result, void* param) { | |
| 178 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); | |
| 179 DCHECK(reply_msg == pending_reply_msg_.get()); | |
| 180 AppCacheMsg_StartUpdate::WriteReplyParams(reply_msg, result); | |
| 181 frontend_proxy_.sender()->Send(pending_reply_msg_.release()); | |
| 182 } | |
| 183 | |
| 184 void AppCacheDispatcherHost::SwapCacheCallback(bool result, void* param) { | |
| 185 IPC::Message* reply_msg = reinterpret_cast<IPC::Message*>(param); | |
| 186 DCHECK(reply_msg == pending_reply_msg_.get()); | |
| 187 AppCacheMsg_SwapCache::WriteReplyParams(reply_msg, result); | |
| 188 frontend_proxy_.sender()->Send(pending_reply_msg_.release()); | |
| 189 } | |
| 190 | |
| 191 void AppCacheDispatcherHost::ReceivedBadMessage(uint32 msg_type) { | |
| 192 // TODO(michaeln): Consider gathering UMA stats | |
| 193 // http://code.google.com/p/chromium/issues/detail?id=24634 | |
| 194 BrowserRenderProcessHost::BadMessageTerminateProcess( | |
| 195 msg_type, process_handle_); | |
| 196 } | |
| OLD | NEW |