Chromium Code Reviews| Index: chrome/browser/in_process_webkit/dom_storage_message_filter.cc |
| =================================================================== |
| --- chrome/browser/in_process_webkit/dom_storage_message_filter.cc (revision 68139) |
| +++ chrome/browser/in_process_webkit/dom_storage_message_filter.cc (working copy) |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/in_process_webkit/dom_storage_dispatcher_host.h" |
| +#include "chrome/browser/in_process_webkit/dom_storage_message_filter.h" |
| #include "base/nullable_string16.h" |
| #include "chrome/browser/browser_thread.h" |
| @@ -10,259 +10,196 @@ |
| #include "chrome/browser/in_process_webkit/dom_storage_context.h" |
| #include "chrome/browser/in_process_webkit/dom_storage_namespace.h" |
| #include "chrome/browser/net/chrome_url_request_context.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/renderer_host/browser_render_process_host.h" |
| #include "chrome/browser/renderer_host/render_view_host_notification_task.h" |
| -#include "chrome/browser/renderer_host/resource_message_filter.h" |
| -#include "chrome/common/render_messages.h" |
| -#include "chrome/common/render_messages_params.h" |
| +#include "chrome/common/dom_storage_messages.h" |
| +#include "chrome/common/url_constants.h" |
| #include "googleurl/src/gurl.h" |
| using WebKit::WebStorageArea; |
| -DOMStorageDispatcherHost* DOMStorageDispatcherHost::storage_event_host_ = NULL; |
| -const GURL* DOMStorageDispatcherHost::storage_event_url_ = NULL; |
| +DOMStorageMessageFilter* DOMStorageMessageFilter::storage_event_message_filter = |
| + NULL; |
| +const GURL* DOMStorageMessageFilter::storage_event_url_ = NULL; |
| -DOMStorageDispatcherHost:: |
| +DOMStorageMessageFilter:: |
| ScopedStorageEventContext::ScopedStorageEventContext( |
| - DOMStorageDispatcherHost* dispatcher_host, const GURL* url) { |
| + DOMStorageMessageFilter* dispatcher_message_filter, const GURL* url) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| - DCHECK(!storage_event_host_); |
| + DCHECK(!storage_event_message_filter); |
| DCHECK(!storage_event_url_); |
| - storage_event_host_ = dispatcher_host; |
| + storage_event_message_filter = dispatcher_message_filter; |
| storage_event_url_ = url; |
| - DCHECK(storage_event_host_); |
| + DCHECK(storage_event_message_filter); |
| DCHECK(storage_event_url_); |
| } |
| -DOMStorageDispatcherHost:: |
| +DOMStorageMessageFilter:: |
| ScopedStorageEventContext::~ScopedStorageEventContext() { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| - DCHECK(storage_event_host_); |
| + DCHECK(storage_event_message_filter); |
| DCHECK(storage_event_url_); |
| - storage_event_host_ = NULL; |
| + storage_event_message_filter = NULL; |
| storage_event_url_ = NULL; |
| } |
| -DOMStorageDispatcherHost::DOMStorageDispatcherHost( |
| - ResourceMessageFilter* resource_message_filter, |
| - WebKitContext* webkit_context) |
| - : webkit_context_(webkit_context), |
| - resource_message_filter_(resource_message_filter), |
| - process_handle_(0), |
| - process_id_(0) { |
| - DCHECK(webkit_context_.get()); |
| - DCHECK(resource_message_filter_); |
| +DOMStorageMessageFilter::DOMStorageMessageFilter(int process_id, |
| + Profile* profile) |
| + : webkit_context_(profile->GetWebKitContext()), |
| + process_id_(process_id), |
| + request_context_(profile->GetRequestContext()), |
| + extensions_request_context_(profile->GetRequestContextForExtensions()) { |
| } |
| -DOMStorageDispatcherHost::~DOMStorageDispatcherHost() { |
| +DOMStorageMessageFilter::~DOMStorageMessageFilter() { |
| + // This is not always true during testing. |
| + if (peer_handle()) |
| + Context()->UnregisterMessageFilter(this); |
| } |
| -void DOMStorageDispatcherHost::Init(int process_id, |
| - base::ProcessHandle process_handle) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - DCHECK(resource_message_filter_); // Ensure Shutdown() has not been called. |
| - DCHECK(!process_handle_); // Make sure Init() has not yet been called. |
| - DCHECK(process_handle); |
| - Context()->RegisterDispatcherHost(this); |
| - process_id_ = process_id; |
| - process_handle_ = process_handle; |
| -} |
| +void DOMStorageMessageFilter::OnChannelConnected(int32 peer_pid) { |
| + BrowserMessageFilter::OnChannelConnected(peer_pid); |
| -void DOMStorageDispatcherHost::Shutdown() { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - // This is not always true during testing. |
| - if (process_handle_) |
| - Context()->UnregisterDispatcherHost(this); |
| - resource_message_filter_ = NULL; |
| + Context()->RegisterMessageFilter(this); |
| } |
| /* static */ |
| -void DOMStorageDispatcherHost::DispatchStorageEvent(const NullableString16& key, |
| +void DOMStorageMessageFilter::DispatchStorageEvent(const NullableString16& key, |
| const NullableString16& old_value, const NullableString16& new_value, |
| const string16& origin, const GURL& url, bool is_local_storage) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DCHECK(is_local_storage); // Only LocalStorage is implemented right now. |
| - DCHECK(storage_event_host_); |
| - ViewMsg_DOMStorageEvent_Params params; |
| - params.key_ = key; |
| - params.old_value_ = old_value; |
| - params.new_value_ = new_value; |
| - params.origin_ = origin; |
| - params.url_ = *storage_event_url_; // The url passed in is junk. |
| - params.storage_type_ = is_local_storage ? DOM_STORAGE_LOCAL |
| - : DOM_STORAGE_SESSION; |
| - // The storage_event_host_ is the DOMStorageDispatcherHost that is up in the |
| - // current call stack since it caused the storage event to fire. |
| + DCHECK(storage_event_message_filter); |
| + DOMStorageMsg_Event_Params params; |
| + params.key = key; |
| + params.old_value = old_value; |
| + params.new_value = new_value; |
| + params.origin = origin; |
| + params.url = *storage_event_url_; // The url passed in is junk. |
| + params.storage_type = is_local_storage ? DOM_STORAGE_LOCAL |
| + : DOM_STORAGE_SESSION; |
| + // The storage_event_message_filter is the DOMStorageMessageFilter that is up |
| + // in the current call stack since it caused the storage event to fire. |
| BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| - NewRunnableMethod(storage_event_host_, |
| - &DOMStorageDispatcherHost::OnStorageEvent, params)); |
| + NewRunnableMethod(storage_event_message_filter, |
| + &DOMStorageMessageFilter::OnStorageEvent, params)); |
| } |
| -bool DOMStorageDispatcherHost::OnMessageReceived(const IPC::Message& message, |
| - bool* msg_is_ok) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - DCHECK(process_handle_); |
| - |
| +bool DOMStorageMessageFilter::OnMessageReceived(const IPC::Message& message, |
| + bool* message_was_ok) { |
| bool handled = true; |
| - IPC_BEGIN_MESSAGE_MAP_EX(DOMStorageDispatcherHost, message, *msg_is_ok) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageStorageAreaId, |
| + IPC_BEGIN_MESSAGE_MAP_EX(DOMStorageMessageFilter, message, *message_was_ok) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_StorageAreaId, |
| OnStorageAreaId) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageLength, OnLength) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageKey, OnKey) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageGetItem, OnGetItem) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageSetItem, OnSetItem) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageRemoveItem, |
| - OnRemoveItem) |
| - IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_DOMStorageClear, OnClear) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_Length, OnLength) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_Key, OnKey) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_GetItem, OnGetItem) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_SetItem, OnSetItem) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_RemoveItem, OnRemoveItem) |
| + IPC_MESSAGE_HANDLER_DELAY_REPLY(DOMStorageHostMsg_Clear, OnClear) |
| IPC_MESSAGE_UNHANDLED(handled = false) |
| IPC_END_MESSAGE_MAP() |
| + |
| return handled; |
| } |
| -int64 DOMStorageDispatcherHost::CloneSessionStorage(int64 original_id) { |
| - return Context()->CloneSessionStorage(original_id); |
| -} |
| - |
| -void DOMStorageDispatcherHost::Send(IPC::Message* message) { |
| - if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - // TODO(jorlow): Even if we successfully post, I believe it's possible for |
| - // the task to never run (if the IO thread is already shutting |
| - // down). We may want to handle this case, though |
| - // realistically it probably doesn't matter. |
| - if (!BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::Send, message))) { |
| - // The IO thread is dead. |
| - delete message; |
| - } |
| - return; |
| +void DOMStorageMessageFilter::OverrideThreadForMessage( |
| + const IPC::Message& message, |
| + BrowserThread::ID* thread) { |
| + if (IPC_MESSAGE_CLASS(message) == DOMStorageMsgStart && |
| + message.type() != DOMStorageHostMsg_StorageAreaId::ID) { |
| + *thread = BrowserThread::WEBKIT; |
| } |
| - |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - if (!resource_message_filter_) |
| - delete message; |
| - else |
| - resource_message_filter_->Send(message); |
| } |
| -void DOMStorageDispatcherHost::OnStorageAreaId(int64 namespace_id, |
| - const string16& origin, |
| - IPC::Message* reply_msg) { |
| +void DOMStorageMessageFilter::OnStorageAreaId(int64 namespace_id, |
| + const string16& origin, |
| + IPC::Message* reply_msg) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| ChromeURLRequestContext* url_request_context = |
| - resource_message_filter_->GetRequestContextForURL(GURL(origin)); |
| + GetRequestContextForURL(GURL(origin)); |
| BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnStorageAreaIdWebKit, namespace_id, |
| + this, &DOMStorageMessageFilter::OnStorageAreaIdWebKit, namespace_id, |
| origin, reply_msg, |
| make_scoped_refptr(url_request_context->host_content_settings_map()))); |
|
jorlow
2010/12/13 17:56:52
I think you can just get the map from the profile
|
| } |
| -void DOMStorageDispatcherHost::OnStorageAreaIdWebKit( |
| +void DOMStorageMessageFilter::OnStorageAreaIdWebKit( |
| int64 namespace_id, const string16& origin, IPC::Message* reply_msg, |
| HostContentSettingsMap* host_content_settings_map) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageNamespace* storage_namespace = |
| Context()->GetStorageNamespace(namespace_id, true); |
| if (!storage_namespace) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageStorageAreaId::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_StorageAreaId::ID); |
| delete reply_msg; |
| return; |
| } |
| DOMStorageArea* storage_area = storage_namespace->GetStorageArea( |
| origin, host_content_settings_map); |
| - ViewHostMsg_DOMStorageStorageAreaId::WriteReplyParams(reply_msg, |
| - storage_area->id()); |
| + DOMStorageHostMsg_StorageAreaId::WriteReplyParams(reply_msg, |
| + storage_area->id()); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnLength(int64 storage_area_id, |
| - IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnLength, storage_area_id, reply_msg)); |
| - return; |
| - } |
| - |
| +void DOMStorageMessageFilter::OnLength(int64 storage_area_id, |
| + IPC::Message* reply_msg) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageLength::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_Length::ID); |
| delete reply_msg; |
| return; |
| } |
| unsigned length = storage_area->Length(); |
| - ViewHostMsg_DOMStorageLength::WriteReplyParams(reply_msg, length); |
| + DOMStorageHostMsg_Length::WriteReplyParams(reply_msg, length); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnKey(int64 storage_area_id, unsigned index, |
| - IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnKey, storage_area_id, index, |
| - reply_msg)); |
| - return; |
| - } |
| - |
| +void DOMStorageMessageFilter::OnKey(int64 storage_area_id, unsigned index, |
| + IPC::Message* reply_msg) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageKey::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_Key::ID); |
| delete reply_msg; |
| return; |
| } |
| const NullableString16& key = storage_area->Key(index); |
| - ViewHostMsg_DOMStorageKey::WriteReplyParams(reply_msg, key); |
| + DOMStorageHostMsg_Key::WriteReplyParams(reply_msg, key); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnGetItem(int64 storage_area_id, |
| - const string16& key, |
| - IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnGetItem, storage_area_id, key, |
| - reply_msg)); |
| - return; |
| - } |
| - |
| +void DOMStorageMessageFilter::OnGetItem(int64 storage_area_id, |
| + const string16& key, |
| + IPC::Message* reply_msg) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageGetItem::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_GetItem::ID); |
| delete reply_msg; |
| return; |
| } |
| const NullableString16& value = storage_area->GetItem(key); |
| - ViewHostMsg_DOMStorageGetItem::WriteReplyParams(reply_msg, value); |
| + DOMStorageHostMsg_GetItem::WriteReplyParams(reply_msg, value); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnSetItem( |
| +void DOMStorageMessageFilter::OnSetItem( |
| int64 storage_area_id, const string16& key, const string16& value, |
| const GURL& url, IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnSetItem, storage_area_id, key, value, |
| - url, reply_msg)); |
| - return; |
| - } |
| - |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageSetItem::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_SetItem::ID); |
| + delete reply_msg; |
| return; |
| } |
| ScopedStorageEventContext scope(this, &url); |
| WebStorageArea::Result result; |
| - NullableString16 old_value = storage_area->SetItem(key, value, &result, this); |
| + NullableString16 old_value = storage_area->SetItem(key, value, &result); |
| // If content was blocked, tell the UI to display the blocked content icon. |
| if (reply_msg->routing_id() == MSG_ROUTING_CONTROL) { |
| @@ -275,67 +212,65 @@ |
| result == WebStorageArea::ResultBlockedByPolicy); |
| } |
| - ViewHostMsg_DOMStorageSetItem::WriteReplyParams(reply_msg, result, old_value); |
| + DOMStorageHostMsg_SetItem::WriteReplyParams(reply_msg, result, old_value); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnRemoveItem( |
| +void DOMStorageMessageFilter::OnRemoveItem( |
| int64 storage_area_id, const string16& key, const GURL& url, |
| IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnRemoveItem, storage_area_id, key, |
| - url, reply_msg)); |
| - return; |
| - } |
| - |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageRemoveItem::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_RemoveItem::ID); |
| + delete reply_msg; |
| return; |
| } |
| ScopedStorageEventContext scope(this, &url); |
| NullableString16 old_value = storage_area->RemoveItem(key); |
| - ViewHostMsg_DOMStorageRemoveItem::WriteReplyParams(reply_msg, old_value); |
| + DOMStorageHostMsg_RemoveItem::WriteReplyParams(reply_msg, old_value); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnClear(int64 storage_area_id, const GURL& url, |
| - IPC::Message* reply_msg) { |
| - if (BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| - BrowserThread::PostTask(BrowserThread::WEBKIT, FROM_HERE, NewRunnableMethod( |
| - this, &DOMStorageDispatcherHost::OnClear, storage_area_id, url, |
| - reply_msg)); |
| - return; |
| - } |
| - |
| +void DOMStorageMessageFilter::OnClear(int64 storage_area_id, const GURL& url, |
| + IPC::Message* reply_msg) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| DOMStorageArea* storage_area = Context()->GetStorageArea(storage_area_id); |
| if (!storage_area) { |
| - BrowserRenderProcessHost::BadMessageTerminateProcess( |
| - ViewHostMsg_DOMStorageClear::ID, process_handle_); |
| + BadMessageReceived(DOMStorageHostMsg_Clear::ID); |
| + delete reply_msg; |
| return; |
| } |
| ScopedStorageEventContext scope(this, &url); |
| bool something_cleared = storage_area->Clear(); |
| - ViewHostMsg_DOMStorageClear::WriteReplyParams(reply_msg, something_cleared); |
| + DOMStorageHostMsg_Clear::WriteReplyParams(reply_msg, something_cleared); |
| Send(reply_msg); |
| } |
| -void DOMStorageDispatcherHost::OnStorageEvent( |
| - const ViewMsg_DOMStorageEvent_Params& params) { |
| +void DOMStorageMessageFilter::OnStorageEvent( |
| + const DOMStorageMsg_Event_Params& params) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - const DOMStorageContext::DispatcherHostSet* set = |
| - Context()->GetDispatcherHostSet(); |
| - DOMStorageContext::DispatcherHostSet::const_iterator cur = set->begin(); |
| + const DOMStorageContext::MessageFilterSet* set = |
| + Context()->GetMessageFilterSet(); |
| + DOMStorageContext::MessageFilterSet::const_iterator cur = set->begin(); |
| while (cur != set->end()) { |
| // The renderer that generates the event handles it itself. |
| if (*cur != this) |
| - (*cur)->Send(new ViewMsg_DOMStorageEvent(params)); |
| + (*cur)->Send(new DOMStorageMsg_Event(params)); |
| ++cur; |
| } |
| } |
| + |
| +// This small function is unfortunately a duplicate of what's in |
| +// ResourceMessageFilter, but it's better than having a dependency on that large |
| +// class. |
| +ChromeURLRequestContext* DOMStorageMessageFilter::GetRequestContextForURL( |
|
jorlow
2010/12/13 17:56:52
I think we can get rid of this.
|
| + const GURL& url) { |
| + URLRequestContextGetter* context_getter = |
| + url.SchemeIs(chrome::kExtensionScheme) ? |
| + extensions_request_context_ : request_context_; |
| + return static_cast<ChromeURLRequestContext*>( |
| + context_getter->GetURLRequestContext()); |
| +} |