Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(544)

Side by Side Diff: content/child/quota_message_filter.cc

Issue 63843002: Add ChildMessageFilter, a base class for renderer/worker cross-thread MessageFilter (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/quota_message_filter.h" 5 #include "content/child/quota_message_filter.h"
6 6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/pickle.h"
11 #include "content/child/quota_dispatcher.h" 7 #include "content/child/quota_dispatcher.h"
12 #include "content/child/thread_safe_sender.h" 8 #include "content/child/thread_safe_sender.h"
13 #include "content/common/quota_messages.h" 9 #include "content/common/quota_messages.h"
14 #include "webkit/child/worker_task_runner.h"
15
16 using webkit_glue::WorkerTaskRunner;
17 10
18 namespace content { 11 namespace content {
19 12
20 QuotaMessageFilter::QuotaMessageFilter( 13 QuotaMessageFilter::QuotaMessageFilter(
21 ThreadSafeSender* thread_safe_sender) 14 ThreadSafeSender* thread_safe_sender)
22 : main_thread_loop_proxy_(base::MessageLoopProxy::current()), 15 : thread_safe_sender_(thread_safe_sender),
23 thread_safe_sender_(thread_safe_sender),
24 next_request_id_(0) { 16 next_request_id_(0) {
25 } 17 }
26 18
27 bool QuotaMessageFilter::OnMessageReceived(const IPC::Message& msg) { 19 QuotaMessageFilter::~QuotaMessageFilter() {}
28 if (IPC_MESSAGE_CLASS(msg) != QuotaMsgStart)
29 return false;
30 int request_id = -1;
31 bool result = PickleIterator(msg).ReadInt(&request_id);
32 DCHECK(result);
33 base::Closure closure = base::Bind(
34 &QuotaMessageFilter::DispatchMessage, this, msg);
35 int thread_id = 0;
36 {
37 base::AutoLock lock(request_id_map_lock_);
38 RequestIdToThreadId::iterator found = request_id_map_.find(request_id);
39 if (found != request_id_map_.end()) {
40 thread_id = found->second;
41 request_id_map_.erase(found);
42 }
43 }
44 if (!thread_id) {
45 main_thread_loop_proxy_->PostTask(FROM_HERE, closure);
46 return true;
47 }
48 WorkerTaskRunner::Instance()->PostTask(thread_id, closure);
49 return true;
50 }
51 20
52 int QuotaMessageFilter::GenerateRequestID(int thread_id) { 21 int QuotaMessageFilter::GenerateRequestID(int thread_id) {
53 base::AutoLock lock(request_id_map_lock_); 22 base::AutoLock lock(request_id_map_lock_);
54 request_id_map_[next_request_id_] = thread_id; 23 request_id_map_[next_request_id_] = thread_id;
55 return next_request_id_++; 24 return next_request_id_++;
56 } 25 }
57 26
58 void QuotaMessageFilter::ClearThreadRequests(int thread_id) { 27 void QuotaMessageFilter::ClearThreadRequests(int thread_id) {
59 base::AutoLock lock(request_id_map_lock_); 28 base::AutoLock lock(request_id_map_lock_);
60 for (RequestIdToThreadId::iterator iter = request_id_map_.begin(); 29 for (RequestIdToThreadId::iterator iter = request_id_map_.begin();
61 iter != request_id_map_.end();) { 30 iter != request_id_map_.end();) {
62 if (iter->second == thread_id) 31 if (iter->second == thread_id)
63 request_id_map_.erase(iter++); 32 request_id_map_.erase(iter++);
64 else 33 else
65 iter++; 34 iter++;
66 } 35 }
67 } 36 }
68 37
69 QuotaMessageFilter::~QuotaMessageFilter() {} 38 bool QuotaMessageFilter::OverrideThreadIDForMessage(const IPC::Message& msg,
39 int* thread_id) {
40 if (IPC_MESSAGE_CLASS(msg) != QuotaMsgStart)
41 return false;
70 42
71 void QuotaMessageFilter::DispatchMessage(const IPC::Message& msg) { 43 int request_id = -1;
44 if (!PickleIterator(msg).ReadInt(&request_id))
45 return false;
46
47 base::AutoLock lock(request_id_map_lock_);
48 RequestIdToThreadId::iterator found = request_id_map_.find(request_id);
49 if (found != request_id_map_.end()) {
50 *thread_id = found->second;
51 request_id_map_.erase(found);
52 }
53 return true;
54 }
55
56 bool QuotaMessageFilter::OnMessageReceived(const IPC::Message& msg) {
57 if (IPC_MESSAGE_CLASS(msg) != QuotaMsgStart)
58 return false;
72 QuotaDispatcher::ThreadSpecificInstance(thread_safe_sender_.get(), this) 59 QuotaDispatcher::ThreadSpecificInstance(thread_safe_sender_.get(), this)
73 ->OnMessageReceived(msg); 60 ->OnMessageReceived(msg);
61 return true;
74 } 62 }
75 63
76 } // namespace content 64 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698