1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_ERROR_CONTROLLER_H_
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_ERROR_CONTROLLER_H_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <set>
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/basictypes.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/compiler_specific.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/observer_list.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "google_apis/gaia/google_service_auth_error.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Keep track of auth errors and expose them to observers in the UI. Services
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// that wish to expose auth errors to the user should register an
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// AuthStatusProvider to report their current authentication state, and should
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// invoke AuthStatusChanged() when their authentication state may have changed.
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class SigninErrorController {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class AuthStatusProvider {
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   public:
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    AuthStatusProvider();
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~AuthStatusProvider();
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Returns the account id with the status specified by GetAuthStatus().
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual std::string GetAccountId() const = 0;
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Returns the username with the status specified by GetAuthStatus().
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    virtual std::string GetUsername() const = 0;
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // API invoked by SigninErrorController to get the current auth status of
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // the various signed in services.
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual GoogleServiceAuthError GetAuthStatus() const = 0;
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The observer class for SigninErrorController lets the controller notify
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // observers when an error arises or changes.
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class Observer {
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   public:
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ~Observer() {}
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual void OnErrorChanged() = 0;
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SigninErrorController();
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ~SigninErrorController();
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Adds a provider which the SigninErrorController object will start querying
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // for auth status.
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AddProvider(const AuthStatusProvider* provider);
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Removes a provider previously added by SigninErrorController (generally
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // only called in preparation for shutdown).
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void RemoveProvider(const AuthStatusProvider* provider);
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Invoked when the auth status of an AuthStatusProvider has changed.
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AuthStatusChanged();
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // True if there exists an error worth elevating to the user.
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool HasError() const;
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AddObserver(Observer* observer);
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void RemoveObserver(Observer* observer);
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::string& error_account_id() const { return error_account_id_; }
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const std::string& error_username() const { return error_username_; }
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const GoogleServiceAuthError& auth_error() const { return auth_error_; }
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::set<const AuthStatusProvider*> provider_set_;
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The account that generated the last auth error.
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string error_account_id_;
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string error_username_;
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The auth error detected the last time AuthStatusChanged() was invoked (or
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // NONE if AuthStatusChanged() has never been invoked).
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GoogleServiceAuthError auth_error_;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ObserverList<Observer, false> observer_list_;
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SigninErrorController);
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif  // COMPONENTS_SIGNIN_CORE_BROWSER_SIGNIN_ERROR_CONTROLLER_H_
85