summaryrefslogtreecommitdiffstats
path: root/chrome/service/gaia
diff options
context:
space:
mode:
authorsanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-12 19:25:07 +0000
committersanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-12 19:25:07 +0000
commit1d0ee423b6a0bb307c7c19fe28c95d8761691e9a (patch)
tree5766edd6b8b8a7b2eb3942e75008388a0ecfeff0 /chrome/service/gaia
parentb90b874fe7023537531fbd08df403694e3e3a520 (diff)
downloadchromium_src-1d0ee423b6a0bb307c7c19fe28c95d8761691e9a.zip
chromium_src-1d0ee423b6a0bb307c7c19fe28c95d8761691e9a.tar.gz
chromium_src-1d0ee423b6a0bb307c7c19fe28c95d8761691e9a.tar.bz2
Created a new process type called the service process to host background tasks such as the Cloud Print Proxy.
BUG=None. TEST=None. Review URL: http://codereview.chromium.org/2001009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47055 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/service/gaia')
-rw-r--r--chrome/service/gaia/service_gaia_authenticator.cc86
-rw-r--r--chrome/service/gaia/service_gaia_authenticator.h57
2 files changed, 143 insertions, 0 deletions
diff --git a/chrome/service/gaia/service_gaia_authenticator.cc b/chrome/service/gaia/service_gaia_authenticator.cc
new file mode 100644
index 0000000..55dfff2
--- /dev/null
+++ b/chrome/service/gaia/service_gaia_authenticator.cc
@@ -0,0 +1,86 @@
+// Copyright (c) 2010 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.
+
+#include "chrome/service/gaia/service_gaia_authenticator.h"
+
+#include "base/message_loop_proxy.h"
+#include "chrome/service/net/service_url_request_context.h"
+#include "googleurl/src/gurl.h"
+
+ServiceGaiaAuthenticator::ServiceGaiaAuthenticator(
+ const std::string& user_agent, const std::string& service_id,
+ const std::string& gaia_url,
+ base::MessageLoopProxy* io_message_loop_proxy)
+ : gaia::GaiaAuthenticator(user_agent, service_id, gaia_url),
+ http_post_completed_(false, false),
+ io_message_loop_proxy_(io_message_loop_proxy),
+ http_response_code_(0) {
+}
+
+ServiceGaiaAuthenticator::~ServiceGaiaAuthenticator() {
+}
+
+bool ServiceGaiaAuthenticator::Post(const GURL& url,
+ const std::string& post_body,
+ unsigned long* response_code,
+ std::string* response_body) {
+ DCHECK(url.SchemeIsSecure());
+ DCHECK(io_message_loop_proxy_);
+ io_message_loop_proxy_->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(this, &ServiceGaiaAuthenticator::DoPost, url,
+ post_body));
+ if (!http_post_completed_.Wait()) // Block until network request completes.
+ NOTREACHED(); // See OnURLFetchComplete.
+
+ *response_code = static_cast<int>(http_response_code_);
+ *response_body = response_data_;
+ return true;
+}
+
+// TODO(sanjeevr): This is a placeholder implementation. Need to move this logic
+// to a common location within the service process so that it can be resued by
+// other classes needing a backoff delay calculation.
+int ServiceGaiaAuthenticator::GetBackoffDelaySeconds(
+ int current_backoff_delay) {
+ const int kMaxBackoffDelaySeconds = 60 * 60; // (1 hour)
+ int ret = 0;
+ if (0 == current_backoff_delay) {
+ ret = 1;
+ } else {
+ ret = current_backoff_delay * 2;
+ }
+ if (ret > kMaxBackoffDelaySeconds) {
+ ret = kMaxBackoffDelaySeconds;
+ }
+ return ret;
+}
+
+void ServiceGaiaAuthenticator::DoPost(const GURL& post_url,
+ const std::string& post_body) {
+ DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
+ request_.reset(new URLFetcher(post_url, URLFetcher::POST, this));
+ ServiceURLRequestContextGetter* context_getter =
+ new ServiceURLRequestContextGetter();
+ request_->set_request_context(context_getter);
+ request_->set_upload_data("application/x-www-form-urlencoded", post_body);
+ request_->Start();
+}
+
+// URLFetcher::Delegate implementation
+void ServiceGaiaAuthenticator::OnURLFetchComplete(
+ const URLFetcher *source, const GURL &url, const URLRequestStatus &status,
+ int response_code, const ResponseCookies &cookies,
+ const std::string &data) {
+ DCHECK(io_message_loop_proxy_->BelongsToCurrentThread());
+ http_response_code_ = response_code;
+ response_data_ = data;
+ // Add an extra reference because we want http_post_completed_ to remain
+ // valid until after Signal() returns.
+ scoped_refptr<ServiceGaiaAuthenticator> keep_alive(this);
+ // Wake the blocked thread in Post.
+ http_post_completed_.Signal();
+ // WARNING: DONT DO ANYTHING AFTER THIS CALL! |this| may be deleted!
+}
+
diff --git a/chrome/service/gaia/service_gaia_authenticator.h b/chrome/service/gaia/service_gaia_authenticator.h
new file mode 100644
index 0000000..aedc302
--- /dev/null
+++ b/chrome/service/gaia/service_gaia_authenticator.h
@@ -0,0 +1,57 @@
+// Copyright (c) 2010 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_SERVICE_GAIA_SERVICE_GAIA_AUTHENTICATOR_H_
+#define CHROME_SERVICE_GAIA_SERVICE_GAIA_AUTHENTICATOR_H_
+
+#include <string>
+
+#include "base/ref_counted.h"
+#include "base/waitable_event.h"
+#include "chrome/common/net/url_fetcher.h"
+#include "chrome/common/net/gaia/gaia_authenticator.h"
+
+namespace base {
+class MessageLoopProxy;
+}
+
+// A GaiaAuthenticator implementation to be used in the service process (where
+// we cannot rely on the existence of a Profile)
+class ServiceGaiaAuthenticator
+ : public base::RefCountedThreadSafe<ServiceGaiaAuthenticator>,
+ public URLFetcher::Delegate,
+ public gaia::GaiaAuthenticator {
+ public:
+ ServiceGaiaAuthenticator(const std::string& user_agent,
+ const std::string& service_id,
+ const std::string& gaia_url,
+ base::MessageLoopProxy* io_message_loop_proxy);
+ ~ServiceGaiaAuthenticator();
+
+ // URLFetcher::Delegate implementation.
+ void OnURLFetchComplete(const URLFetcher *source, const GURL &url,
+ const URLRequestStatus &status, int response_code,
+ const ResponseCookies &cookies,
+ const std::string &data);
+
+ protected:
+ // GaiaAuthenticator overrides.
+ virtual bool Post(const GURL& url, const std::string& post_body,
+ unsigned long* response_code, std::string* response_body);
+ virtual int GetBackoffDelaySeconds(int current_backoff_delay);
+
+ private:
+ void DoPost(const GURL& post_url, const std::string& post_body);
+
+ base::WaitableEvent http_post_completed_;
+ scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
+ int http_response_code_;
+ std::string response_data_;
+ scoped_ptr<URLFetcher> request_;
+
+ DISALLOW_COPY_AND_ASSIGN(ServiceGaiaAuthenticator);
+};
+
+#endif // CHROME_SERVICE_GAIA_SERVICE_GAIA_AUTHENTICATOR_H_
+