Chromium Code Reviews| Index: chrome/browser/net/client_hints.cc |
| diff --git a/chrome/browser/net/client_hints.cc b/chrome/browser/net/client_hints.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..67e61eebd84a4df681f19f8f43d1a5c212c86a81 |
| --- /dev/null |
| +++ b/chrome/browser/net/client_hints.cc |
| @@ -0,0 +1,67 @@ |
| +// Copyright 2013 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/net/client_hints.h" |
| + |
| +#include <locale> |
| + |
| +#include "base/bind.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "ui/gfx/screen.h" |
| + |
| +namespace { |
| + |
| +void FetchScreenInfoOnUIThread(float* device_pixel_ratio) { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + // TODO(bengr): Consider multi-display scenarios. |
| + gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); |
| + *device_pixel_ratio = display.device_scale_factor(); |
| +} |
| + |
| +} // namespace |
| + |
| +ClientHints::ClientHints() |
| + : weak_ptr_factory_(this) { |
| +} |
| + |
| +ClientHints::~ClientHints() { |
| +} |
| + |
| +void ClientHints::Init() { |
| + // TODO(bengr): Observe the device pixel ratio in case it changes during |
| + // the Chrome session. |
| + RetrieveScreenInfo(); |
| +} |
| + |
| +bool ClientHints::RetrieveScreenInfo() { |
| + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| + float* devicePixelRatio = new float; |
| + return content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&FetchScreenInfoOnUIThread, devicePixelRatio), |
| + base::Bind(&ClientHints::UpdateScreenInfo, weak_ptr_factory_.GetWeakPtr(), |
| + base::Owned(devicePixelRatio))); |
|
mmenke
2013/09/25 16:26:19
How about just using PostTaskAndReplyWithResult in
bengr
2013/09/25 17:25:52
Done. I hadn't done this before b/c I was returnin
|
| +} |
| + |
| +const std::string& ClientHints::GetString() const { |
| + return screen_hints_; |
| +} |
| + |
| +void ClientHints::UpdateScreenInfo(const float* device_pixel_ratio_value) { |
| + if (device_pixel_ratio_value && *device_pixel_ratio_value > 0.0) { |
|
mmenke
2013/09/25 16:26:19
If something can't happen, you shouldn't be testin
bengr
2013/09/25 17:25:52
Done.
|
| + std::string device_pixel_ratio = base::StringPrintf("%.2f", |
| + *device_pixel_ratio_value); |
| + // Make sure the Client Hints value doesn't change |
| + // according to the machine's locale |
| + std::locale locale; |
| + for (std::string::iterator it = device_pixel_ratio.begin(); |
| + it != device_pixel_ratio.end(); ++it) |
| + if (!std::isdigit(*it, locale)) |
| + *it = '.'; |
| + screen_hints_ = device_pixel_ratio; |
| + } |
| +} |
| + |