login_handler.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_LOGIN_HANDLER_H_
  5. #define ATOM_BROWSER_LOGIN_HANDLER_H_
  6. #include "base/strings/string16.h"
  7. #include "base/synchronization/lock.h"
  8. #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
  9. namespace content {
  10. class WebContents;
  11. }
  12. namespace net {
  13. class AuthChallengeInfo;
  14. class URLRequest;
  15. } // namespace net
  16. namespace atom {
  17. // Handles the HTTP basic auth, must be created on IO thread.
  18. class LoginHandler : public content::ResourceDispatcherHostLoginDelegate {
  19. public:
  20. LoginHandler(net::AuthChallengeInfo* auth_info, net::URLRequest* request);
  21. // Returns the WebContents associated with the request, must be called on UI
  22. // thread.
  23. content::WebContents* GetWebContents() const;
  24. // The auth is cancelled, must be called on UI thread.
  25. void CancelAuth();
  26. // Login with |username| and |password|, must be called on UI thread.
  27. void Login(const base::string16& username, const base::string16& password);
  28. const net::AuthChallengeInfo* auth_info() const { return auth_info_.get(); }
  29. protected:
  30. ~LoginHandler() override;
  31. // content::ResourceDispatcherHostLoginDelegate:
  32. void OnRequestCancelled() override;
  33. private:
  34. // Must be called on IO thread.
  35. void DoCancelAuth();
  36. void DoLogin(const base::string16& username, const base::string16& password);
  37. // Marks authentication as handled and returns the previous handled
  38. // state.
  39. bool TestAndSetAuthHandled();
  40. // True if we've handled auth (Login or CancelAuth has been called).
  41. bool handled_auth_ = false;
  42. mutable base::Lock handled_auth_lock_;
  43. // Who/where/what asked for the authentication.
  44. scoped_refptr<net::AuthChallengeInfo> auth_info_;
  45. // The request that wants login data.
  46. // This should only be accessed on the IO loop.
  47. net::URLRequest* request_ = nullptr;
  48. // Cached from the net::URLRequest, in case it goes NULL on us.
  49. int render_process_host_id_ = 0;
  50. int render_frame_id_ = 0;
  51. DISALLOW_COPY_AND_ASSIGN(LoginHandler);
  52. };
  53. } // namespace atom
  54. #endif // ATOM_BROWSER_LOGIN_HANDLER_H_