permission_request_creator_sync.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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. 4 5#include "chrome/browser/supervised_user/permission_request_creator_sync.h" 6 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 12using base::Time; 13 14const char kSupervisedUserAccessRequestKeyPrefix[] = 15 "X-ManagedUser-AccessRequests"; 16const char kSupervisedUserAccessRequestTime[] = "timestamp"; 17const char kSupervisedUserName[] = "name"; 18 19// Key for the notification setting of the custodian. This is a shared setting 20// so we can include the setting in the access request data that is used to 21// trigger notifications. 22const char kNotificationSetting[] = "custodian-notification-setting"; 23 24PermissionRequestCreatorSync::PermissionRequestCreatorSync( 25 SupervisedUserSettingsService* settings_service, 26 SupervisedUserSharedSettingsService* shared_settings_service, 27 const std::string& name, 28 const std::string& supervised_user_id) 29 : settings_service_(settings_service), 30 shared_settings_service_(shared_settings_service), 31 name_(name), 32 supervised_user_id_(supervised_user_id) { 33} 34 35PermissionRequestCreatorSync::~PermissionRequestCreatorSync() {} 36 37void PermissionRequestCreatorSync::CreatePermissionRequest( 38 const std::string& url_requested, 39 const base::Closure& callback) { 40 // Add the prefix. 41 std::string key = SupervisedUserSettingsService::MakeSplitSettingKey( 42 kSupervisedUserAccessRequestKeyPrefix, url_requested); 43 44 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 45 46 // TODO(sergiu): Use sane time here when it's ready. 47 dict->SetDouble(kSupervisedUserAccessRequestTime, 48 base::Time::Now().ToJsTime()); 49 50 dict->SetString(kSupervisedUserName, name_); 51 52 // Copy the notification setting of the custodian. 53 const base::Value* value = shared_settings_service_->GetValue( 54 supervised_user_id_, kNotificationSetting); 55 bool notifications_enabled = false; 56 if (value) { 57 bool success = value->GetAsBoolean(¬ifications_enabled); 58 DCHECK(success); 59 } 60 dict->SetBoolean(kNotificationSetting, notifications_enabled); 61 62 settings_service_->UploadItem(key, dict.PassAs<base::Value>()); 63 64 callback.Run(); 65} 66