OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/net/client_hints.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "ui/gfx/screen.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 void FetchScreenInfoOnUIThread(float* device_pixel_ratio) { |
| 15 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 16 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); |
| 17 *device_pixel_ratio = display.device_scale_factor(); |
| 18 } |
| 19 |
| 20 } // namespace |
| 21 |
| 22 ClientHints::ClientHints() |
| 23 : weak_ptr_factory_(this) { |
| 24 } |
| 25 |
| 26 ClientHints::~ClientHints() { |
| 27 } |
| 28 |
| 29 void ClientHints::Init() { |
| 30 RetrieveScreenInfo(); |
| 31 } |
| 32 |
| 33 bool ClientHints::RetrieveScreenInfo() { |
| 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 35 float* devicePixelRatio = new float; |
| 36 return content::BrowserThread::PostTaskAndReply( |
| 37 content::BrowserThread::UI, |
| 38 FROM_HERE, |
| 39 base::Bind(&FetchScreenInfoOnUIThread, devicePixelRatio), |
| 40 base::Bind(&ClientHints::UpdateScreenInfo, weak_ptr_factory_.GetWeakPtr(), |
| 41 base::Owned(devicePixelRatio))); |
| 42 } |
| 43 |
| 44 const std::string& ClientHints::GetString() const { |
| 45 return screen_hints_; |
| 46 } |
| 47 |
| 48 void ClientHints::UpdateScreenInfo(const float* device_pixel_ratio_value) { |
| 49 if (device_pixel_ratio_value && *device_pixel_ratio_value > 0.0) { |
| 50 std::string device_pixel_ratio = base::StringPrintf("%.2f", |
| 51 *device_pixel_ratio_value); |
| 52 // Make sure the Client Hints value doesn't change |
| 53 // according to the machine's locale |
| 54 std::locale locale; |
| 55 for (std::string::iterator it = device_pixel_ratio.begin(); |
| 56 it != device_pixel_ratio.end(); ++it) |
| 57 if (!std::isdigit(*it, locale)) |
| 58 *it = '.'; |
| 59 screen_hints_ = std::string("dpr=") + device_pixel_ratio; |
| 60 } |
| 61 } |
| 62 |
OLD | NEW |