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