Add some more linux-specific files.

git-svn-id: http://libjingle.googlecode.com/svn/trunk@113 dd674b97-3498-5ee5-1854-bdd07cd0ff33
diff --git a/talk/base/dbus_unittest.cc b/talk/base/dbus_unittest.cc
new file mode 100644
index 0000000..2b9a545
--- /dev/null
+++ b/talk/base/dbus_unittest.cc
@@ -0,0 +1,249 @@
+/*
+ * libjingle
+ * Copyright 2011, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_DBUS_GLIB
+
+#include "talk/base/dbus.h"
+#include "talk/base/gunit.h"
+#include "talk/base/thread.h"
+
+namespace talk_base {
+
+#define SIG_NAME "NameAcquired"
+
+static const uint32 kTimeoutMs = 5000U;
+
+class DBusSigFilterTest : public DBusSigFilter {
+ public:
+  // DBusSigFilterTest listens on DBus service itself for "NameAcquired" signal.
+  // This signal should be received when the application connects to DBus
+  // service and gains ownership of a name.
+  // http://dbus.freedesktop.org/doc/dbus-specification.html
+  DBusSigFilterTest()
+      : DBusSigFilter(GetFilter()),
+        message_received_(false) {
+  }
+
+  bool MessageReceived() {
+    return message_received_;
+  }
+
+ private:
+  static std::string GetFilter() {
+    return talk_base::DBusSigFilter::BuildFilterString("", "", SIG_NAME);
+  }
+
+  // Implement virtual method of DBusSigFilter. On caller thread.
+  virtual void ProcessSignal(DBusMessage *message) {
+    EXPECT_TRUE(message != NULL);
+    message_received_ = true;
+  }
+
+  bool message_received_;
+};
+
+TEST(DBusMonitorTest, StartStopStartStop) {
+  DBusSigFilterTest filter;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter));
+
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_NOT_INITIALIZED);
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_RUNNING);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+// DBusMonitorTest listens on DBus service itself for "NameAcquired" signal.
+// This signal should be received when the application connects to DBus
+// service and gains ownership of a name.
+// This test is to make sure that we capture the "NameAcquired" signal.
+TEST(DBusMonitorTest, ReceivedNameAcquiredSignal) {
+  DBusSigFilterTest filter;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter));
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE_WAIT(filter.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+TEST(DBusMonitorTest, ConcurrentMonitors) {
+  DBusSigFilterTest filter1;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor1;
+  monitor1.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor1.get()) {
+    EXPECT_TRUE(monitor1->AddFilter(&filter1));
+    DBusSigFilterTest filter2;
+    talk_base::scoped_ptr<talk_base::DBusMonitor> monitor2;
+    monitor2.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+    EXPECT_TRUE(monitor2->AddFilter(&filter2));
+
+    EXPECT_TRUE(monitor1->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor1->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE(monitor2->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor2->GetStatus(), kTimeoutMs);
+
+    EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE(monitor2->StopMonitoring());
+    EXPECT_EQ(monitor2->GetStatus(), DBusMonitor::DMS_STOPPED);
+
+    EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE(monitor1->StopMonitoring());
+    EXPECT_EQ(monitor1->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+TEST(DBusMonitorTest, ConcurrentFilters) {
+  DBusSigFilterTest filter1;
+  DBusSigFilterTest filter2;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter1));
+    EXPECT_TRUE(monitor->AddFilter(&filter2));
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+
+    EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
+
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+TEST(DBusMonitorTest, NoAddFilterIfRunning) {
+  DBusSigFilterTest filter1;
+  DBusSigFilterTest filter2;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter1));
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_FALSE(monitor->AddFilter(&filter2));
+
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+TEST(DBusMonitorTest, AddFilterAfterStop) {
+  DBusSigFilterTest filter1;
+  DBusSigFilterTest filter2;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter1));
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+
+    EXPECT_TRUE(monitor->AddFilter(&filter2));
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_EQ_WAIT(DBusMonitor::DMS_RUNNING, monitor->GetStatus(), kTimeoutMs);
+    EXPECT_TRUE_WAIT(filter1.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE_WAIT(filter2.MessageReceived(), kTimeoutMs);
+    EXPECT_TRUE(monitor->StopMonitoring());
+    EXPECT_EQ(monitor->GetStatus(), DBusMonitor::DMS_STOPPED);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started. Skipping test.";
+  }
+}
+
+TEST(DBusMonitorTest, StopRightAfterStart) {
+  DBusSigFilterTest filter;
+  talk_base::scoped_ptr<talk_base::DBusMonitor> monitor;
+  monitor.reset(talk_base::DBusMonitor::Create(DBUS_BUS_SYSTEM));
+  if (monitor.get()) {
+    EXPECT_TRUE(monitor->AddFilter(&filter));
+
+    EXPECT_TRUE(monitor->StartMonitoring());
+    EXPECT_TRUE(monitor->StopMonitoring());
+
+    // Stop the monitoring thread right after it had been started.
+    // If the monitoring thread got a chance to receive a DBus signal, it would
+    // post a message to the main thread and signal the main thread wakeup.
+    // This message will be cleaned out automatically when the filter get
+    // destructed. Here we also consume the wakeup signal (if there is one) so
+    // that the testing (main) thread is reset to a clean state.
+    talk_base::Thread::Current()->ProcessMessages(1);
+  } else {
+    LOG(LS_WARNING) << "DBus Monitor not started.";
+  }
+}
+
+TEST(DBusSigFilter, BuildFilterString) {
+  EXPECT_EQ(DBusSigFilter::BuildFilterString("", "", ""),
+      (DBUS_TYPE "='" DBUS_SIGNAL "'"));
+  EXPECT_EQ(DBusSigFilter::BuildFilterString("p", "", ""),
+      (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p'"));
+  EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i", ""),
+      (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p',"
+          DBUS_INTERFACE "='i'"));
+  EXPECT_EQ(DBusSigFilter::BuildFilterString("p","i","m"),
+      (DBUS_TYPE "='" DBUS_SIGNAL "'," DBUS_PATH "='p',"
+          DBUS_INTERFACE "='i'," DBUS_MEMBER "='m'"));
+}
+
+}  // namespace talk_base
+
+#endif  // HAVE_DBUS_GLIB
diff --git a/talk/base/linuxfdwalk.c b/talk/base/linuxfdwalk.c
new file mode 100644
index 0000000..4179f41
--- /dev/null
+++ b/talk/base/linuxfdwalk.c
@@ -0,0 +1,98 @@
+/*
+ * libjingle
+ * Copyright 2004--2009, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "talk/base/linuxfdwalk.h"
+
+// Parses a file descriptor number in base 10, requiring the strict format used
+// in /proc/*/fd. Returns the value, or -1 if not a valid string.
+static int parse_fd(const char *s) {
+  if (!*s) {
+    // Empty string is invalid.
+    return -1;
+  }
+  int val = 0;
+  do {
+    if (*s < '0' || *s > '9') {
+      // Non-numeric characters anywhere are invalid.
+      return -1;
+    }
+    int digit = *s++ - '0';
+    val = val * 10 + digit;
+  } while (*s);
+  return val;
+}
+
+int fdwalk(void (*func)(void *, int), void *opaque) {
+  DIR *dir = opendir("/proc/self/fd");
+  if (!dir) {
+    return -1;
+  }
+  int opendirfd = dirfd(dir);
+  int parse_errors = 0;
+  struct dirent *ent;
+  // Have to clear errno to distinguish readdir() completion from failure.
+  while (errno = 0, (ent = readdir(dir)) != NULL) {
+    if (strcmp(ent->d_name, ".") == 0 ||
+        strcmp(ent->d_name, "..") == 0) {
+      continue;
+    }
+    // We avoid atoi or strtol because those are part of libc and they involve
+    // locale stuff, which is probably not safe from a post-fork context in a
+    // multi-threaded app.
+    int fd = parse_fd(ent->d_name);
+    if (fd < 0) {
+      parse_errors = 1;
+      continue;
+    }
+    if (fd != opendirfd) {
+      (*func)(opaque, fd);
+    }
+  }
+  int saved_errno = errno;
+  if (closedir(dir) < 0) {
+    if (!saved_errno) {
+      // Return the closedir error.
+      return -1;
+    }
+    // Else ignore it because we have a more relevant error to return.
+  }
+  if (saved_errno) {
+    errno = saved_errno;
+    return -1;
+  } else if (parse_errors) {
+    errno = EBADF;
+    return -1;
+  } else {
+    return 0;
+  }
+}
diff --git a/talk/base/linuxfdwalk_unittest.cc b/talk/base/linuxfdwalk_unittest.cc
new file mode 100644
index 0000000..ff14b66
--- /dev/null
+++ b/talk/base/linuxfdwalk_unittest.cc
@@ -0,0 +1,92 @@
+/*
+ * libjingle
+ * Copyright 2009, Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <set>
+#include <sstream>
+
+#include "talk/base/gunit.h"
+#include "talk/base/linuxfdwalk.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+static const int kArbitraryLargeFdNumber = 424;
+
+static void FdCheckVisitor(void *data, int fd) {
+  std::set<int> *fds = static_cast<std::set<int> *>(data);
+  EXPECT_EQ(1U, fds->erase(fd));
+}
+
+static void FdEnumVisitor(void *data, int fd) {
+  std::set<int> *fds = static_cast<std::set<int> *>(data);
+  EXPECT_TRUE(fds->insert(fd).second);
+}
+
+// Checks that the set of open fds is exactly the given list.
+static void CheckOpenFdList(std::set<int> fds) {
+  EXPECT_EQ(0, fdwalk(&FdCheckVisitor, &fds));
+  EXPECT_EQ(0U, fds.size());
+}
+
+static void GetOpenFdList(std::set<int> *fds) {
+  fds->clear();
+  EXPECT_EQ(0, fdwalk(&FdEnumVisitor, fds));
+}
+
+TEST(LinuxFdWalk, TestFdWalk) {
+  std::set<int> fds;
+  GetOpenFdList(&fds);
+  std::ostringstream str;
+  // I have observed that the open set when starting a test is [0, 6]. Leaked
+  // fds would change that, but so can (e.g.) running under a debugger, so we
+  // can't really do an EXPECT. :(
+  str << "File descriptors open in test executable:";
+  for (std::set<int>::const_iterator i = fds.begin(); i != fds.end(); ++i) {
+    str << " " << *i;
+  }
+  LOG(LS_INFO) << str.str();
+  // Open some files.
+  int fd1 = open("/dev/null", O_RDONLY);
+  EXPECT_LE(0, fd1);
+  int fd2 = open("/dev/null", O_WRONLY);
+  EXPECT_LE(0, fd2);
+  int fd3 = open("/dev/null", O_RDWR);
+  EXPECT_LE(0, fd3);
+  int fd4 = dup2(fd3, kArbitraryLargeFdNumber);
+  EXPECT_LE(0, fd4);
+  EXPECT_TRUE(fds.insert(fd1).second);
+  EXPECT_TRUE(fds.insert(fd2).second);
+  EXPECT_TRUE(fds.insert(fd3).second);
+  EXPECT_TRUE(fds.insert(fd4).second);
+  CheckOpenFdList(fds);
+  EXPECT_EQ(0, close(fd1));
+  EXPECT_EQ(0, close(fd2));
+  EXPECT_EQ(0, close(fd3));
+  EXPECT_EQ(0, close(fd4));
+}
diff --git a/talk/base/linuxwindowpicker.cc b/talk/base/linuxwindowpicker.cc
new file mode 100644
index 0000000..250c1db
--- /dev/null
+++ b/talk/base/linuxwindowpicker.cc
@@ -0,0 +1,735 @@
+/*
+ * libjingle
+ * Copyright 2010 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "talk/base/linuxwindowpicker.h"
+
+#include <math.h>
+#include <string.h>
+
+#include <string>
+
+#include <X11/Xatom.h>
+#include <X11/extensions/Xcomposite.h>
+#include <X11/extensions/Xrender.h>
+#include <X11/Xutil.h>
+
+#include "talk/base/logging.h"
+
+namespace talk_base {
+
+// Stupid X11.  It seems none of the synchronous returns codes from X11 calls
+// are meaningful unless an asynchronous error handler is configured.  This
+// RAII class registers and unregisters an X11 error handler.
+class XErrorSuppressor {
+ public:
+  explicit XErrorSuppressor(Display* display)
+      : display_(display), original_error_handler_(NULL) {
+    SuppressX11Errors();
+  }
+  ~XErrorSuppressor() {
+    UnsuppressX11Errors();
+  }
+
+ private:
+  static int ErrorHandler(Display* display, XErrorEvent* e) {
+    char buf[256];
+    XGetErrorText(display, e->error_code, buf, sizeof buf);
+    LOG(LS_WARNING) << "Received X11 error \"" << buf << "\" for request code "
+                    << static_cast<unsigned int>(e->request_code);
+    return 0;
+  }
+
+  void SuppressX11Errors() {
+    XFlush(display_);
+    XSync(display_, False);
+    original_error_handler_ = XSetErrorHandler(&ErrorHandler);
+  }
+
+  void UnsuppressX11Errors() {
+    XFlush(display_);
+    XSync(display_, False);
+    XErrorHandler handler = XSetErrorHandler(original_error_handler_);
+    if (handler != &ErrorHandler) {
+      LOG(LS_WARNING) << "Unbalanced XSetErrorHandler() calls detected. "
+                      << "Final error handler may not be what you expect!";
+    }
+    original_error_handler_ = NULL;
+  }
+
+  Display* display_;
+  XErrorHandler original_error_handler_;
+
+  DISALLOW_COPY_AND_ASSIGN(XErrorSuppressor);
+};
+
+// Hiding all X11 specifics inside its own class. This to avoid
+// conflicts between talk and X11 header declarations.
+class XWindowEnumerator {
+ public:
+  XWindowEnumerator()
+      : display_(NULL),
+        has_composite_extension_(false),
+        has_render_extension_(false) {
+  }
+
+  ~XWindowEnumerator() {
+    if (display_ != NULL) {
+      XCloseDisplay(display_);
+    }
+  }
+
+  bool Init() {
+    if (display_ != NULL) {
+      // Already initialized.
+      return true;
+    }
+    display_ = XOpenDisplay(NULL);
+    if (display_ == NULL) {
+      LOG(LS_ERROR) << "Failed to open display.";
+      return false;
+    }
+
+    XErrorSuppressor error_suppressor(display_);
+
+    wm_state_ = XInternAtom(display_, "WM_STATE", True);
+    net_wm_icon_ = XInternAtom(display_, "_NET_WM_ICON", False);
+
+    int event_base, error_base, major_version, minor_version;
+    if (XCompositeQueryExtension(display_, &event_base, &error_base) &&
+        XCompositeQueryVersion(display_, &major_version, &minor_version) &&
+        // XCompositeNameWindowPixmap() requires version 0.2
+        (major_version > 0 || minor_version >= 2)) {
+      has_composite_extension_ = true;
+    } else {
+      LOG(LS_INFO) << "Xcomposite extension not available or too old.";
+    }
+
+    if (XRenderQueryExtension(display_, &event_base, &error_base) &&
+        XRenderQueryVersion(display_, &major_version, &minor_version) &&
+        // XRenderSetPictureTransform() requires version 0.6
+        (major_version > 0 || minor_version >= 6)) {
+      has_render_extension_ = true;
+    } else {
+      LOG(LS_INFO) << "Xrender extension not available or too old.";
+    }
+    return true;
+  }
+
+  bool EnumerateWindows(WindowDescriptionList* descriptions) {
+    if (!Init()) {
+      return false;
+    }
+    XErrorSuppressor error_suppressor(display_);
+    int num_screens = XScreenCount(display_);
+    bool result = false;
+    for (int i = 0; i < num_screens; ++i) {
+      if (EnumerateScreenWindows(descriptions, i)) {
+        // We know we succeded on at least one screen.
+        result = true;
+      }
+    }
+    return result;
+  }
+
+  bool EnumerateDesktops(DesktopDescriptionList* descriptions) {
+    if (!Init()) {
+      return false;
+    }
+    XErrorSuppressor error_suppressor(display_);
+    int num_screens = XScreenCount(display_);
+    for (int i = 0; i < num_screens; ++i) {
+      Window root_window = XRootWindow(display_, i);
+      DesktopId id(DesktopId(root_window, i));
+      // TODO: Figure out an appropriate desktop title.
+      DesktopDescription desc(id, "");
+      descriptions->push_back(desc);
+    }
+    return num_screens > 0;
+  }
+
+  bool IsVisible(const WindowId& id) {
+    if (!Init()) {
+      return false;
+    }
+    XErrorSuppressor error_suppressor(display_);
+    XWindowAttributes attr;
+    if (!XGetWindowAttributes(display_, id.id(), &attr)) {
+      LOG(LS_ERROR) << "XGetWindowAttributes() failed";
+      return false;
+    }
+    return attr.map_state == IsViewable;
+  }
+
+  bool MoveToFront(const WindowId& id) {
+    if (!Init()) {
+      return false;
+    }
+    XErrorSuppressor error_suppressor(display_);
+    unsigned int num_children;
+    Window* children;
+    Window parent;
+    Window root;
+
+    // Find root window to pass event to.
+    int status = XQueryTree(display_, id.id(), &root, &parent, &children,
+                            &num_children);
+    if (status == 0) {
+      LOG(LS_WARNING) << "Failed to query for child windows.";
+      return false;
+    }
+    if (children != NULL) {
+      XFree(children);
+    }
+
+    // Move the window to front.
+    XRaiseWindow(display_, id.id());
+
+    // Some window managers (e.g., metacity in GNOME) consider it illegal to
+    // raise a window without also giving it input focus with
+    // _NET_ACTIVE_WINDOW, so XRaiseWindow() on its own isn't enough.
+    Atom atom = XInternAtom(display_, "_NET_ACTIVE_WINDOW", True);
+    if (atom != None) {
+      XEvent xev;
+      long event_mask;
+
+      xev.xclient.type = ClientMessage;
+      xev.xclient.serial = 0;
+      xev.xclient.send_event = True;
+      xev.xclient.window = id.id();
+      xev.xclient.message_type = atom;
+
+      // The format member is set to 8, 16, or 32 and specifies whether the
+      // data should be viewed as a list of bytes, shorts, or longs.
+      xev.xclient.format = 32;
+
+      xev.xclient.data.l[0] = 0;
+      xev.xclient.data.l[1] = 0;
+      xev.xclient.data.l[2] = 0;
+      xev.xclient.data.l[3] = 0;
+      xev.xclient.data.l[4] = 0;
+
+      event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
+
+      XSendEvent(display_, root, False, event_mask, &xev);
+    }
+    XFlush(display_);
+    return true;
+  }
+
+  uint8* GetWindowIcon(const WindowId& id, int* width, int* height) {
+    if (!Init()) {
+      return NULL;
+    }
+    XErrorSuppressor error_suppressor(display_);
+    Atom ret_type;
+    int format;
+    unsigned long length, bytes_after, size;
+    unsigned char* data = NULL;
+
+    // Find out the size of the icon data.
+    if (XGetWindowProperty(
+            display_, id.id(), net_wm_icon_, 0, 0, False, XA_CARDINAL,
+            &ret_type, &format, &length, &size, &data) == Success &&
+        data) {
+      XFree(data);
+    } else {
+      LOG(LS_ERROR) << "Failed to get size of the icon.";
+      return NULL;
+    }
+    // Get the icon data, the format is one uint32 each for width and height,
+    // followed by the actual pixel data.
+    if (size >= 2 &&
+        XGetWindowProperty(
+            display_, id.id(), net_wm_icon_, 0, size, False, XA_CARDINAL,
+            &ret_type, &format, &length, &bytes_after, &data) == Success &&
+        data) {
+      uint32* data_ptr = reinterpret_cast<uint32*>(data);
+      int w, h;
+      w = data_ptr[0];
+      h = data_ptr[1];
+      if (size < static_cast<unsigned long>(w * h + 2)) {
+        XFree(data);
+        LOG(LS_ERROR) << "Not a vaild icon.";
+        return NULL;
+      }
+      uint8* rgba =
+          ArgbToRgba(&data_ptr[2], 0, 0, w, h, w, h, true);
+      XFree(data);
+      *width = w;
+      *height = h;
+      return rgba;
+    } else {
+      LOG(LS_ERROR) << "Failed to get window icon data.";
+      return NULL;
+    }
+  }
+
+  uint8* GetWindowThumbnail(const WindowId& id, int width, int height) {
+    if (!Init()) {
+      return NULL;
+    }
+
+    if (!has_composite_extension_) {
+      // Without the Xcomposite extension we would only get a good thumbnail if
+      // the whole window is visible on screen and not covered by any
+      // other window. This is not something we want so instead, just
+      // bail out.
+      LOG(LS_INFO) << "No Xcomposite extension detected.";
+      return NULL;
+    }
+    XErrorSuppressor error_suppressor(display_);
+
+    Window root;
+    int x;
+    int y;
+    unsigned int src_width;
+    unsigned int src_height;
+    unsigned int border_width;
+    unsigned int depth;
+
+    // In addition to needing X11 server-side support for Xcomposite, it
+    // actually needs to be turned on for this window in order to get a good
+    // thumbnail. If the user has modern hardware/drivers but isn't using a
+    // compositing window manager, that won't be the case. We could
+    // automatically turn it on for all windows here so that we can get
+    // thumbnails, but the transition is visually ugly, so instead we just want
+    // to detect this case and bail out. To do so, we attempt to retrieve the
+    // backing pixmap for the window, which will only exist if the window has
+    // been redirected to offscreen drawing by a compositing window manager. If
+    // it doesn't exist, the calls will raise errors, so we must suppress errors
+    // to prevent libX11 from aborting the program.
+    Pixmap src_pixmap = XCompositeNameWindowPixmap(display_, id.id());
+    if (!src_pixmap) {
+      // Even if the backing pixmap doesn't exist, this still should have
+      // succeeded and returned a valid handle (it just wouldn't be a handle to
+      // anything). So this is a real error path.
+      LOG(LS_ERROR) << "XCompositeNameWindowPixmap() failed";
+      return NULL;
+    }
+    if (!XGetGeometry(display_, src_pixmap, &root, &x, &y,
+                      &src_width, &src_height, &border_width,
+                      &depth)) {
+      // If the window does not actually have a backing pixmap, this is the path
+      // that will "fail", so it's a warning rather than an error.
+      LOG(LS_WARNING) << "XGetGeometry() failed (probably composite is not in "
+                      << "use)";
+      XFreePixmap(display_, src_pixmap);
+      return NULL;
+    }
+
+    // If we get to here, then composite is in use for this window and it has a
+    // valid backing pixmap.
+
+    XWindowAttributes attr;
+    if (!XGetWindowAttributes(display_, id.id(), &attr)) {
+      LOG(LS_ERROR) << "XGetWindowAttributes() failed";
+      XFreePixmap(display_, src_pixmap);
+      return NULL;
+    }
+
+    uint8* data = GetDrawableThumbnail(src_pixmap,
+                                       attr.visual,
+                                       src_width,
+                                       src_height,
+                                       width,
+                                       height);
+    XFreePixmap(display_, src_pixmap);
+    return data;
+  }
+
+  int GetNumDesktops() {
+    if (!Init()) {
+      return -1;
+    }
+
+    return XScreenCount(display_);
+  }
+
+  uint8* GetDesktopThumbnail(const DesktopId& id, int width, int height) {
+    if (!Init()) {
+      return NULL;
+    }
+    XErrorSuppressor error_suppressor(display_);
+
+    Window root_window = id.id();
+    XWindowAttributes attr;
+    if (!XGetWindowAttributes(display_, root_window, &attr)) {
+      LOG(LS_ERROR) << "XGetWindowAttributes() failed";
+      return NULL;
+    }
+
+    return GetDrawableThumbnail(root_window,
+                                attr.visual,
+                                attr.width,
+                                attr.height,
+                                width,
+                                height);
+  }
+
+ private:
+  uint8* GetDrawableThumbnail(Drawable src_drawable,
+                              Visual* visual,
+                              int src_width,
+                              int src_height,
+                              int dst_width,
+                              int dst_height) {
+    if (!has_render_extension_) {
+      // Without the Xrender extension we would have to read the full window and
+      // scale it down in our process. Xrender is over a decade old so we aren't
+      // going to expend effort to support that situation. We still need to
+      // check though because probably some virtual VNC displays are in this
+      // category.
+      LOG(LS_INFO) << "No Xrender extension detected.";
+      return NULL;
+    }
+
+    XRenderPictFormat* format = XRenderFindVisualFormat(display_,
+                                                        visual);
+    if (!format) {
+      LOG(LS_ERROR) << "XRenderFindVisualFormat() failed";
+      return NULL;
+    }
+
+    // Create a picture to reference the window pixmap.
+    XRenderPictureAttributes pa;
+    pa.subwindow_mode = IncludeInferiors;  // Don't clip child widgets
+    Picture src = XRenderCreatePicture(display_,
+                                       src_drawable,
+                                       format,
+                                       CPSubwindowMode,
+                                       &pa);
+    if (!src) {
+      LOG(LS_ERROR) << "XRenderCreatePicture() failed";
+      return NULL;
+    }
+
+    // Create a picture to reference the destination pixmap.
+    Pixmap dst_pixmap = XCreatePixmap(display_,
+                                      src_drawable,
+                                      dst_width,
+                                      dst_height,
+                                      format->depth);
+    if (!dst_pixmap) {
+      LOG(LS_ERROR) << "XCreatePixmap() failed";
+      XRenderFreePicture(display_, src);
+      return NULL;
+    }
+
+    Picture dst = XRenderCreatePicture(display_, dst_pixmap, format, 0, NULL);
+    if (!dst) {
+      LOG(LS_ERROR) << "XRenderCreatePicture() failed";
+      XFreePixmap(display_, dst_pixmap);
+      XRenderFreePicture(display_, src);
+      return NULL;
+    }
+
+    // Clear the background.
+    XRenderColor transparent = {0};
+    XRenderFillRectangle(display_,
+                         PictOpSrc,
+                         dst,
+                         &transparent,
+                         0,
+                         0,
+                         dst_width,
+                         dst_height);
+
+    // Calculate how much we need to scale the image.
+    double scale_x = static_cast<double>(dst_width) /
+        static_cast<double>(src_width);
+    double scale_y = static_cast<double>(dst_height) /
+        static_cast<double>(src_height);
+    double scale = talk_base::_min(scale_y, scale_x);
+
+    int scaled_width = round(src_width * scale);
+    int scaled_height = round(src_height * scale);
+
+    // Render the thumbnail centered on both axis.
+    int centered_x = (dst_width - scaled_width) / 2;
+    int centered_y = (dst_height - scaled_height) / 2;
+
+    // Scaling matrix
+    XTransform xform = { {
+        { XDoubleToFixed(1), XDoubleToFixed(0), XDoubleToFixed(0) },
+        { XDoubleToFixed(0), XDoubleToFixed(1), XDoubleToFixed(0) },
+        { XDoubleToFixed(0), XDoubleToFixed(0), XDoubleToFixed(scale) }
+        } };
+    XRenderSetPictureTransform(display_, src, &xform);
+
+    // Apply filter to smooth out the image.
+    XRenderSetPictureFilter(display_, src, FilterBest, NULL, 0);
+
+    // Render the image to the destination picture.
+    XRenderComposite(display_,
+                     PictOpSrc,
+                     src,
+                     None,
+                     dst,
+                     0,
+                     0,
+                     0,
+                     0,
+                     centered_x,
+                     centered_y,
+                     scaled_width,
+                     scaled_height);
+
+    // Get the pixel data from the X server. TODO: XGetImage
+    // might be slow here, compare with ShmGetImage.
+    XImage* image = XGetImage(display_,
+                              dst_pixmap,
+                              0,
+                              0,
+                              dst_width,
+                              dst_height,
+                              AllPlanes, ZPixmap);
+    uint8* data = ArgbToRgba(reinterpret_cast<uint32*>(image->data),
+                             centered_x,
+                             centered_y,
+                             scaled_width,
+                             scaled_height,
+                             dst_width,
+                             dst_height,
+                             false);
+    XDestroyImage(image);
+    XRenderFreePicture(display_, dst);
+    XFreePixmap(display_, dst_pixmap);
+    XRenderFreePicture(display_, src);
+    return data;
+  }
+
+  uint8* ArgbToRgba(uint32* argb_data, int x, int y, int w, int h,
+                    int stride_x, int stride_y, bool has_alpha) {
+    uint8* p;
+    int len = stride_x * stride_y * 4;
+    uint8* data = new uint8[len];
+    memset(data, 0, len);
+    p = data + 4 * (y * stride_x + x);
+    for (int i = 0; i < h; ++i) {
+      for (int j = 0; j < w; ++j) {
+        uint32 argb;
+        uint32 rgba;
+        argb = argb_data[stride_x * (y + i) + x + j];
+        rgba = (argb << 8) | (argb >> 24);
+        *p = rgba >> 24;
+        ++p;
+        *p = (rgba >> 16) & 0xff;
+        ++p;
+        *p = (rgba >> 8) & 0xff;
+        ++p;
+        *p = has_alpha ? rgba & 0xFF : 0xFF;
+        ++p;
+      }
+      p += (stride_x - w) * 4;
+    }
+    return data;
+  }
+
+  bool EnumerateScreenWindows(WindowDescriptionList* descriptions, int screen) {
+    Window parent;
+    Window *children;
+    int status;
+    unsigned int num_children;
+    Window root_window = XRootWindow(display_, screen);
+    status = XQueryTree(display_, root_window, &root_window, &parent, &children,
+                        &num_children);
+    if (status == 0) {
+      LOG(LS_ERROR) << "Failed to query for child windows.";
+      return false;
+    }
+    for (unsigned int i = 0; i < num_children; ++i) {
+      // Iterate in reverse order to display windows from front to back.
+      Window app_window = GetApplicationWindow(children[num_children - 1 - i]);
+      if (app_window &&
+          !LinuxWindowPicker::IsDesktopElement(display_, app_window)) {
+        std::string title;
+        if (GetWindowTitle(app_window, &title)) {
+          WindowId id(app_window);
+          WindowDescription desc(id, title);
+          descriptions->push_back(desc);
+        }
+      }
+    }
+    if (children != NULL) {
+      XFree(children);
+    }
+    return true;
+  }
+
+  bool GetWindowTitle(Window window, std::string* title) {
+    int status;
+    bool result = false;
+    XTextProperty window_name;
+    window_name.value = NULL;
+    if (window) {
+      status = XGetWMName(display_, window, &window_name);
+      if (status && window_name.value && window_name.nitems) {
+        int cnt;
+        char **list = NULL;
+        status = Xutf8TextPropertyToTextList(display_, &window_name, &list,
+                                             &cnt);
+        if (status >= Success && cnt && *list) {
+          if (cnt > 1) {
+            LOG(LS_INFO) << "Window has " << cnt
+                         << " text properties, only using the first one.";
+          }
+          *title = *list;
+          result = true;
+        }
+        if (list != NULL) {
+          XFreeStringList(list);
+        }
+      }
+      if (window_name.value != NULL) {
+        XFree(window_name.value);
+      }
+    }
+    return result;
+  }
+
+  Window GetApplicationWindow(Window window) {
+    Window root, parent;
+    Window app_window = 0;
+    Window *children;
+    unsigned int num_children;
+    Atom type = None;
+    int format;
+    unsigned long nitems, after;
+    unsigned char *data;
+
+    int ret = XGetWindowProperty(display_, window,
+                                 wm_state_, 0L, 2,
+                                 False, wm_state_, &type, &format,
+                                 &nitems, &after, &data);
+    if (ret != Success) {
+      LOG(LS_ERROR) << "XGetWindowProperty failed with return code " << ret
+                    << " for window " << window << ".";
+      return 0;
+    }
+    if (type != None) {
+      int64 state = static_cast<int64>(*data);
+      XFree(data);
+      return state == NormalState ? window : 0;
+    }
+    XFree(data);
+    if (!XQueryTree(display_, window, &root, &parent, &children,
+                    &num_children)) {
+      LOG(LS_ERROR) << "Failed to query for child windows although window"
+                    << "does not have a valid WM_STATE.";
+      return 0;
+    }
+    for (unsigned int i = 0; i < num_children; ++i) {
+      app_window = GetApplicationWindow(children[i]);
+      if (app_window) {
+        break;
+      }
+    }
+    if (children != NULL) {
+      XFree(children);
+    }
+    return app_window;
+  }
+
+  Atom wm_state_;
+  Atom net_wm_icon_;
+  Display* display_;
+  bool has_composite_extension_;
+  bool has_render_extension_;
+};
+
+LinuxWindowPicker::LinuxWindowPicker() : enumerator_(new XWindowEnumerator()) {
+}
+
+LinuxWindowPicker::~LinuxWindowPicker() {
+}
+
+bool LinuxWindowPicker::IsDesktopElement(_XDisplay* display, Window window) {
+  if (window == 0) {
+    LOG(LS_WARNING) << "Zero is never a valid window.";
+    return false;
+  }
+  XClassHint class_hint;
+  Status s = XGetClassHint(display, window, &class_hint);
+  bool result = false;
+  if (s == 0) {
+    // No hints, assume this is a normal application window.
+    return result;
+  }
+  static const std::string gnome_panel("gnome-panel");
+  static const std::string desktop_window("desktop_window");
+
+  if (gnome_panel.compare(class_hint.res_name) == 0 ||
+      desktop_window.compare(class_hint.res_name) == 0) {
+    result = true;
+  }
+  XFree(class_hint.res_name);
+  XFree(class_hint.res_class);
+  return result;
+}
+
+bool LinuxWindowPicker::Init() {
+  return enumerator_->Init();
+}
+
+bool LinuxWindowPicker::GetWindowList(WindowDescriptionList* descriptions) {
+  return enumerator_->EnumerateWindows(descriptions);
+}
+
+bool LinuxWindowPicker::GetDesktopList(DesktopDescriptionList* descriptions) {
+  return enumerator_->EnumerateDesktops(descriptions);
+}
+
+bool LinuxWindowPicker::IsVisible(const WindowId& id) {
+  return enumerator_->IsVisible(id);
+}
+
+bool LinuxWindowPicker::MoveToFront(const WindowId& id) {
+  return enumerator_->MoveToFront(id);
+}
+
+
+uint8* LinuxWindowPicker::GetWindowIcon(const WindowId& id, int* width,
+                                        int* height) {
+  return enumerator_->GetWindowIcon(id, width, height);
+}
+
+uint8* LinuxWindowPicker::GetWindowThumbnail(const WindowId& id, int width,
+                                             int height) {
+  return enumerator_->GetWindowThumbnail(id, width, height);
+}
+
+int LinuxWindowPicker::GetNumDesktops() {
+  return enumerator_->GetNumDesktops();
+}
+
+uint8* LinuxWindowPicker::GetDesktopThumbnail(const DesktopId& id,
+                                              int width,
+                                              int height) {
+  return enumerator_->GetDesktopThumbnail(id, width, height);
+}
+
+}  // namespace talk_base
diff --git a/talk/base/linuxwindowpicker.h b/talk/base/linuxwindowpicker.h
new file mode 100644
index 0000000..ad92c6f
--- /dev/null
+++ b/talk/base/linuxwindowpicker.h
@@ -0,0 +1,66 @@
+/*
+ * libjingle
+ * Copyright 2010 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TALK_BASE_LINUXWINDOWPICKER_H_
+#define TALK_BASE_LINUXWINDOWPICKER_H_
+
+#include "talk/base/basictypes.h"
+#include "talk/base/scoped_ptr.h"
+#include "talk/base/windowpicker.h"
+
+// Avoid include <X11/Xlib.h>.
+struct _XDisplay;
+typedef unsigned long Window;
+
+namespace talk_base {
+
+class XWindowEnumerator;
+
+class LinuxWindowPicker : public WindowPicker {
+ public:
+  LinuxWindowPicker();
+  ~LinuxWindowPicker();
+
+  static bool IsDesktopElement(_XDisplay* display, Window window);
+
+  virtual bool Init();
+  virtual bool IsVisible(const WindowId& id);
+  virtual bool MoveToFront(const WindowId& id);
+  virtual bool GetWindowList(WindowDescriptionList* descriptions);
+  virtual bool GetDesktopList(DesktopDescriptionList* descriptions);
+  uint8* GetWindowIcon(const WindowId& id, int* width, int* height);
+  uint8* GetWindowThumbnail(const WindowId& id, int width, int height);
+  int GetNumDesktops();
+  uint8* GetDesktopThumbnail(const DesktopId& id, int width, int height);
+
+ private:
+  scoped_ptr<XWindowEnumerator> enumerator_;
+};
+
+}  // namespace talk_base
+
+#endif  // TALK_BASE_LINUXWINDOWPICKER_H_
diff --git a/talk/base/linuxwindowpicker_unittest.cc b/talk/base/linuxwindowpicker_unittest.cc
new file mode 100644
index 0000000..fa0d49a
--- /dev/null
+++ b/talk/base/linuxwindowpicker_unittest.cc
@@ -0,0 +1,46 @@
+/*
+ * libjingle
+ * Copyright 2010 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "talk/base/gunit.h"
+#include "talk/base/linuxwindowpicker.h"
+#include "talk/base/logging.h"
+#include "talk/base/windowpicker.h"
+
+#ifndef LINUX
+#error Only for Linux
+#endif
+
+namespace talk_base {
+
+TEST(LinuxWindowPickerTest, TestGetWindowList) {
+  LinuxWindowPicker window_picker;
+  WindowDescriptionList descriptions;
+  window_picker.Init();
+  window_picker.GetWindowList(&descriptions);
+}
+
+}  // namespace talk_base
diff --git a/talk/base/windowpickerfactory.h b/talk/base/windowpickerfactory.h
new file mode 100644
index 0000000..e001a2c
--- /dev/null
+++ b/talk/base/windowpickerfactory.h
@@ -0,0 +1,69 @@
+/*
+ * libjingle
+ * Copyright 2010 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TALK_BASE_WINDOWPICKERFACTORY_H_
+#define TALK_BASE_WINDOWPICKERFACTORY_H_
+
+#if defined(WIN32)
+#include "talk/base/win32windowpicker.h"
+#elif defined(OSX)
+#include "talk/base/macutils.h"
+#include "talk/base/macwindowpicker.h"
+#elif defined(LINUX)
+#include "talk/base/linuxwindowpicker.h"
+#endif
+
+#include "talk/base/windowpicker.h"
+
+namespace talk_base {
+
+class WindowPickerFactory {
+ public:
+  static WindowPicker* CreateWindowPicker() {
+#if defined(WIN32)
+    return new Win32WindowPicker();
+#elif defined(OSX)
+    return new MacWindowPicker();
+#elif defined(LINUX)
+    return new LinuxWindowPicker();
+#else
+    return NULL;
+#endif
+  }
+
+  static bool IsSupported() {
+#ifdef OSX
+    return GetOSVersionName() >= kMacOSLeopard;
+#else
+    return true;
+#endif
+  }
+};
+
+}  // namespace talk_base
+
+#endif  // TALK_BASE_WINDOWPICKERFACTORY_H_
diff --git a/talk/libjingle.scons b/talk/libjingle.scons
index 6cdf5b6..e1af691 100644
--- a/talk/libjingle.scons
+++ b/talk/libjingle.scons
@@ -74,6 +74,7 @@
              lin_srcs = [
                "base/latebindingsymboltable.cc",
                "base/linux.cc",
+               "base/linuxfdwalk.c",
                "base/libdbusglibsymboltable.cc",
                "session/phone/libudevsymboltable.cc",
                "session/phone/linuxdevicemanager.cc",
@@ -318,6 +319,7 @@
              extra_srcs = [
                "base/dbus.cc",
                "base/json.cc",
+               "base/linuxwindowpicker.cc",
                "base/natserver_main.cc",
                "base/maccocoasocketserver.mm",
                "base/maccocoathreadhelper.mm",
@@ -444,6 +446,7 @@
               lin_srcs = [
                 "base/latebindingsymboltable_unittest.cc",
                 "base/linux_unittest.cc",
+                "base/linuxfdwalk_unittest.cc",
               ],
               mac_srcs = [
                 "base/macsocketserver_unittest.cc",
@@ -526,7 +529,9 @@
                 "jingle",
               ],
               extra_srcs = [
+                "base/dbus_unittest.cc",
                 "base/json_unittest.cc",
+                "base/linuxwindowpicker_unittest.cc",
               ],
 )
 talk.Unittest(env, name = "p2p",