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 <locale> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "ui/gfx/screen.h" | |
mmenke
2013/09/25 17:37:40
nit: #include "base/task_runner_util.h"
bengr
2013/09/25 20:04:12
Done.
| |
13 | |
14 namespace { | |
15 | |
16 float FetchScreenInfoOnUIThread() { | |
17 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
18 // TODO(bengr): Consider multi-display scenarios. | |
19 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay(); | |
20 return display.device_scale_factor(); | |
21 } | |
22 | |
23 } // namespace | |
24 | |
25 const char* ClientHints::kDevicePixelRatioHeader = "CH-DPR"; | |
26 | |
27 ClientHints::ClientHints() | |
28 : weak_ptr_factory_(this) { | |
29 } | |
30 | |
31 ClientHints::~ClientHints() { | |
32 } | |
33 | |
34 void ClientHints::Init() { | |
35 // TODO(bengr): Observe the device pixel ratio in case it changes during | |
36 // the Chrome session. | |
37 RetrieveScreenInfo(); | |
38 } | |
39 | |
40 bool ClientHints::RetrieveScreenInfo() { | |
41 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
42 return content::BrowserThread::PostTaskAndReplyWithResult( | |
43 content::BrowserThread::UI, | |
44 FROM_HERE, | |
45 base::Bind(&FetchScreenInfoOnUIThread), | |
46 base::Bind( | |
47 &ClientHints::UpdateScreenInfo, weak_ptr_factory_.GetWeakPtr())); | |
48 } | |
49 | |
50 const std::string& ClientHints::GetDevicePixelRatioHeader() const { | |
51 return screen_hints_; | |
52 } | |
53 | |
54 void ClientHints::UpdateScreenInfo(float device_pixel_ratio_value) { | |
55 if (device_pixel_ratio_value <= 0.0) | |
56 return; | |
57 std::string device_pixel_ratio = base::StringPrintf("%.2f", | |
58 device_pixel_ratio_value); | |
59 // Make sure the Client Hints value doesn't change | |
60 // according to the machine's locale | |
61 std::locale locale; | |
62 for (std::string::iterator it = device_pixel_ratio.begin(); | |
63 it != device_pixel_ratio.end(); ++it) | |
mmenke
2013/09/25 17:37:40
nit: Use braces {}.
bengr
2013/09/25 20:04:12
Done.
| |
64 if (!std::isdigit(*it, locale)) | |
65 *it = '.'; | |
66 screen_hints_ = device_pixel_ratio; | |
67 } | |
68 | |
OLD | NEW |