Make DBusStatistics only run on the main thread and add additional CHECKs to ensure thread safety.

Calls from other threads will be ignored. Currently the only DBus calls from other threads are for Geolocation. Supporting statistics gathering across multiple threads is unnecessary overhead that we don't currently need.

BUG=168134


Review URL: https://chromiumcodereview.appspot.com/11761015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175706 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 787e639c1306ca968a4651145133e2b22919f893
diff --git a/dbus/dbus_statistics.cc b/dbus/dbus_statistics.cc
index 015e20e..8368c4c 100644
--- a/dbus/dbus_statistics.cc
+++ b/dbus/dbus_statistics.cc
@@ -10,6 +10,7 @@
 #include "base/memory/scoped_ptr.h"
 #include "base/stl_util.h"
 #include "base/stringprintf.h"
+#include "base/threading/platform_thread.h"
 #include "base/time.h"
 
 namespace dbus {
@@ -60,10 +61,13 @@
 // Simple class for gathering DBus usage statistics.
 class DBusStatistics {
  public:
-  DBusStatistics() : start_time_(base::Time::Now()) {
+  DBusStatistics()
+      : start_time_(base::Time::Now()),
+        origin_thread_id_(base::PlatformThread::CurrentId()) {
   }
 
   ~DBusStatistics() {
+    DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
     STLDeleteContainerPointers(stats_.begin(), stats_.end());
   }
 
@@ -79,6 +83,11 @@
                const std::string& interface,
                const std::string& method,
                StatType type) {
+    if (base::PlatformThread::CurrentId() != origin_thread_id_) {
+      DLOG(WARNING) << "Ignoring DBusStatistics::AddStat call from thread: "
+                    << base::PlatformThread::CurrentId();
+      return;
+    }
     Stat* stat = GetStat(service, interface, method, true);
     DCHECK(stat);
     if (type == TYPE_SENT_METHOD_CALLS)
@@ -97,6 +106,7 @@
                 const std::string& interface,
                 const std::string& method,
                 bool add_stat) {
+    DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
     scoped_ptr<Stat> stat(new Stat(service, interface, method));
     StatSet::iterator found = stats_.find(stat.get());
     if (found != stats_.end())
@@ -113,6 +123,7 @@
  private:
   StatSet stats_;
   base::Time start_time_;
+  base::PlatformThreadId origin_thread_id_;
 
   DISALLOW_COPY_AND_ASSIGN(DBusStatistics);
 };