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