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.
5 #include "chrome/browser/supervised_user/permission_request_creator_sync.h"
7 #include "base/callback.h"
8 #include "base/values.h"
9 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
10 #include "chrome/browser/supervised_user/supervised_user_shared_settings_service.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "net/base/escape.h"
18 const char kSupervisedUserAccessRequestKeyPrefix
[] =
19 "X-ManagedUser-AccessRequests";
20 const char kSupervisedUserAccessRequestTime
[] = "timestamp";
21 const char kSupervisedUserName
[] = "name";
23 // Key for the notification setting of the custodian. This is a shared setting
24 // so we can include the setting in the access request data that is used to
25 // trigger notifications.
26 const char kNotificationSetting
[] = "custodian-notification-setting";
28 PermissionRequestCreatorSync::PermissionRequestCreatorSync(
29 SupervisedUserSettingsService
* settings_service
,
30 SupervisedUserSharedSettingsService
* shared_settings_service
,
31 ProfileSyncService
* sync_service
,
32 const std::string
& name
,
33 const std::string
& supervised_user_id
)
34 : settings_service_(settings_service
),
35 shared_settings_service_(shared_settings_service
),
36 sync_service_(sync_service
),
38 supervised_user_id_(supervised_user_id
) {
41 PermissionRequestCreatorSync::~PermissionRequestCreatorSync() {}
43 bool PermissionRequestCreatorSync::IsEnabled() const {
44 GoogleServiceAuthError::State state
= sync_service_
->GetAuthError().state();
45 // We allow requesting access if Sync is working or has a transient error.
46 return (state
== GoogleServiceAuthError::NONE
||
47 state
== GoogleServiceAuthError::CONNECTION_FAILED
||
48 state
== GoogleServiceAuthError::SERVICE_UNAVAILABLE
);
51 void PermissionRequestCreatorSync::CreatePermissionRequest(
52 const GURL
& url_requested
,
53 const SuccessCallback
& callback
) {
54 // Escape the URL and add the prefix.
55 std::string key
= SupervisedUserSettingsService::MakeSplitSettingKey(
56 kSupervisedUserAccessRequestKeyPrefix
,
57 net::EscapeQueryParamValue(url_requested
.spec(), true));
59 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
61 // TODO(sergiu): Use sane time here when it's ready.
62 dict
->SetDouble(kSupervisedUserAccessRequestTime
,
63 base::Time::Now().ToJsTime());
65 dict
->SetString(kSupervisedUserName
, name_
);
67 // Copy the notification setting of the custodian.
68 const base::Value
* value
= shared_settings_service_
->GetValue(
69 supervised_user_id_
, kNotificationSetting
);
70 bool notifications_enabled
= false;
72 bool success
= value
->GetAsBoolean(¬ifications_enabled
);
75 dict
->SetBoolean(kNotificationSetting
, notifications_enabled
);
77 settings_service_
->UploadItem(key
, dict
.Pass());