OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/permissions_updater.h" | 5 #include "chrome/browser/extensions/permissions_updater.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 10 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
11 #include "chrome/browser/extensions/extension_event_router.h" | 11 #include "chrome/browser/extensions/extension_event_router.h" |
12 #include "chrome/browser/extensions/extension_prefs.h" | 12 #include "chrome/browser/extensions/extension_prefs.h" |
13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/api/permissions.h" | 15 #include "chrome/common/extensions/api/permissions.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_messages.h" | 18 #include "chrome/common/extensions/extension_messages.h" |
19 #include "chrome/common/extensions/extension_permission_set.h" | |
20 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
21 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
22 | 21 |
23 using content::RenderProcessHost; | 22 using content::RenderProcessHost; |
24 using extensions::permissions_api_helpers::PackPermissionSet; | 23 using extensions::permissions_api_helpers::PackPermissionSet; |
25 | 24 |
26 namespace extensions { | 25 namespace extensions { |
27 | 26 |
28 namespace { | 27 namespace { |
29 | 28 |
30 const char kOnAdded[] = "permissions.onAdded"; | 29 const char kOnAdded[] = "permissions.onAdded"; |
31 const char kOnRemoved[] = "permissions.onRemoved"; | 30 const char kOnRemoved[] = "permissions.onRemoved"; |
32 | 31 |
33 } | 32 } |
34 | 33 |
35 PermissionsUpdater::PermissionsUpdater(Profile* profile) | 34 PermissionsUpdater::PermissionsUpdater(Profile* profile) |
36 : profile_(profile) {} | 35 : profile_(profile) {} |
37 | 36 |
38 PermissionsUpdater::~PermissionsUpdater() {} | 37 PermissionsUpdater::~PermissionsUpdater() {} |
39 | 38 |
40 void PermissionsUpdater::AddPermissions( | 39 void PermissionsUpdater::AddPermissions( |
41 const Extension* extension, const ExtensionPermissionSet* permissions) { | 40 const Extension* extension, const PermissionSet* permissions) { |
42 scoped_refptr<const ExtensionPermissionSet> existing( | 41 scoped_refptr<const PermissionSet> existing( |
43 extension->GetActivePermissions()); | 42 extension->GetActivePermissions()); |
44 scoped_refptr<ExtensionPermissionSet> total( | 43 scoped_refptr<PermissionSet> total( |
45 ExtensionPermissionSet::CreateUnion(existing, permissions)); | 44 PermissionSet::CreateUnion(existing, permissions)); |
46 scoped_refptr<ExtensionPermissionSet> added( | 45 scoped_refptr<PermissionSet> added( |
47 ExtensionPermissionSet::CreateDifference(total.get(), existing)); | 46 PermissionSet::CreateDifference(total.get(), existing)); |
48 | 47 |
49 UpdateActivePermissions(extension, total.get()); | 48 UpdateActivePermissions(extension, total.get()); |
50 | 49 |
51 // Update the granted permissions so we don't auto-disable the extension. | 50 // Update the granted permissions so we don't auto-disable the extension. |
52 GrantActivePermissions(extension); | 51 GrantActivePermissions(extension); |
53 | 52 |
54 NotifyPermissionsUpdated(ADDED, extension, added.get()); | 53 NotifyPermissionsUpdated(ADDED, extension, added.get()); |
55 } | 54 } |
56 | 55 |
57 void PermissionsUpdater::RemovePermissions( | 56 void PermissionsUpdater::RemovePermissions( |
58 const Extension* extension, const ExtensionPermissionSet* permissions) { | 57 const Extension* extension, const PermissionSet* permissions) { |
59 scoped_refptr<const ExtensionPermissionSet> existing( | 58 scoped_refptr<const PermissionSet> existing( |
60 extension->GetActivePermissions()); | 59 extension->GetActivePermissions()); |
61 scoped_refptr<ExtensionPermissionSet> total( | 60 scoped_refptr<PermissionSet> total( |
62 ExtensionPermissionSet::CreateDifference(existing, permissions)); | 61 PermissionSet::CreateDifference(existing, permissions)); |
63 scoped_refptr<ExtensionPermissionSet> removed( | 62 scoped_refptr<PermissionSet> removed( |
64 ExtensionPermissionSet::CreateDifference(existing, total.get())); | 63 PermissionSet::CreateDifference(existing, total.get())); |
65 | 64 |
66 // We update the active permissions, and not the granted permissions, because | 65 // We update the active permissions, and not the granted permissions, because |
67 // the extension, not the user, removed the permissions. This allows the | 66 // the extension, not the user, removed the permissions. This allows the |
68 // extension to add them again without prompting the user. | 67 // extension to add them again without prompting the user. |
69 UpdateActivePermissions(extension, total.get()); | 68 UpdateActivePermissions(extension, total.get()); |
70 | 69 |
71 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); | 70 NotifyPermissionsUpdated(REMOVED, extension, removed.get()); |
72 } | 71 } |
73 | 72 |
74 void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { | 73 void PermissionsUpdater::GrantActivePermissions(const Extension* extension) { |
75 CHECK(extension); | 74 CHECK(extension); |
76 | 75 |
77 // We only maintain the granted permissions prefs for INTERNAL and LOAD | 76 // We only maintain the granted permissions prefs for INTERNAL and LOAD |
78 // extensions. | 77 // extensions. |
79 if (extension->location() != Extension::LOAD && | 78 if (extension->location() != Extension::LOAD && |
80 extension->location() != Extension::INTERNAL) | 79 extension->location() != Extension::INTERNAL) |
81 return; | 80 return; |
82 | 81 |
83 GetExtensionPrefs()->AddGrantedPermissions( | 82 GetExtensionPrefs()->AddGrantedPermissions( |
84 extension->id(), extension->GetActivePermissions()); | 83 extension->id(), extension->GetActivePermissions()); |
85 } | 84 } |
86 | 85 |
87 void PermissionsUpdater::UpdateActivePermissions( | 86 void PermissionsUpdater::UpdateActivePermissions( |
88 const Extension* extension, const ExtensionPermissionSet* permissions) { | 87 const Extension* extension, const PermissionSet* permissions) { |
89 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); | 88 GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions); |
90 extension->SetActivePermissions(permissions); | 89 extension->SetActivePermissions(permissions); |
91 } | 90 } |
92 | 91 |
93 void PermissionsUpdater::DispatchEvent( | 92 void PermissionsUpdater::DispatchEvent( |
94 const std::string& extension_id, | 93 const std::string& extension_id, |
95 const char* event_name, | 94 const char* event_name, |
96 const ExtensionPermissionSet* changed_permissions) { | 95 const PermissionSet* changed_permissions) { |
97 if (!profile_ || !profile_->GetExtensionEventRouter()) | 96 if (!profile_ || !profile_->GetExtensionEventRouter()) |
98 return; | 97 return; |
99 | 98 |
100 ListValue value; | 99 ListValue value; |
101 scoped_ptr<api::permissions::Permissions> permissions = | 100 scoped_ptr<api::permissions::Permissions> permissions = |
102 PackPermissionSet(changed_permissions); | 101 PackPermissionSet(changed_permissions); |
103 value.Append(permissions->ToValue().release()); | 102 value.Append(permissions->ToValue().release()); |
104 std::string json_value; | 103 std::string json_value; |
105 base::JSONWriter::Write(&value, &json_value); | 104 base::JSONWriter::Write(&value, &json_value); |
106 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 105 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
107 extension_id, event_name, json_value, profile_, GURL()); | 106 extension_id, event_name, json_value, profile_, GURL()); |
108 } | 107 } |
109 | 108 |
110 void PermissionsUpdater::NotifyPermissionsUpdated( | 109 void PermissionsUpdater::NotifyPermissionsUpdated( |
111 EventType event_type, | 110 EventType event_type, |
112 const Extension* extension, | 111 const Extension* extension, |
113 const ExtensionPermissionSet* changed) { | 112 const PermissionSet* changed) { |
114 if (!changed || changed->IsEmpty()) | 113 if (!changed || changed->IsEmpty()) |
115 return; | 114 return; |
116 | 115 |
117 UpdatedExtensionPermissionsInfo::Reason reason; | 116 UpdatedExtensionPermissionsInfo::Reason reason; |
118 const char* event_name = NULL; | 117 const char* event_name = NULL; |
119 | 118 |
120 if (event_type == REMOVED) { | 119 if (event_type == REMOVED) { |
121 reason = UpdatedExtensionPermissionsInfo::REMOVED; | 120 reason = UpdatedExtensionPermissionsInfo::REMOVED; |
122 event_name = kOnRemoved; | 121 event_name = kOnRemoved; |
123 } else { | 122 } else { |
(...skipping 26 matching lines...) Expand all Loading... |
150 | 149 |
151 // Trigger the onAdded and onRemoved events in the extension. | 150 // Trigger the onAdded and onRemoved events in the extension. |
152 DispatchEvent(extension->id(), event_name, changed); | 151 DispatchEvent(extension->id(), event_name, changed); |
153 } | 152 } |
154 | 153 |
155 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { | 154 ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() { |
156 return profile_->GetExtensionService()->extension_prefs(); | 155 return profile_->GetExtensionService()->extension_prefs(); |
157 } | 156 } |
158 | 157 |
159 } // namespace extensions | 158 } // namespace extensions |
OLD | NEW |