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