Rent syncemove all uses of the global Dispatcher Get function.

This reqired reworking how plugin->host GetInterface works. Previously,
interface requests were symmetric where each side would first do a
SupportsInterface to see if the remote side supports the interface, then create
the proxy. Since the plugin may talk to multiple renderers, we don't know where
to send these requests. The solution is to make the assumption that the
renderer always supports all PPB interfaces (which is possible since the proxy
is compiled with the executable).

This also adds some better lookup for interfaces to avoid having multiple lists
of interfaces. We now have a list of interfaces and factory functions in
dispatcher.cc.

Add some additional testing infrastructure for the dispatchers with simple tests.
Review URL: http://codereview.chromium.org/6286070

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@74121 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ppapi/proxy/ppapi_proxy_test.cc b/ppapi/proxy/ppapi_proxy_test.cc
index 240860c..1b75001 100644
--- a/ppapi/proxy/ppapi_proxy_test.cc
+++ b/ppapi/proxy/ppapi_proxy_test.cc
@@ -5,14 +5,21 @@
 #include "ppapi/proxy/ppapi_proxy_test.h"
 
 #include "ppapi/c/pp_errors.h"
+#include "ppapi/proxy/ppapi_messages.h"
 
 namespace pp {
 namespace proxy {
 
 namespace {
 
-const void* MockGetInterface(const char*) {
-  return NULL;
+ProxyTestBase* current_test = NULL;
+
+const void* MockGetInterface(const char* name) {
+  if (!current_test) {
+    NOTREACHED();
+    return NULL;
+  }
+  return current_test->GetInterface(name);
 }
 
 int32_t MockInitModule(PP_Module, Dispatcher::GetInterfaceFunc) {
@@ -24,36 +31,109 @@
 
 }  // namespace
 
+// ProxyTestBase ---------------------------------------------------------------
+
+ProxyTestBase::ProxyTestBase() : pp_module_(0x98765), pp_instance_(0x12345) {
+  DCHECK(!current_test);
+  current_test = this;
+}
+
+ProxyTestBase::~ProxyTestBase() {
+  DCHECK(current_test == this);
+  current_test = NULL;
+}
+
+const void* ProxyTestBase::GetInterface(const char* name) {
+  return registered_interfaces_[name];
+}
+
+void ProxyTestBase::RegisterTestInterface(const char* name,
+                                          const void* interface) {
+  registered_interfaces_[name] = interface;
+}
+
+bool ProxyTestBase::SupportsInterface(const char* name) {
+  sink().ClearMessages();
+
+  // IPC doesn't actually write to this when we send a message manually
+  // not actually using IPC.
+  bool unused_result = false;
+  PpapiMsg_SupportsInterface msg(name, &unused_result);
+  GetDispatcher()->OnMessageReceived(msg);
+
+  const IPC::Message* reply_msg =
+      sink().GetUniqueMessageMatching(IPC_REPLY_ID);
+  EXPECT_TRUE(reply_msg);
+  if (!reply_msg)
+    return false;
+
+  TupleTypes<PpapiMsg_SupportsInterface::ReplyParam>::ValueTuple reply_data;
+  EXPECT_TRUE(PpapiMsg_SupportsInterface::ReadReplyParam(
+      reply_msg, &reply_data));
+
+  sink().ClearMessages();
+  return reply_data.a;
+}
+
+// PluginProxyTest -------------------------------------------------------------
+
 PluginProxyTest::PluginProxyTest() {
 }
 
 PluginProxyTest::~PluginProxyTest() {
 }
 
+Dispatcher* PluginProxyTest::GetDispatcher() {
+  return plugin_dispatcher_.get();
+}
+
 void PluginProxyTest::SetUp() {
   // These must be first since the dispatcher set-up uses them.
   PluginResourceTracker::SetInstanceForTest(&resource_tracker_);
   PluginVarTracker::SetInstanceForTest(&var_tracker_);
 
-  pp_instance_ = 0x1234;
   plugin_dispatcher_.reset(new PluginDispatcher(
       base::Process::Current().handle(),
       &MockGetInterface,
       &MockInitModule,
       &MockShutdownModuleFunc));
-  plugin_dispatcher_->InitWithTestSink(&sink_);
-  // When the plugin dispatcher is per-instance, this is the line to use:
-  // PluginDispatcher::SetForInstance(pp_instance_, plugin_dispatcher_.get());
-  PluginDispatcher::SetGlobal(plugin_dispatcher_.get());
+  plugin_dispatcher_->InitWithTestSink(&sink());
+  plugin_dispatcher_->DidCreateInstance(pp_instance());
 }
 
 void PluginProxyTest::TearDown() {
-  PluginDispatcher::SetGlobal(NULL);
+  plugin_dispatcher_->DidDestroyInstance(pp_instance());
   plugin_dispatcher_.reset();
 
   PluginVarTracker::SetInstanceForTest(NULL);
   PluginResourceTracker::SetInstanceForTest(NULL);
 }
 
+// HostProxyTest ---------------------------------------------------------------
+
+HostProxyTest::HostProxyTest() {
+}
+
+HostProxyTest::~HostProxyTest() {
+}
+
+Dispatcher* HostProxyTest::GetDispatcher() {
+  return host_dispatcher_.get();
+}
+
+void HostProxyTest::SetUp() {
+  host_dispatcher_.reset(new HostDispatcher(
+      base::Process::Current().handle(),
+      pp_module(),
+      &MockGetInterface));
+  host_dispatcher_->InitWithTestSink(&sink());
+  HostDispatcher::SetForInstance(pp_instance(), host_dispatcher_.get());
+}
+
+void HostProxyTest::TearDown() {
+  HostDispatcher::RemoveForInstance(pp_instance());
+  host_dispatcher_.reset();
+}
+
 }  // namespace proxy
 }  // namespace pp