summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_permissions_api.h
blob: 583302e11e003631ce45043bd69acb84dee554aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PERMISSIONS_API_H__
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PERMISSIONS_API_H__
#pragma once

#include <string>

#include "base/compiler_specific.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/browser/extensions/extension_install_ui.h"
#include "chrome/common/extensions/extension_permission_set.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/render_process_host.h"

class Extension;
class ExtensionPermissionSet;
class ExtensionService;

class ExtensionPermissionsManager {
 public:
  explicit ExtensionPermissionsManager(ExtensionService* extension_service);
  ~ExtensionPermissionsManager();

  // Adds the set of |permissions| to the |extension|'s active permission set
  // and sends the relevant messages and notifications. This method assumes the
  // user has already been prompted, if necessary, for the extra permissions.
  void AddPermissions(const Extension* extension,
                      const ExtensionPermissionSet* permissions);

  // Removes the set of |permissions| from the |extension|'s active permission
  // set and sends the relevant messages and notifications.
  void RemovePermissions(const Extension* extension,
                         const ExtensionPermissionSet* permissions);

 private:
  enum EventType {
    ADDED,
    REMOVED,
  };

  // Dispatches specified event to the extension.
  void DispatchEvent(const std::string& extension_id,
                     const char* event_name,
                     const ExtensionPermissionSet* changed_permissions);

  // Issues the relevant events, messages and notifications when the
  // |extension|'s permissions have |changed| (|changed| is the delta).
  // Specifically, this sends the EXTENSION_PERMISSIONS_UPDATED notification,
  // the ExtensionMsg_UpdatePermissions IPC message, and fires the
  // onAdded/onRemoved events in the extension.
  void NotifyPermissionsUpdated(EventType event_type,
                                const Extension* extension,
                                const ExtensionPermissionSet* changed);

  ExtensionService* extension_service_;
};


// chrome.permissions.contains
class ContainsPermissionsFunction : public SyncExtensionFunction {
  virtual ~ContainsPermissionsFunction() {}
  virtual bool RunImpl() OVERRIDE;
  DECLARE_EXTENSION_FUNCTION_NAME("permissions.contains")
};

// chrome.permissions.getAll
class GetAllPermissionsFunction : public SyncExtensionFunction {
  virtual ~GetAllPermissionsFunction() {}
  virtual bool RunImpl() OVERRIDE;
  DECLARE_EXTENSION_FUNCTION_NAME("permissions.getAll")
};

// chrome.permissions.remove
class RemovePermissionsFunction : public SyncExtensionFunction {
  virtual ~RemovePermissionsFunction() {}
  virtual bool RunImpl() OVERRIDE;
  DECLARE_EXTENSION_FUNCTION_NAME("permissions.remove")
};

// chrome.permissions.request
class RequestPermissionsFunction : public AsyncExtensionFunction,
                                   public ExtensionInstallUI::Delegate {
 public:
  // FOR TESTS ONLY to bypass the confirmation UI.
  static void SetAutoConfirmForTests(bool should_proceed);
  static void SetIgnoreUserGestureForTests(bool ignore);

  RequestPermissionsFunction();

  // Implementing ExtensionInstallUI::Delegate interface.
  virtual void InstallUIProceed() OVERRIDE;
  virtual void InstallUIAbort(bool user_initiated) OVERRIDE;

 protected:
  virtual ~RequestPermissionsFunction();
  virtual bool RunImpl() OVERRIDE;

 private:
  scoped_ptr<ExtensionInstallUI> install_ui_;
  scoped_refptr<ExtensionPermissionSet> requested_permissions_;
  const Extension* extension_;
  DECLARE_EXTENSION_FUNCTION_NAME("permissions.request")
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_PERMISSIONS_API_H__