summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 21:42:23 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-07 21:42:23 +0000
commit95f14bf54cbbca1a2dd77999149f595c14ca18a7 (patch)
tree831393a6b15c78dbe40df420df50eb0731502941
parentde600ff6c6ff4ae5ea327d3f5ea3017acb2d77f7 (diff)
downloadchromium_src-95f14bf54cbbca1a2dd77999149f595c14ca18a7.zip
chromium_src-95f14bf54cbbca1a2dd77999149f595c14ca18a7.tar.gz
chromium_src-95f14bf54cbbca1a2dd77999149f595c14ca18a7.tar.bz2
Make ScopedGetDC() CHECK fail if GetDC(NULL) returns NULL.
Use scoping objects more in the omnibox code. BUG=110105 TEST=none Review URL: https://chromiumcodereview.appspot.com/10545045 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141087 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/win/scoped_hdc.h13
-rw-r--r--chrome/browser/ui/views/omnibox/omnibox_view_win.cc15
2 files changed, 17 insertions, 11 deletions
diff --git a/base/win/scoped_hdc.h b/base/win/scoped_hdc.h
index 9e2ea62..070c656 100644
--- a/base/win/scoped_hdc.h
+++ b/base/win/scoped_hdc.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 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.
@@ -21,8 +21,15 @@ class ScopedGetDC {
explicit ScopedGetDC(HWND hwnd)
: hwnd_(hwnd),
hdc_(GetDC(hwnd)) {
- DCHECK(!hwnd_ || IsWindow(hwnd_));
- DCHECK(hdc_);
+ if (hwnd_) {
+ DCHECK(IsWindow(hwnd_));
+ DCHECK(hdc_);
+ } else {
+ // If GetDC(NULL) returns NULL, something really bad has happened, like
+ // GDI handle exhaustion. In this case Chrome is going to behave badly no
+ // matter what, so we may as well just force a crash now.
+ CHECK(hdc_);
+ }
}
~ScopedGetDC() {
diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc
index 558f0ad..d27b578 100644
--- a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc
+++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc
@@ -20,6 +20,8 @@
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/win/iat_patch_function.h"
+#include "base/win/scoped_hdc.h"
+#include "base/win/scoped_select_object.h"
#include "base/win/windows_version.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
@@ -482,10 +484,10 @@ OmniboxViewWin::OmniboxViewWin(AutocompleteEditController* controller,
reinterpret_cast<LPARAM>(&WordBreakProc));
// Get the metrics for the font.
- HDC hdc = ::GetDC(NULL);
- HGDIOBJ old_font = SelectObject(hdc, font_.GetNativeFont());
+ base::win::ScopedGetDC screen_dc(NULL);
+ base::win::ScopedSelectObject(screen_dc, font_.GetNativeFont());
TEXTMETRIC tm = {0};
- GetTextMetrics(hdc, &tm);
+ GetTextMetrics(screen_dc, &tm);
int cap_height = font_.GetBaseline() - tm.tmInternalLeading;
// The ratio of a font's x-height to its cap height. Sadly, Windows
// doesn't provide a true value for a font's x-height in its text
@@ -502,11 +504,8 @@ OmniboxViewWin::OmniboxViewWin(AutocompleteEditController* controller,
// Get the number of twips per pixel, which we need below to offset our text
// by the desired number of pixels.
- const long kTwipsPerPixel = kTwipsPerInch / GetDeviceCaps(hdc, LOGPIXELSY);
- // It's unsafe to delete a DC with a non-stock object selected, so restore the
- // original font.
- SelectObject(hdc, old_font);
- ::ReleaseDC(NULL, hdc);
+ const long kTwipsPerPixel =
+ kTwipsPerInch / GetDeviceCaps(screen_dc, LOGPIXELSY);
// Set the default character style -- adjust to our desired baseline.
CHARFORMAT cf = {0};