// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/idle_query_linux.h"

#include <X11/extensions/scrnsaver.h>
#include "ui/base/x/x11_util.h"

namespace browser {

class IdleData {
 public:
  IdleData() {
    int event_base;
    int error_base;
    if (XScreenSaverQueryExtension(ui::GetXDisplay(), &event_base,
                                   &error_base)) {
      mit_info = XScreenSaverAllocInfo();
    } else {
      mit_info = NULL;
    }
  }

  ~IdleData() {
    if (mit_info)
      XFree(mit_info);
  }

  XScreenSaverInfo *mit_info;
};

IdleQueryLinux::IdleQueryLinux() : idle_data_(new IdleData()) {}

IdleQueryLinux::~IdleQueryLinux() {}

int IdleQueryLinux::IdleTime() {
  if (!idle_data_->mit_info)
    return 0;

  if (XScreenSaverQueryInfo(ui::GetXDisplay(),
                            RootWindow(ui::GetXDisplay(), 0),
                            idle_data_->mit_info)) {
    return (idle_data_->mit_info->idle) / 1000;
  } else {
    return 0;
  }
}

}  // namespace browser

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/idle_query_linux.h"

#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>

namespace browser {

class IdleData {
 public:
  IdleData() {
    int event_base;
    int error_base;
    display = XOpenDisplay(NULL);
    if (XScreenSaverQueryExtension(display, &event_base, &error_base)) {
      mit_info = XScreenSaverAllocInfo();
    } else {
      mit_info = NULL;
    }
  }

  ~IdleData() {
    if (display) {
      XCloseDisplay(display);
      display = NULL;
    }
    if (mit_info)
      XFree(mit_info);
  }

  XScreenSaverInfo *mit_info;
  Display *display;
};

IdleQueryLinux::IdleQueryLinux() : idle_data_(new IdleData()) {}

IdleQueryLinux::~IdleQueryLinux() {}

int IdleQueryLinux::IdleTime() {
  if (!idle_data_->mit_info || !idle_data_->display)
    return 0;

  if (XScreenSaverQueryInfo(idle_data_->display,
                            RootWindow(idle_data_->display, 0),
                            idle_data_->mit_info)) {
    return (idle_data_->mit_info->idle) / 1000;
  } else {
    return 0;
  }
}

}  // namespace browser

--- 
+++ 
@@ -4,8 +4,8 @@
 
 #include "chrome/browser/idle_query_linux.h"
 
-#include <X11/Xlib.h>
 #include <X11/extensions/scrnsaver.h>
+#include "ui/base/x/x11_util.h"
 
 namespace browser {
 
@@ -14,8 +14,8 @@
   IdleData() {
     int event_base;
     int error_base;
-    display = XOpenDisplay(NULL);
-    if (XScreenSaverQueryExtension(display, &event_base, &error_base)) {
+    if (XScreenSaverQueryExtension(ui::GetXDisplay(), &event_base,
+                                   &error_base)) {
       mit_info = XScreenSaverAllocInfo();
     } else {
       mit_info = NULL;
@@ -23,16 +23,11 @@
   }
 
   ~IdleData() {
-    if (display) {
-      XCloseDisplay(display);
-      display = NULL;
-    }
     if (mit_info)
       XFree(mit_info);
   }
 
   XScreenSaverInfo *mit_info;
-  Display *display;
 };
 
 IdleQueryLinux::IdleQueryLinux() : idle_data_(new IdleData()) {}
@@ -40,11 +35,11 @@
 IdleQueryLinux::~IdleQueryLinux() {}
 
 int IdleQueryLinux::IdleTime() {
-  if (!idle_data_->mit_info || !idle_data_->display)
+  if (!idle_data_->mit_info)
     return 0;
 
-  if (XScreenSaverQueryInfo(idle_data_->display,
-                            RootWindow(idle_data_->display, 0),
+  if (XScreenSaverQueryInfo(ui::GetXDisplay(),
+                            RootWindow(ui::GetXDisplay(), 0),
                             idle_data_->mit_info)) {
     return (idle_data_->mit_info->idle) / 1000;
   } else {
Use GetXDisplay() instead of XOpenDisplay() in IdleQueryLinux.

We weren't checking that XOpenDisplay was actually succeeding before
attempting to use the display.

BUG=82308
TEST=none

Review URL: http://codereview.chromium.org/7015007

git-svn-id: de016e52bd170d2d4f2344f9bf92d50478b649e0@85138 0039d316-1c4b-4281-b951-d872f2087c98
