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. 4 #ifndef CHROME_FRAME_TEST_NET_TEST_AUTOMATION_RESOURCE_MESSAGE_FILTER_H_ 5 #define CHROME_FRAME_TEST_NET_TEST_AUTOMATION_RESOURCE_MESSAGE_FILTER_H_ 6 7 #include <map> 8 9 #include "base/synchronization/lock.h" 10 #include "chrome/browser/automation/automation_provider.h" 11 #include "chrome/browser/automation/automation_resource_message_filter.h" 12 #include "chrome/browser/automation/url_request_automation_job.h" 13 14 // Performs the same duties as AutomationResourceMessageFilter but with one 15 // difference. Instead of being tied to an IPC channel running on Chrome's 16 // IO thread, this instance runs on the unit test's IO thread (all URL request 17 // tests have their own IO loop) and is tied to an instance of 18 // AutomationProvider (TestAutomationProvider to be exact). 19 // 20 // Messages from the AutomationProvider that are destined to request objects 21 // owned by this class are marshaled over to the request's IO thread instead 22 // of using the thread the messages are received on. This way we allow the 23 // URL request tests to run sequentially as they were written while still 24 // allowing the automation layer to work as it normally does (i.e. process 25 // its messages on the receiving thread). 26 class TestAutomationResourceMessageFilter 27 : public AutomationResourceMessageFilter { 28 public: 29 explicit TestAutomationResourceMessageFilter(AutomationProvider* automation); 30 31 virtual bool Send(IPC::Message* message); 32 33 static void OnRequestMessage(URLRequestAutomationJob* job, 34 IPC::Message* msg); 35 36 virtual bool OnMessageReceived(const IPC::Message& message); 37 38 // Add request to the list of outstanding requests. 39 virtual bool RegisterRequest(URLRequestAutomationJob* job); 40 // Remove request from the list of outstanding requests. 41 virtual void UnRegisterRequest(URLRequestAutomationJob* job); 42 43 protected: 44 AutomationProvider* automation_; 45 // declare the request map. 46 struct RequestJob { 47 base::MessageLoop* loop_; 48 scoped_refptr<URLRequestAutomationJob> job_; 49 }; 50 typedef std::map<int, RequestJob> RequestMap; 51 RequestMap requests_; 52 53 // Protects access to requests_. 54 base::Lock requests_lock_; 55 }; 56 57 #endif // CHROME_FRAME_TEST_NET_TEST_AUTOMATION_RESOURCE_MESSAGE_FILTER_H_ 58