OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/geolocation/win7_location_api_win.h" | 5 #include "chrome/browser/geolocation/win7_location_api_win.h" |
6 | 6 |
7 #include "base/base_paths_win.h" | 7 #include "base/base_paths_win.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/scoped_ptr.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/common/geoposition.h" | 14 #include "chrome/common/geoposition.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 const double kKnotsToMetresPerSecondConversionFactor = 0.5144; | 17 const double kKnotsToMetresPerSecondConversionFactor = 0.5144; |
17 | 18 |
18 void ConvertKnotsToMetresPerSecond(double* knots) { | 19 void ConvertKnotsToMetresPerSecond(double* knots) { |
19 *knots *= kKnotsToMetresPerSecondConversionFactor; | 20 *knots *= kKnotsToMetresPerSecondConversionFactor; |
20 } | 21 } |
21 | 22 |
22 HINSTANCE LoadWin7Library(const string16& lib_name) { | 23 HINSTANCE LoadWin7Library(const string16& lib_name) { |
23 FilePath sys_dir; | 24 FilePath sys_dir; |
24 PathService::Get(base::DIR_SYSTEM, &sys_dir); | 25 PathService::Get(base::DIR_SYSTEM, &sys_dir); |
25 return LoadLibrary(sys_dir.Append(lib_name).value().c_str()); | 26 return LoadLibrary(sys_dir.Append(lib_name).value().c_str()); |
26 } | 27 } |
27 } | 28 } |
28 | 29 |
29 Win7LocationApi::Win7LocationApi( | 30 Win7LocationApi::Win7LocationApi() |
30 HINSTANCE prop_library, | 31 : prop_lib_(0), |
| 32 PropVariantToDouble_function_(0), |
| 33 locator_(0) { |
| 34 } |
| 35 |
| 36 void Win7LocationApi::Init(HINSTANCE prop_library, |
31 PropVariantToDoubleFunction PropVariantToDouble_function, | 37 PropVariantToDoubleFunction PropVariantToDouble_function, |
32 ILocation* locator) | 38 ILocation* locator) { |
33 : prop_lib_(prop_library), | 39 prop_lib_ = prop_library; |
34 PropVariantToDouble_function_(PropVariantToDouble_function), | 40 PropVariantToDouble_function_ = PropVariantToDouble_function; |
35 locator_(locator) { | 41 locator_ = locator; |
36 } | 42 } |
37 | 43 |
38 Win7LocationApi::~Win7LocationApi() { | 44 Win7LocationApi::~Win7LocationApi() { |
39 if (prop_lib_ != NULL) | 45 if (prop_lib_ != NULL) |
40 FreeLibrary(prop_lib_); | 46 FreeLibrary(prop_lib_); |
41 } | 47 } |
42 | 48 |
43 Win7LocationApi* Win7LocationApi::Create() { | 49 Win7LocationApi* Win7LocationApi::Create() { |
44 if (!CommandLine::ForCurrentProcess() | 50 if (!CommandLine::ForCurrentProcess() |
45 ->HasSwitch(switches::kExperimentalLocationFeatures)) | 51 ->HasSwitch(switches::kExperimentalLocationFeatures)) |
46 return NULL; | 52 return NULL; |
| 53 |
| 54 scoped_ptr<Win7LocationApi> result(new Win7LocationApi); |
47 // Load probsys.dll | 55 // Load probsys.dll |
48 string16 lib_needed = L"propsys.dll"; | 56 string16 lib_needed = L"propsys.dll"; |
49 HINSTANCE prop_lib = LoadWin7Library(lib_needed); | 57 HINSTANCE prop_lib = LoadWin7Library(lib_needed); |
50 if (!prop_lib) | 58 if (!prop_lib) |
51 return NULL; | 59 return NULL; |
52 // Get pointer to function. | 60 // Get pointer to function. |
53 PropVariantToDoubleFunction PropVariantToDouble_function; | 61 PropVariantToDoubleFunction PropVariantToDouble_function; |
54 PropVariantToDouble_function = | 62 PropVariantToDouble_function = |
55 reinterpret_cast<PropVariantToDoubleFunction>( | 63 reinterpret_cast<PropVariantToDoubleFunction>( |
56 GetProcAddress(prop_lib, "PropVariantToDouble")); | 64 GetProcAddress(prop_lib, "PropVariantToDouble")); |
57 if (!PropVariantToDouble_function) { | 65 if (!PropVariantToDouble_function) { |
58 FreeLibrary(prop_lib); | 66 FreeLibrary(prop_lib); |
59 return NULL; | 67 return NULL; |
60 } | 68 } |
61 // Create the ILocation object that receives location reports. | 69 // Create the ILocation object that receives location reports. |
62 HRESULT result_type; | 70 HRESULT result_type; |
63 CComPtr<ILocation> locator; | 71 CComPtr<ILocation> locator; |
64 result_type = CoCreateInstance( | 72 result_type = CoCreateInstance( |
65 CLSID_Location, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&locator)); | 73 CLSID_Location, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&locator)); |
66 if (!SUCCEEDED(result_type)) { | 74 if (!SUCCEEDED(result_type)) { |
67 FreeLibrary(prop_lib); | 75 FreeLibrary(prop_lib); |
68 return NULL; | 76 return NULL; |
69 } | 77 } |
70 IID reports_needed[] = { IID_ILatLongReport }; | 78 IID reports_needed[] = { IID_ILatLongReport }; |
71 result_type = locator->RequestPermissions(NULL, reports_needed, 1, TRUE); | 79 result_type = locator->RequestPermissions(NULL, reports_needed, 1, TRUE); |
72 return new Win7LocationApi(prop_lib, | 80 result->Init(prop_lib, PropVariantToDouble_function, locator); |
73 PropVariantToDouble_function, | 81 return result.release(); |
74 locator); | 82 } |
| 83 |
| 84 Win7LocationApi* Win7LocationApi::CreateForTesting( |
| 85 PropVariantToDoubleFunction PropVariantToDouble_function, |
| 86 ILocation* locator) { |
| 87 Win7LocationApi* result = new Win7LocationApi; |
| 88 result->Init(NULL, PropVariantToDouble_function, locator); |
| 89 return result; |
75 } | 90 } |
76 | 91 |
77 void Win7LocationApi::GetPosition(Geoposition* position) { | 92 void Win7LocationApi::GetPosition(Geoposition* position) { |
78 DCHECK(position); | 93 DCHECK(position); |
79 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 94 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
80 if (!locator_) | 95 if (!locator_) |
81 return; | 96 return; |
82 // Try to get a position fix | 97 // Try to get a position fix |
83 if (!GetPositionIfFixed(position)) | 98 if (!GetPositionIfFixed(position)) |
84 return; | 99 return; |
85 position->error_code = Geoposition::ERROR_CODE_NONE; | 100 position->error_code = Geoposition::ERROR_CODE_NONE; |
86 if (!position->IsValidFix()) { | 101 if (!position->IsValidFix()) { |
87 // GetPositionIfFixed returned true, yet we've not got a valid fix. | 102 // GetPositionIfFixed returned true, yet we've not got a valid fix. |
88 // This shouldn't happen; something went wrong in the conversion. | 103 // This shouldn't happen; something went wrong in the conversion. |
89 NOTREACHED() << "Invalid position from GetPositionIfFixed: lat,long " | 104 NOTREACHED() << "Invalid position from GetPositionIfFixed: lat,long " |
90 << position->latitude << "," << position->longitude | 105 << position->latitude << "," << position->longitude |
91 << " accuracy " << position->accuracy << " time " | 106 << " accuracy " << position->accuracy << " time " |
92 << position->timestamp.ToDoubleT(); | 107 << position->timestamp.ToDoubleT(); |
93 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 108 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
94 position->error_message = "Bad fix from Win7 provider"; | 109 position->error_message = "Bad fix from Win7 provider"; |
95 } | 110 } |
96 } | 111 } |
97 | 112 |
98 bool Win7LocationApi::GetPositionIfFixed(Geoposition* position) { | 113 bool Win7LocationApi::GetPositionIfFixed(Geoposition* position) { |
99 HRESULT result_type; | 114 HRESULT result_type; |
100 CComPtr<ILocationReport> location_report; | 115 CComPtr<ILocationReport> location_report; |
101 CComPtr<ILatLongReport> lat_long_report; | 116 CComPtr<ILatLongReport> lat_long_report; |
102 result_type = locator_->GetReport(IID_ILatLongReport, &location_report); | 117 result_type = locator_->GetReport(IID_ILatLongReport, &location_report); |
103 // Checks to see if location access is allowed. | 118 // Checks to see if location access is allowed. |
104 if (result_type == E_ACCESSDENIED) | 119 if (result_type == E_ACCESSDENIED) |
105 position->error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; | 120 position->error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; |
106 // Checks for any other errors while requesting a location report. | 121 // Checks for any other errors while requesting a location report. |
107 if(!SUCCEEDED(result_type)) | 122 if (!SUCCEEDED(result_type)) |
108 return false; | 123 return false; |
109 result_type = location_report->QueryInterface(&lat_long_report); | 124 result_type = location_report->QueryInterface(&lat_long_report); |
110 if(!SUCCEEDED(result_type)) | 125 if (!SUCCEEDED(result_type)) |
111 return false; | 126 return false; |
112 result_type = lat_long_report->GetLatitude(&position->latitude); | 127 result_type = lat_long_report->GetLatitude(&position->latitude); |
113 if(!SUCCEEDED(result_type)) | 128 if (!SUCCEEDED(result_type)) |
114 return false; | 129 return false; |
115 result_type = lat_long_report->GetLongitude(&position->longitude); | 130 result_type = lat_long_report->GetLongitude(&position->longitude); |
116 if(!SUCCEEDED(result_type)) | 131 if (!SUCCEEDED(result_type)) |
117 return false; | 132 return false; |
118 result_type = lat_long_report->GetErrorRadius(&position->accuracy); | 133 result_type = lat_long_report->GetErrorRadius(&position->accuracy); |
119 if (!SUCCEEDED(result_type) || position->accuracy <= 0) | 134 if (!SUCCEEDED(result_type) || position->accuracy <= 0) |
120 return false; | 135 return false; |
121 double temp_dbl; | 136 double temp_dbl; |
122 result_type = lat_long_report->GetAltitude(&temp_dbl); | 137 result_type = lat_long_report->GetAltitude(&temp_dbl); |
123 if (SUCCEEDED(result_type)) | 138 if (SUCCEEDED(result_type)) |
124 position->altitude = temp_dbl; | 139 position->altitude = temp_dbl; |
125 result_type = lat_long_report->GetAltitudeError(&temp_dbl); | 140 result_type = lat_long_report->GetAltitudeError(&temp_dbl); |
126 if (SUCCEEDED(result_type)) | 141 if (SUCCEEDED(result_type)) |
127 position->altitude_accuracy = temp_dbl; | 142 position->altitude_accuracy = temp_dbl; |
128 PROPVARIANT heading; | 143 PROPVARIANT heading; |
129 PropVariantInit(&heading); | 144 PropVariantInit(&heading); |
130 result_type = lat_long_report->GetValue( | 145 result_type = lat_long_report->GetValue( |
131 SENSOR_DATA_TYPE_TRUE_HEADING_DEGREES, &heading); | 146 SENSOR_DATA_TYPE_TRUE_HEADING_DEGREES, &heading); |
132 if (SUCCEEDED(result_type)) | 147 if (SUCCEEDED(result_type)) |
133 PropVariantToDouble_function_(heading, &position->heading); | 148 PropVariantToDouble_function_(heading, &position->heading); |
134 PROPVARIANT speed; | 149 PROPVARIANT speed; |
135 PropVariantInit(&speed); | 150 PropVariantInit(&speed); |
136 result_type = lat_long_report->GetValue( | 151 result_type = lat_long_report->GetValue( |
137 SENSOR_DATA_TYPE_SPEED_KNOTS, &speed); | 152 SENSOR_DATA_TYPE_SPEED_KNOTS, &speed); |
138 if (SUCCEEDED(result_type)) { | 153 if (SUCCEEDED(result_type)) { |
139 PropVariantToDouble_function_(speed, &position->speed); | 154 PropVariantToDouble_function_(speed, &position->speed); |
140 ConvertKnotsToMetresPerSecond(&position->speed); | 155 ConvertKnotsToMetresPerSecond(&position->speed); |
141 } | 156 } |
142 position->timestamp = base::Time::Now(); | 157 position->timestamp = base::Time::Now(); |
143 return true; | 158 return true; |
144 } | 159 } |
145 | 160 |
146 bool Win7LocationApi::SetHighAccuracy(bool acc) { | 161 bool Win7LocationApi::SetHighAccuracy(bool acc) { |
147 HRESULT result_type; | 162 HRESULT result_type; |
148 result_type = locator_->SetDesiredAccuracy(IID_ILatLongReport, | 163 result_type = locator_->SetDesiredAccuracy(IID_ILatLongReport, |
149 acc ? LOCATION_DESIRED_ACCURACY_HIGH : | 164 acc ? LOCATION_DESIRED_ACCURACY_HIGH : |
150 LOCATION_DESIRED_ACCURACY_DEFAULT); | 165 LOCATION_DESIRED_ACCURACY_DEFAULT); |
151 return SUCCEEDED(result_type); | 166 return SUCCEEDED(result_type); |
152 } | 167 } |
OLD | NEW |