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..a4c0ac6b9950c0208485d66fb7a76f9e3f4786cb |
--- /dev/null |
+++ b/chrome/browser/net/client_hints.cc |
@@ -0,0 +1,62 @@ |
+// 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 "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)); |
+ 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() { |
+ 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))); |
+} |
+ |
+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) { |
+ 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_ = std::string("dpr=") + device_pixel_ratio; |
+ } |
+} |
+ |