oauth2_access_token_fetcher_immediate_error.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "google_apis/gaia/oauth2_access_token_fetcher_immediate_error.h"
  5. #include "base/bind.h"
  6. #include "base/threading/thread_task_runner_handle.h"
  7. #include "google_apis/gaia/google_service_auth_error.h"
  8. OAuth2AccessTokenFetcherImmediateError::FailCaller::FailCaller(
  9. OAuth2AccessTokenFetcherImmediateError* fetcher)
  10. : fetcher_(fetcher) {
  11. }
  12. OAuth2AccessTokenFetcherImmediateError::FailCaller::~FailCaller() {
  13. }
  14. void OAuth2AccessTokenFetcherImmediateError::FailCaller::run() {
  15. if (fetcher_) {
  16. fetcher_->Fail();
  17. fetcher_ = nullptr;
  18. }
  19. }
  20. void OAuth2AccessTokenFetcherImmediateError::FailCaller::detach() {
  21. fetcher_ = nullptr;
  22. }
  23. OAuth2AccessTokenFetcherImmediateError::OAuth2AccessTokenFetcherImmediateError(
  24. OAuth2AccessTokenConsumer* consumer,
  25. const GoogleServiceAuthError& error)
  26. : OAuth2AccessTokenFetcher(consumer),
  27. immediate_error_(error) {
  28. DCHECK(immediate_error_ != GoogleServiceAuthError::AuthErrorNone());
  29. }
  30. OAuth2AccessTokenFetcherImmediateError::
  31. ~OAuth2AccessTokenFetcherImmediateError() {
  32. CancelRequest();
  33. }
  34. void OAuth2AccessTokenFetcherImmediateError::CancelRequest() {
  35. if (failer_) {
  36. failer_->detach();
  37. failer_ = nullptr;
  38. }
  39. }
  40. void OAuth2AccessTokenFetcherImmediateError::Start(
  41. const std::string& client_id,
  42. const std::string& client_secret,
  43. const std::vector<std::string>& scopes) {
  44. failer_ = new FailCaller(this);
  45. base::ThreadTaskRunnerHandle::Get()->PostTask(
  46. FROM_HERE,
  47. base::BindOnce(&OAuth2AccessTokenFetcherImmediateError::FailCaller::run,
  48. failer_));
  49. }
  50. void OAuth2AccessTokenFetcherImmediateError::Fail() {
  51. // The call below will likely destruct this object. We have to make a copy
  52. // of the error into a local variable because the class member thus will
  53. // be destroyed after which the copy-passed-by-reference will cause a
  54. // memory violation when accessed.
  55. GoogleServiceAuthError error_copy = immediate_error_;
  56. FireOnGetTokenFailure(error_copy);
  57. }