1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_indexeddb_permissionrequestbase_h__
8 #define mozilla_dom_indexeddb_permissionrequestbase_h__
9 
10 #include "mozilla/Attributes.h"
11 #include "nsCOMPtr.h"
12 #include "nsIInterfaceRequestor.h"
13 #include "nsIObserver.h"
14 #include "nsIPermissionManager.h"
15 #include "nsISupportsImpl.h"
16 #include "nsString.h"
17 
18 class nsIPrincipal;
19 
20 namespace mozilla {
21 namespace dom {
22 
23 class Element;
24 
25 namespace indexedDB {
26 
27 class PermissionRequestBase
28   : public nsIObserver
29   , public nsIInterfaceRequestor
30 {
31   nsCOMPtr<Element> mOwnerElement;
32   nsCOMPtr<nsIPrincipal> mPrincipal;
33 
34 public:
35   enum PermissionValue {
36     kPermissionAllowed = nsIPermissionManager::ALLOW_ACTION,
37     kPermissionDenied = nsIPermissionManager::DENY_ACTION,
38     kPermissionPrompt = nsIPermissionManager::PROMPT_ACTION
39   };
40 
41   NS_DECL_ISUPPORTS
42 
43   // This function will not actually prompt. It will never return
44   // kPermissionDefault but will instead translate the permission manager value
45   // into the correct value for the given type.
46   static nsresult
47   GetCurrentPermission(nsIPrincipal* aPrincipal,
48                        PermissionValue* aCurrentValue);
49 
50   static PermissionValue
51   PermissionValueForIntPermission(uint32_t aIntPermission);
52 
53   // This function will prompt if needed. It may only be called once.
54   nsresult
55   PromptIfNeeded(PermissionValue* aCurrentValue);
56 
57 protected:
58   PermissionRequestBase(Element* aOwnerElement,
59                         nsIPrincipal* aPrincipal);
60 
61   // Reference counted.
62   virtual
63   ~PermissionRequestBase();
64 
65   virtual void
66   OnPromptComplete(PermissionValue aPermissionValue) = 0;
67 
68 private:
69   void
70   SetExplicitPermission(nsIPrincipal* aPrincipal,
71                         uint32_t aIntPermission);
72 
73   NS_DECL_NSIOBSERVER
74   NS_DECL_NSIINTERFACEREQUESTOR
75 };
76 
77 } // namespace indexedDB
78 } // namespace dom
79 } // namespace mozilla
80 
81 #endif // mozilla_dom_indexeddb_permissionrequestbase_h__
82