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