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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// Copyright (c) 2012 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.
#include "chrome/browser/extensions/permissions_updater.h"
#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/token_service.h"
#include "chrome/browser/signin/token_service_factory.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/api/permissions.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_messages.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_process_host.h"
#include "google_apis/gaia/oauth2_mint_token_flow.h"
using content::RenderProcessHost;
using extensions::permissions_api_helpers::PackPermissionSet;
using extensions::PermissionSet;
namespace extensions {
namespace {
const char kOnAdded[] = "permissions.onAdded";
const char kOnRemoved[] = "permissions.onRemoved";
// An object to link the lifetime of an OAuth2MintTokenFlow to a Profile.
// The flow should not outlive the profile because the request context will
// become invalid.
class OAuth2GrantRecorder : public OAuth2MintTokenFlow::Delegate,
public content::NotificationObserver {
public:
OAuth2GrantRecorder(Profile* profile, const Extension* extension)
: ALLOW_THIS_IN_INITIALIZER_LIST(flow_(
profile->GetRequestContext(),
this,
OAuth2MintTokenFlow::Parameters(
TokenServiceFactory::GetForProfile(profile)->
GetOAuth2LoginRefreshToken(),
extension->id(),
extension->oauth2_info().client_id,
extension->oauth2_info().scopes,
OAuth2MintTokenFlow::MODE_RECORD_GRANT))) {
notification_registrar_.Add(this,
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::Source<Profile>(profile));
flow_.Start();
}
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE {
DCHECK_EQ(type, chrome::NOTIFICATION_PROFILE_DESTROYED);
delete this;
}
// OAuth2MintTokenFlow::Delegate:
virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE {
delete this;
}
virtual void OnIssueAdviceSuccess(
const IssueAdviceInfo& issue_advice) OVERRIDE {
delete this;
}
virtual void OnMintTokenFailure(
const GoogleServiceAuthError& error) OVERRIDE {
delete this;
}
private:
virtual ~OAuth2GrantRecorder() {}
OAuth2MintTokenFlow flow_;
content::NotificationRegistrar notification_registrar_;
};
} // namespace
PermissionsUpdater::PermissionsUpdater(Profile* profile)
: profile_(profile) {}
PermissionsUpdater::~PermissionsUpdater() {}
void PermissionsUpdater::AddPermissions(
const Extension* extension, const PermissionSet* permissions) {
scoped_refptr<const PermissionSet> existing(
extension->GetActivePermissions());
scoped_refptr<PermissionSet> total(
PermissionSet::CreateUnion(existing, permissions));
scoped_refptr<PermissionSet> added(
PermissionSet::CreateDifference(total.get(), existing));
UpdateActivePermissions(extension, total.get());
// Update the granted permissions so we don't auto-disable the extension.
GrantActivePermissions(extension, false);
NotifyPermissionsUpdated(ADDED, extension, added.get());
}
void PermissionsUpdater::RemovePermissions(
const Extension* extension, const PermissionSet* permissions) {
scoped_refptr<const PermissionSet> existing(
extension->GetActivePermissions());
scoped_refptr<PermissionSet> total(
PermissionSet::CreateDifference(existing, permissions));
scoped_refptr<PermissionSet> removed(
PermissionSet::CreateDifference(existing, total.get()));
// We update the active permissions, and not the granted permissions, because
// the extension, not the user, removed the permissions. This allows the
// extension to add them again without prompting the user.
UpdateActivePermissions(extension, total.get());
NotifyPermissionsUpdated(REMOVED, extension, removed.get());
}
void PermissionsUpdater::GrantActivePermissions(const Extension* extension,
bool record_oauth2_grant) {
CHECK(extension);
// We only maintain the granted permissions prefs for INTERNAL and LOAD
// extensions.
if (extension->location() != Extension::LOAD &&
extension->location() != Extension::INTERNAL)
return;
if (record_oauth2_grant) {
// Only record OAuth grant if:
// 1. The extension has client id and scopes.
// 2. The user is signed in to Chrome.
const Extension::OAuth2Info& oauth2_info = extension->oauth2_info();
if (!oauth2_info.client_id.empty() && !oauth2_info.scopes.empty()) {
TokenService* token_service = TokenServiceFactory::GetForProfile(
profile_);
if (token_service && token_service->HasOAuthLoginToken()) {
new OAuth2GrantRecorder(profile_, extension);
}
}
}
GetExtensionPrefs()->AddGrantedPermissions(extension->id(),
extension->GetActivePermissions());
}
void PermissionsUpdater::UpdateActivePermissions(
const Extension* extension, const PermissionSet* permissions) {
GetExtensionPrefs()->SetActivePermissions(extension->id(), permissions);
extension->SetActivePermissions(permissions);
}
void PermissionsUpdater::DispatchEvent(
const std::string& extension_id,
const char* event_name,
const PermissionSet* changed_permissions) {
if (!profile_ ||
!extensions::ExtensionSystem::Get(profile_)->event_router())
return;
scoped_ptr<ListValue> value(new ListValue());
scoped_ptr<api::permissions::Permissions> permissions =
PackPermissionSet(changed_permissions);
value->Append(permissions->ToValue().release());
extensions::ExtensionSystem::Get(profile_)->event_router()->
DispatchEventToExtension(extension_id, event_name, value.Pass(),
profile_, GURL());
}
void PermissionsUpdater::NotifyPermissionsUpdated(
EventType event_type,
const Extension* extension,
const PermissionSet* changed) {
if (!changed || changed->IsEmpty())
return;
UpdatedExtensionPermissionsInfo::Reason reason;
const char* event_name = NULL;
if (event_type == REMOVED) {
reason = UpdatedExtensionPermissionsInfo::REMOVED;
event_name = kOnRemoved;
} else {
CHECK_EQ(ADDED, event_type);
reason = UpdatedExtensionPermissionsInfo::ADDED;
event_name = kOnAdded;
}
// Notify other APIs or interested parties.
UpdatedExtensionPermissionsInfo info = UpdatedExtensionPermissionsInfo(
extension, changed, reason);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
content::Source<Profile>(profile_),
content::Details<UpdatedExtensionPermissionsInfo>(&info));
// Send the new permissions to the renderers.
for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
RenderProcessHost* host = i.GetCurrentValue();
Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
if (profile_->IsSameProfile(profile))
host->Send(new ExtensionMsg_UpdatePermissions(
static_cast<int>(reason),
extension->id(),
changed->apis(),
changed->explicit_hosts(),
changed->scriptable_hosts()));
}
// Trigger the onAdded and onRemoved events in the extension.
DispatchEvent(extension->id(), event_name, changed);
}
ExtensionPrefs* PermissionsUpdater::GetExtensionPrefs() {
return extensions::ExtensionSystem::Get(profile_)->extension_service()->
extension_prefs();
}
} // namespace extensions
|