| OLD | NEW |
| 1 // Copyright 2008, Google Inc. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // Redistribution and use in source and binary forms, with or without | 3 // found in the LICENSE file. |
| 4 // modification, are permitted provided that the following conditions are met: | |
| 5 // | |
| 6 // 1. Redistributions of source code must retain the above copyright notice, | |
| 7 // this list of conditions and the following disclaimer. | |
| 8 // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 9 // this list of conditions and the following disclaimer in the documentation | |
| 10 // and/or other materials provided with the distribution. | |
| 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be | |
| 12 // used to endorse or promote products derived from this software without | |
| 13 // specific prior written permission. | |
| 14 // | |
| 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 | 4 |
| 26 // TODO(joth): port to chromium | 5 // TODO(joth): port to chromium |
| 27 #if 0 | 6 #if 0 |
| 28 | 7 |
| 29 #include "gears/geolocation/location_provider_pool.h" | 8 #include "gears/geolocation/location_provider_pool.h" |
| 30 | 9 |
| 31 #include <assert.h> | 10 #include <assert.h> |
| 32 | 11 |
| 33 static const char16 *kMockString = STRING16(L"MOCK"); | 12 static const char16 *kMockString = STRING16(L"MOCK"); |
| 34 static const char16 *kGpsString = STRING16(L"GPS"); | 13 static const char16 *kGpsString = STRING16(L"GPS"); |
| 35 static const char16 *kNetworkString = STRING16(L"NETWORK"); | 14 static const char16 *kNetworkString = STRING16(L"NETWORK"); |
| 36 | 15 |
| 37 // Local functions | 16 // Local functions |
| 38 static std::string16 MakeKey(const std::string16 &type, | 17 static string16 MakeKey(const string16& type, |
| 39 const std::string16 &url, | 18 const string16& url, |
| 40 const std::string16 &host, | 19 const string16& host, |
| 41 const std::string16 &language); | 20 const string16& language); |
| 42 | 21 |
| 43 // static member variables | 22 // static member variables |
| 44 LocationProviderPool LocationProviderPool::instance_; | 23 LocationProviderPool LocationProviderPool::instance_; |
| 45 | 24 |
| 46 LocationProviderPool::LocationProviderPool() | 25 LocationProviderPool::LocationProviderPool() |
| 47 : use_mock_location_provider_(false) { | 26 : use_mock_location_provider_(false) { |
| 48 } | 27 } |
| 49 | 28 |
| 50 LocationProviderPool::~LocationProviderPool() { | 29 LocationProviderPool::~LocationProviderPool() { |
| 51 #if BROWSER_IEMOBILE | 30 #if BROWSER_IEMOBILE |
| 52 // The lack of unload monitoring on IE Mobile on WinCE means that we may leak | 31 // The lack of unload monitoring on IE Mobile on WinCE means that we may leak |
| 53 // providers. | 32 // providers. |
| 54 #else | 33 #else |
| 55 assert(providers_.empty()); | 34 DCHECK(providers_.empty()); |
| 56 #endif // BROWSER_IEMOBILE | 35 #endif // BROWSER_IEMOBILE |
| 57 } | 36 } |
| 58 | 37 |
| 59 // static | 38 // static |
| 60 LocationProviderPool *LocationProviderPool::GetInstance() { | 39 LocationProviderPool *LocationProviderPool::GetInstance() { |
| 61 return &instance_; | 40 return &instance_; |
| 62 } | 41 } |
| 63 | 42 |
| 64 LocationProviderBase *LocationProviderPool::Register( | 43 LocationProviderBase* LocationProviderPool::Register( |
| 65 BrowsingContext *browsing_context, | 44 BrowsingContext* browsing_context, |
| 66 const std::string16 &type, | 45 const string16& type, |
| 67 const std::string16 &url, | 46 const string16& url, |
| 68 const std::string16 &host, | 47 const string16& host, |
| 69 bool request_address, | 48 bool request_address, |
| 70 const std::string16 &language, | 49 const string16& language, |
| 71 LocationProviderBase::ListenerInterface *listener) { | 50 LocationProviderBase::ListenerInterface* listener) { |
| 72 assert(listener); | 51 DCHECK(listener); |
| 73 MutexLock lock(&providers_mutex_); | 52 MutexLock lock(&providers_mutex_); |
| 74 std::string16 key = MakeKey(type, url, host, language); | 53 string16 key = MakeKey(type, url, host, language); |
| 75 ProviderMap::iterator iter = providers_.find(key); | 54 ProviderMap::iterator iter = providers_.find(key); |
| 76 if (iter == providers_.end()) { | 55 if (iter == providers_.end()) { |
| 77 LocationProviderBase *provider = NewProvider(browsing_context, type, url, | 56 LocationProviderBase *provider = NewProvider(browsing_context, type, url, |
| 78 host, language); | 57 host, language); |
| 79 if (!provider) { | 58 if (!provider) { |
| 80 return NULL; | 59 return NULL; |
| 81 } | 60 } |
| 82 std::pair<ProviderMap::iterator, bool> result = | 61 std::pair<ProviderMap::iterator, bool> result = |
| 83 providers_.insert( | 62 providers_.insert( |
| 84 std::make_pair(key, | 63 std::make_pair(key, |
| 85 std::make_pair(provider, new RefCount()))); | 64 std::make_pair(provider, new RefCount()))); |
| 86 assert(result.second); | 65 DCHECK(result.second); |
| 87 iter = result.first; | 66 iter = result.first; |
| 88 } | 67 } |
| 89 LocationProviderBase *provider = iter->second.first; | 68 LocationProviderBase *provider = iter->second.first; |
| 90 assert(provider); | 69 DCHECK(provider); |
| 91 provider->RegisterListener(listener, request_address); | 70 provider->RegisterListener(listener, request_address); |
| 92 RefCount *count = iter->second.second; | 71 RefCount* count = iter->second.second; |
| 93 assert(count); | 72 DCHECK(count); |
| 94 count->Ref(); | 73 count->Ref(); |
| 95 return provider; | 74 return provider; |
| 96 } | 75 } |
| 97 | 76 |
| 98 bool LocationProviderPool::Unregister( | 77 bool LocationProviderPool::Unregister( |
| 99 LocationProviderBase *provider, | 78 LocationProviderBase* provider, |
| 100 LocationProviderBase::ListenerInterface *listener) { | 79 LocationProviderBase::ListenerInterface* listener) { |
| 101 assert(provider); | 80 DCHECK(provider); |
| 102 assert(listener); | 81 DCHECK(listener); |
| 103 MutexLock lock(&providers_mutex_); | 82 MutexLock lock(&providers_mutex_); |
| 104 for (ProviderMap::iterator iter = providers_.begin(); | 83 for (ProviderMap::iterator iter = providers_.begin(); |
| 105 iter != providers_.end(); | 84 iter != providers_.end(); |
| 106 ++iter) { | 85 ++iter) { |
| 107 LocationProviderBase *current_provider = iter->second.first; | 86 LocationProviderBase* current_provider = iter->second.first; |
| 108 if (current_provider == provider) { | 87 if (current_provider == provider) { |
| 109 current_provider->UnregisterListener(listener); | 88 current_provider->UnregisterListener(listener); |
| 110 RefCount *count = iter->second.second; | 89 RefCount *count = iter->second.second; |
| 111 assert(count); | 90 DCHECK(count); |
| 112 if (count->Unref()) { | 91 if (count->Unref()) { |
| 113 delete current_provider; | 92 delete current_provider; |
| 114 delete count; | 93 delete count; |
| 115 providers_.erase(iter); | 94 providers_.erase(iter); |
| 116 } | 95 } |
| 117 return true; | 96 return true; |
| 118 } | 97 } |
| 119 } | 98 } |
| 120 return false; | 99 return false; |
| 121 } | 100 } |
| 122 | 101 |
| 123 void LocationProviderPool::UseMockLocationProvider( | 102 void LocationProviderPool::UseMockLocationProvider( |
| 124 bool use_mock_location_provider) { | 103 bool use_mock_location_provider) { |
| 125 use_mock_location_provider_ = use_mock_location_provider; | 104 use_mock_location_provider_ = use_mock_location_provider; |
| 126 } | 105 } |
| 127 | 106 |
| 128 LocationProviderBase *LocationProviderPool::NewProvider( | 107 LocationProviderBase* LocationProviderPool::NewProvider( |
| 129 BrowsingContext *browsing_context, | 108 BrowsingContext* browsing_context, |
| 130 const std::string16 &type, | 109 const string16& type, |
| 131 const std::string16 &url, | 110 const string16& url, |
| 132 const std::string16 &host, | 111 const string16& host, |
| 133 const std::string16 &language) { | 112 const string16& language) { |
| 134 if (type == kMockString) { | 113 if (type == kMockString) { |
| 135 // use_mock_location_provider_ can only be set to true in a build that uses | 114 // use_mock_location_provider_ can only be set to true in a build that uses |
| 136 // USING_CCTESTS. | 115 // USING_CCTESTS. |
| 137 #if USING_CCTESTS | 116 #if USING_CCTESTS |
| 138 if (use_mock_location_provider_) { | 117 if (use_mock_location_provider_) { |
| 139 return NewMockLocationProvider(); | 118 return NewMockLocationProvider(); |
| 140 } else { | 119 } else { |
| 141 return NULL; | 120 return NULL; |
| 142 } | 121 } |
| 143 #else | 122 #else |
| 144 return NULL; | 123 return NULL; |
| 145 #endif // USING_CCTESTS | 124 #endif // USING_CCTESTS |
| 146 } else if (type == kGpsString) { | 125 } else if (type == kGpsString) { |
| 147 return NewGpsLocationProvider(browsing_context, url, host, language); | 126 return NewGpsLocationProvider(browsing_context, url, host, language); |
| 148 } else if (type == kNetworkString) { | 127 } else if (type == kNetworkString) { |
| 149 return NewNetworkLocationProvider(browsing_context, url, host, language); | 128 return NewNetworkLocationProvider(browsing_context, url, host, language); |
| 150 } | 129 } |
| 151 assert(false); | 130 DCHECK(false); |
| 152 return NULL; | 131 return NULL; |
| 153 } | 132 } |
| 154 | 133 |
| 155 // Local function | 134 // Local function |
| 156 static std::string16 MakeKey(const std::string16 &type, | 135 static string16 MakeKey(const string16& type, |
| 157 const std::string16 &url, | 136 const string16& url, |
| 158 const std::string16 &host, | 137 const string16& host, |
| 159 const std::string16 &language) { | 138 const string16& language) { |
| 160 // Network requests are made from a specific host and use a specific language. | 139 // Network requests are made from a specific host and use a specific language. |
| 161 // Therefore we must key network and GPS providers on server URL, host and | 140 // Therefore we must key network and GPS providers on server URL, host and |
| 162 // language. | 141 // language. |
| 163 if (type == kMockString) { | 142 if (type == kMockString) { |
| 164 return type; | 143 return type; |
| 165 } else if (type == kGpsString || type == kNetworkString) { | 144 } else if (type == kGpsString || type == kNetworkString) { |
| 166 std::string16 key = type; | 145 string16 key = type; |
| 167 if (!url.empty()) { | 146 if (!url.empty()) { |
| 168 key += STRING16(L" url=") + url; | 147 key += STRING16(L" url=") + url; |
| 169 } | 148 } |
| 170 if (!host.empty()) { | 149 if (!host.empty()) { |
| 171 key += STRING16(L" host=") + host; | 150 key += STRING16(L" host=") + host; |
| 172 } | 151 } |
| 173 if (!language.empty()) { | 152 if (!language.empty()) { |
| 174 key += STRING16(L" language=") + language; | 153 key += STRING16(L" language=") + language; |
| 175 } | 154 } |
| 176 return key; | 155 return key; |
| 177 } | 156 } |
| 178 assert(false); | 157 DCHECK(false); |
| 179 return STRING16(L""); | 158 return STRING16(L""); |
| 180 } | 159 } |
| 181 | 160 |
| 182 #endif // if 0 | 161 #endif // if 0 |
| OLD | NEW |