diff options
author | alemate@chromium.org <alemate@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-19 02:36:01 +0000 |
---|---|---|
committer | alemate@chromium.org <alemate@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-19 02:36:01 +0000 |
commit | 9e844cf3385e4f71db681e77000eb0c9d0f88562 (patch) | |
tree | 92f451ba5bc721a80ecbef73ec04090b92576317 /chrome/browser/chromeos/geolocation/simple_geolocation_request.h | |
parent | e0326e79e26d4cfd14d2c0afb4ef1de48cec82d4 (diff) | |
download | chromium_src-9e844cf3385e4f71db681e77000eb0c9d0f88562.zip chromium_src-9e844cf3385e4f71db681e77000eb0c9d0f88562.tar.gz chromium_src-9e844cf3385e4f71db681e77000eb0c9d0f88562.tar.bz2 |
Implement chromeos::SimpleGeolocationProvider.
Implement chromeos::SimpleGeolocationProvider to be used in ChromeOS
code instead of WEB geolocation.
BUG=356270
TEST=manual
Review URL: https://codereview.chromium.org/237963004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@264913 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/geolocation/simple_geolocation_request.h')
-rw-r--r-- | chrome/browser/chromeos/geolocation/simple_geolocation_request.h | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/geolocation/simple_geolocation_request.h b/chrome/browser/chromeos/geolocation/simple_geolocation_request.h new file mode 100644 index 0000000..3cc47e3 --- /dev/null +++ b/chrome/browser/chromeos/geolocation/simple_geolocation_request.h @@ -0,0 +1,111 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ +#define CHROME_BROWSER_CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ + +#include "base/basictypes.h" +#include "base/callback.h" +#include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/threading/thread_checker.h" +#include "base/timer/timer.h" +#include "chrome/browser/chromeos/geolocation/geoposition.h" +#include "net/url_request/url_fetcher.h" +#include "net/url_request/url_fetcher_delegate.h" +#include "url/gurl.h" + +namespace net { +class URLRequestContextGetter; +} + +namespace chromeos { + +// Sends request to a server to get local geolocation information. +// It performs formatting of the request and interpretation of the response. +// Request is owned and destroyed by caller (usually SimpleGeolocationProvider). +// - If error occurs, request is retried until timeout. +// - On successul response, callback is called. +// - On timeout, callback with last (failed) position is called. +// (position.status is set to STATUS_TIMEOUT.) +// - If request is destroyed while callback has not beed called yet, request +// is silently cancelled. +class SimpleGeolocationRequest : private net::URLFetcherDelegate { + public: + // Called when a new geo geolocation information is available. + // The second argument indicates whether there was a server error or not. + // It is true when there was a server or network error - either no response + // or a 500 error code. + typedef base::Callback<void(const Geoposition& /* position*/, + bool /* server_error */, + const base::TimeDelta elapsed)> ResponseCallback; + + // |url| is the server address to which the request wil be sent. + // |timeout| retry request on error until timeout. + SimpleGeolocationRequest(net::URLRequestContextGetter* url_context_getter, + const GURL& service_url, + base::TimeDelta timeout); + + virtual ~SimpleGeolocationRequest(); + + // Initiates request. + // Note: if request object is destroyed before callback is called, + // request will be silently cancelled. + void MakeRequest(const ResponseCallback& callback); + + private: + // net::URLFetcherDelegate + virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; + + // Start new request. + void StartRequest(); + + // Schedules retry. + void Retry(bool server_error); + + // Run callback and destroy "this". + void ReplyAndDestroySelf(const base::TimeDelta elapsed, bool server_error); + + // Called by timeout_timer_ . + void OnTimeout(); + + scoped_refptr<net::URLRequestContextGetter> url_context_getter_; + + // Service URL from constructor arguments. + const GURL service_url_; + + ResponseCallback callback_; + + // Actual URL with parameters. + GURL request_url_; + + scoped_ptr<net::URLFetcher> url_fetcher_; + + // When request was actually started. + base::Time request_started_at_; + + const base::TimeDelta timeout_; + + // Pending retry. + base::OneShotTimer<SimpleGeolocationRequest> request_scheduled_; + + // Stop request on timeout. + base::OneShotTimer<SimpleGeolocationRequest> timeout_timer_; + + // Number of retry attempts. + unsigned retries_; + + // This is updated on each retry. + Geoposition position_; + + // Creation and destruction should happen on the same thread. + base::ThreadChecker thread_checker_; + + DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationRequest); +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_GEOLOCATION_SIMPLE_GEOLOCATION_REQUEST_H_ |