diff options
author | jknotten@chromium.org <jknotten@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-12 14:21:56 +0000 |
---|---|---|
committer | jknotten@chromium.org <jknotten@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-12 14:21:56 +0000 |
commit | e0c7405b8f24e6c0096285446f94f729e63107df (patch) | |
tree | 1761e4f23e2e9060ff7bfa5a9026a4937d73e312 | |
parent | 91aaa217affb5e4123383700afffe1a90b84c6d1 (diff) | |
download | chromium_src-e0c7405b8f24e6c0096285446f94f729e63107df.zip chromium_src-e0c7405b8f24e6c0096285446f94f729e63107df.tar.gz chromium_src-e0c7405b8f24e6c0096285446f94f729e63107df.tar.bz2 |
Call CoInitialize before using Win 7 Location API.
BUG=66822
TEST=None
Review URL: http://codereview.chromium.org/6115004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71166 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 46 insertions, 24 deletions
diff --git a/chrome/browser/geolocation/win7_location_api_unittest_win.cc b/chrome/browser/geolocation/win7_location_api_unittest_win.cc index a22b99b..cfc35c3 100644 --- a/chrome/browser/geolocation/win7_location_api_unittest_win.cc +++ b/chrome/browser/geolocation/win7_location_api_unittest_win.cc @@ -293,9 +293,8 @@ class GeolocationApiWin7Tests : public testing::Test { Win7LocationApi* CreateMock() { MockLocation* locator = new MockLocation(); locator_ = locator; - return new Win7LocationApi(NULL, - &MockPropVariantToDoubleFunction, - locator); + return Win7LocationApi::CreateForTesting(&MockPropVariantToDoubleFunction, + locator); } scoped_ptr<Win7LocationApi> api_; diff --git a/chrome/browser/geolocation/win7_location_api_win.cc b/chrome/browser/geolocation/win7_location_api_win.cc index 7b6c3ca..7df0340 100644 --- a/chrome/browser/geolocation/win7_location_api_win.cc +++ b/chrome/browser/geolocation/win7_location_api_win.cc @@ -9,6 +9,7 @@ #include "base/file_path.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/scoped_ptr.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/geoposition.h" @@ -26,13 +27,18 @@ HINSTANCE LoadWin7Library(const string16& lib_name) { } } -Win7LocationApi::Win7LocationApi( - HINSTANCE prop_library, +Win7LocationApi::Win7LocationApi() + : prop_lib_(0), + PropVariantToDouble_function_(0), + locator_(0) { +} + +void Win7LocationApi::Init(HINSTANCE prop_library, PropVariantToDoubleFunction PropVariantToDouble_function, - ILocation* locator) - : prop_lib_(prop_library), - PropVariantToDouble_function_(PropVariantToDouble_function), - locator_(locator) { + ILocation* locator) { + prop_lib_ = prop_library; + PropVariantToDouble_function_ = PropVariantToDouble_function; + locator_ = locator; } Win7LocationApi::~Win7LocationApi() { @@ -44,6 +50,8 @@ Win7LocationApi* Win7LocationApi::Create() { if (!CommandLine::ForCurrentProcess() ->HasSwitch(switches::kExperimentalLocationFeatures)) return NULL; + + scoped_ptr<Win7LocationApi> result(new Win7LocationApi); // Load probsys.dll string16 lib_needed = L"propsys.dll"; HINSTANCE prop_lib = LoadWin7Library(lib_needed); @@ -69,9 +77,16 @@ Win7LocationApi* Win7LocationApi::Create() { } IID reports_needed[] = { IID_ILatLongReport }; result_type = locator->RequestPermissions(NULL, reports_needed, 1, TRUE); - return new Win7LocationApi(prop_lib, - PropVariantToDouble_function, - locator); + result->Init(prop_lib, PropVariantToDouble_function, locator); + return result.release(); +} + +Win7LocationApi* Win7LocationApi::CreateForTesting( + PropVariantToDoubleFunction PropVariantToDouble_function, + ILocation* locator) { + Win7LocationApi* result = new Win7LocationApi; + result->Init(NULL, PropVariantToDouble_function, locator); + return result; } void Win7LocationApi::GetPosition(Geoposition* position) { @@ -101,19 +116,19 @@ bool Win7LocationApi::GetPositionIfFixed(Geoposition* position) { CComPtr<ILatLongReport> lat_long_report; result_type = locator_->GetReport(IID_ILatLongReport, &location_report); // Checks to see if location access is allowed. - if (result_type == E_ACCESSDENIED) + if (result_type == E_ACCESSDENIED) position->error_code = Geoposition::ERROR_CODE_PERMISSION_DENIED; // Checks for any other errors while requesting a location report. - if(!SUCCEEDED(result_type)) + if (!SUCCEEDED(result_type)) return false; result_type = location_report->QueryInterface(&lat_long_report); - if(!SUCCEEDED(result_type)) + if (!SUCCEEDED(result_type)) return false; result_type = lat_long_report->GetLatitude(&position->latitude); - if(!SUCCEEDED(result_type)) + if (!SUCCEEDED(result_type)) return false; result_type = lat_long_report->GetLongitude(&position->longitude); - if(!SUCCEEDED(result_type)) + if (!SUCCEEDED(result_type)) return false; result_type = lat_long_report->GetErrorRadius(&position->accuracy); if (!SUCCEEDED(result_type) || position->accuracy <= 0) @@ -124,7 +139,7 @@ bool Win7LocationApi::GetPositionIfFixed(Geoposition* position) { position->altitude = temp_dbl; result_type = lat_long_report->GetAltitudeError(&temp_dbl); if (SUCCEEDED(result_type)) - position->altitude_accuracy = temp_dbl; + position->altitude_accuracy = temp_dbl; PROPVARIANT heading; PropVariantInit(&heading); result_type = lat_long_report->GetValue( diff --git a/chrome/browser/geolocation/win7_location_api_win.h b/chrome/browser/geolocation/win7_location_api_win.h index 78bbed4..afdf1b8 100644 --- a/chrome/browser/geolocation/win7_location_api_win.h +++ b/chrome/browser/geolocation/win7_location_api_win.h @@ -11,6 +11,7 @@ #include <sensors.h> #include <Windows.h> +#include "app/win/scoped_com_initializer.h" #include "base/time.h" struct Geoposition; @@ -21,28 +22,35 @@ typedef HRESULT (WINAPI* PropVariantToDoubleFunction) class Win7LocationApi { public: - // Public for testing. See Create() below for normal usage. - Win7LocationApi(HINSTANCE prop_library, - PropVariantToDoubleFunction PropVariantToDouble_function, - ILocation* locator); virtual ~Win7LocationApi(); - // Attempts to load propsys.dll, initialise |location_| and requests the user // for access to location information. Creates and returns ownership of an // instance of Win7LocationApi if all succeed. static Win7LocationApi* Create(); + static Win7LocationApi* CreateForTesting( + PropVariantToDoubleFunction PropVariantToDouble_function, + ILocation* locator); // Gives the best available position. // Returns false if no valid position is available. virtual void GetPosition(Geoposition* position); // Changes the "accuracy" needed. Affects power levels of devices. virtual bool SetHighAccuracy(bool acc); + protected: + Win7LocationApi(); + private: + void Init(HINSTANCE prop_library, + PropVariantToDoubleFunction PropVariantToDouble_function, + ILocation* locator); + // Provides the best position fix if one is available. // Does this by requesting a location report and querying it to obtain // location information. virtual bool GetPositionIfFixed(Geoposition* position); + // Ensure that COM has been initialized for this thread. + app::win::ScopedCOMInitializer com_initializer_; // ILocation object that lets us communicate with the Location and // Sensors platform. CComPtr<ILocation> locator_; diff --git a/chrome/browser/geolocation/win7_location_provider_unittest_win.cc b/chrome/browser/geolocation/win7_location_provider_unittest_win.cc index ababadd..a930db1 100644 --- a/chrome/browser/geolocation/win7_location_provider_unittest_win.cc +++ b/chrome/browser/geolocation/win7_location_provider_unittest_win.cc @@ -49,7 +49,7 @@ class MockWin7LocationApi : public Win7LocationApi { } private: - MockWin7LocationApi() : Win7LocationApi(NULL, NULL, NULL) { + MockWin7LocationApi() { ON_CALL(*this, GetPosition(_)) .WillByDefault(Invoke(this, &MockWin7LocationApi::GetPositionValid)); |