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