permission_request_handler.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef ANDROID_WEBVIEW_BROWSER_PERMISSION_PERMISSION_REQUEST_HANDLER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_PERMISSION_PERMISSION_REQUEST_HANDLER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "content/public/browser/web_contents_observer.h"
  13. #include "url/gurl.h"
  14. namespace android_webview {
  15. class AwPermissionRequest;
  16. class AwPermissionRequestDelegate;
  17. class PermissionRequestHandlerClient;
  18. // This class is used to send the permission requests, or cancel ongoing
  19. // requests.
  20. // It is owned by AwContents and has 1x1 mapping to AwContents. All methods
  21. // are running on UI thread.
  22. class PermissionRequestHandler : public content::WebContentsObserver {
  23. public:
  24. PermissionRequestHandler(PermissionRequestHandlerClient* client,
  25. content::WebContents* aw_contents);
  26. PermissionRequestHandler(const PermissionRequestHandler&) = delete;
  27. PermissionRequestHandler& operator=(const PermissionRequestHandler&) = delete;
  28. ~PermissionRequestHandler() override;
  29. // Send the given |request| to PermissionRequestHandlerClient.
  30. void SendRequest(std::unique_ptr<AwPermissionRequestDelegate> request);
  31. // Cancel the ongoing request initiated by |origin| for accessing |resources|.
  32. void CancelRequest(const GURL& origin, int64_t resources);
  33. // Allow |origin| to access the |resources|.
  34. void PreauthorizePermission(const GURL& origin, int64_t resources);
  35. // WebContentsObserver
  36. void NavigationEntryCommitted(
  37. const content::LoadCommittedDetails& load_details) override;
  38. private:
  39. friend class TestPermissionRequestHandler;
  40. typedef std::vector<base::WeakPtr<AwPermissionRequest>>::iterator
  41. RequestIterator;
  42. // Return the request initiated by |origin| for accessing |resources|.
  43. RequestIterator FindRequest(const GURL& origin, int64_t resources);
  44. // Cancel the given request.
  45. void CancelRequestInternal(RequestIterator i);
  46. void CancelAllRequests();
  47. // Remove the invalid requests from requests_.
  48. void PruneRequests();
  49. // Return true if |origin| were preauthorized to access |resources|.
  50. bool Preauthorized(const GURL& origin, int64_t resources);
  51. raw_ptr<PermissionRequestHandlerClient> client_;
  52. // A list of ongoing requests.
  53. std::vector<base::WeakPtr<AwPermissionRequest>> requests_;
  54. std::map<std::string, int64_t> preauthorized_permission_;
  55. // The unique id of the active NavigationEntry of the WebContents that we were
  56. // opened for. Used to help expire on requests.
  57. int contents_unique_id_;
  58. };
  59. } // namespace android_webview
  60. #endif // ANDROID_WEBVIEW_BROWSER_PERMISSION_PERMISSION_REQUEST_HANDLER_H_