OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/stringprintf.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "ui/gfx/screen.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 void FetchScreenInfoOnUIThread(ClientHints::ScreenInfo* info) { |
| 15 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 16 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); |
| 17 info->width = display.GetSizeInPixel().width(); |
| 18 info->height = display.GetSizeInPixel().height(); |
| 19 info->pixel_ratio = display.device_scale_factor(); |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 ClientHints::ClientHints() : |
| 25 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 26 } |
| 27 |
| 28 ClientHints::~ClientHints() { |
| 29 } |
| 30 |
| 31 void ClientHints::Init() { |
| 32 RetrieveScreenInfo(); |
| 33 } |
| 34 |
| 35 bool ClientHints::RetrieveScreenInfo() { |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 37 ClientHints::ScreenInfo* info = new ClientHints::ScreenInfo(); |
| 38 return content::BrowserThread::PostTaskAndReply( |
| 39 content::BrowserThread::UI, |
| 40 FROM_HERE, |
| 41 base::Bind(&FetchScreenInfoOnUIThread, info), |
| 42 base::Bind(&ClientHints::UpdateScreenInfo, weak_ptr_factory_.GetWeakPtr(), |
| 43 base::Owned(info))); |
| 44 } |
| 45 |
| 46 const std::string& ClientHints::GetScreenInfoHints() { |
| 47 return screen_hints_; |
| 48 } |
| 49 |
| 50 void ClientHints::UpdateScreenInfo(ClientHints::ScreenInfo* info) { |
| 51 if (info->height > 0 && info->width > 0 && info->pixel_ratio > 0.0) { |
| 52 screen_hints_ = base::StringPrintf( |
| 53 "dh=%d, dw=%d, dpr=%0.3g", |
| 54 info->height, info->width, info->pixel_ratio); |
| 55 } |
| 56 } |
| 57 |
OLD | NEW |