| 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); | |
| 75 } | 82 } |
| 76 | 83 |
| 77 void Win7LocationApi::GetPosition(Geoposition* position) { | 84 void Win7LocationApi::GetPosition(Geoposition* position) { |
| 78 DCHECK(position); | 85 DCHECK(position); |
| 79 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 86 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 80 if (!locator_) | 87 if (!locator_) |
| 81 return; | 88 return; |
| 82 // Try to get a position fix | 89 // Try to get a position fix |
| 83 if (!GetPositionIfFixed(position)) | 90 if (!GetPositionIfFixed(position)) |
| 84 return; | 91 return; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return true; | 150 return true; |
| 144 } | 151 } |
| 145 | 152 |
| 146 bool Win7LocationApi::SetHighAccuracy(bool acc) { | 153 bool Win7LocationApi::SetHighAccuracy(bool acc) { |
| 147 HRESULT result_type; | 154 HRESULT result_type; |
| 148 result_type = locator_->SetDesiredAccuracy(IID_ILatLongReport, | 155 result_type = locator_->SetDesiredAccuracy(IID_ILatLongReport, |
| 149 acc ? LOCATION_DESIRED_ACCURACY_HIGH : | 156 acc ? LOCATION_DESIRED_ACCURACY_HIGH : |
| 150 LOCATION_DESIRED_ACCURACY_DEFAULT); | 157 LOCATION_DESIRED_ACCURACY_DEFAULT); |
| 151 return SUCCEEDED(result_type); | 158 return SUCCEEDED(result_type); |
| 152 } | 159 } |
| OLD | NEW |