Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / supervised_user / legacy / permission_request_creator_sync.cc
blob25c4ab613b829903492e0b3c09d3fdc74cf2f147
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/legacy/permission_request_creator_sync.h"
7 #include "base/callback.h"
8 #include "base/values.h"
9 #include "chrome/browser/supervised_user/legacy/supervised_user_shared_settings_service.h"
10 #include "chrome/browser/supervised_user/supervised_user_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"
14 #include "url/gurl.h"
16 const char kSupervisedUserAccessRequestKeyPrefix[] =
17 "X-ManagedUser-AccessRequests";
18 const char kSupervisedUserUpdateRequestKeyPrefix[] =
19 "X-ManagedUser-UpdateRequests";
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),
37 name_(name),
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::CreateURLAccessRequest(
52 const GURL& url_requested,
53 const SuccessCallback& callback) {
54 CreateRequest(kSupervisedUserAccessRequestKeyPrefix,
55 net::EscapeQueryParamValue(url_requested.spec(), true),
56 callback);
59 void PermissionRequestCreatorSync::CreateExtensionUpdateRequest(
60 const std::string& id,
61 const SuccessCallback& callback) {
62 CreateRequest(kSupervisedUserUpdateRequestKeyPrefix, id, callback);
65 void PermissionRequestCreatorSync::CreateRequest(
66 const std::string& prefix,
67 const std::string& data,
68 const SuccessCallback& callback) {
69 // Add the prefix.
70 std::string key =
71 SupervisedUserSettingsService::MakeSplitSettingKey(prefix, data);
73 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
74 // TODO(sergiu): Use sane time here when it's ready.
75 dict->SetDouble(kSupervisedUserAccessRequestTime,
76 base::Time::Now().ToJsTime());
77 dict->SetString(kSupervisedUserName, name_);
79 // Copy the notification setting of the custodian.
80 const base::Value* value = shared_settings_service_->GetValue(
81 supervised_user_id_, kNotificationSetting);
82 bool notifications_enabled = false;
83 if (value) {
84 bool success = value->GetAsBoolean(&notifications_enabled);
85 DCHECK(success);
87 dict->SetBoolean(kNotificationSetting, notifications_enabled);
89 settings_service_->UploadItem(key, dict.Pass());
91 callback.Run(true);