Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/managed_mode/permission_request_creator.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/managed_mode/managed_user_settings_service.h" | |
| 11 #include "chrome/browser/managed_mode/managed_user_shared_settings_service.h" | |
| 12 #include "chrome/common/chrome_switches.h" | |
| 13 #include "google_apis/gaia/google_service_auth_error.h" | |
| 14 | |
| 15 using base::Time; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kManagedUserAccessRequestKeyPrefix[] = | |
| 20 "X-ManagedUser-AccessRequests"; | |
| 21 const char kManagedUserAccessRequestTime[] = "timestamp"; | |
| 22 const char kManagedUserName[] = "name"; | |
| 23 | |
| 24 // Key for the notification setting of the custodian. This is a shared setting | |
| 25 // so we can include the setting in the access request data that is used to | |
| 26 // trigger notifications. | |
| 27 const char kNotificationSetting[] = "custodian-notification-setting"; | |
| 28 | |
| 29 class PermissionRequestCreatorImpl : public PermissionRequestCreator { | |
|
Bernhard Bauer
2014/05/16 15:22:50
Wait, do you have two classes with the same name?
Adrian Kuegel
2014/05/19 11:00:53
Acknowledged.
| |
| 30 public: | |
| 31 PermissionRequestCreatorImpl( | |
| 32 ManagedUserSettingsService* settings_service, | |
| 33 ManagedUserSharedSettingsService* shared_settings_service, | |
| 34 const std::string& name, | |
| 35 const std::string& managed_user_id); | |
| 36 virtual ~PermissionRequestCreatorImpl(); | |
| 37 | |
| 38 // PermissionRequestCreator implementation: | |
| 39 virtual void CreatePermissionRequest( | |
| 40 const std::string& url_requested, | |
| 41 const PermissionRequestCallback& callback) OVERRIDE; | |
| 42 | |
| 43 private: | |
| 44 ManagedUserSettingsService* settings_service_; | |
| 45 ManagedUserSharedSettingsService* shared_settings_service_; | |
| 46 std::string name_; | |
| 47 std::string managed_user_id_; | |
| 48 }; | |
| 49 | |
| 50 PermissionRequestCreatorImpl::PermissionRequestCreatorImpl( | |
| 51 ManagedUserSettingsService* settings_service, | |
| 52 ManagedUserSharedSettingsService* shared_settings_service, | |
| 53 const std::string& name, | |
| 54 const std::string& managed_user_id) | |
| 55 : settings_service_(settings_service), | |
| 56 shared_settings_service_(shared_settings_service), | |
| 57 name_(name), | |
| 58 managed_user_id_(managed_user_id) { | |
| 59 } | |
| 60 | |
| 61 PermissionRequestCreatorImpl::~PermissionRequestCreatorImpl() {} | |
| 62 | |
| 63 void PermissionRequestCreatorImpl::CreatePermissionRequest( | |
| 64 const std::string& url_requested, | |
| 65 const PermissionRequestCallback& callback) { | |
| 66 // Add the prefix. | |
| 67 std::string key = ManagedUserSettingsService::MakeSplitSettingKey( | |
| 68 kManagedUserAccessRequestKeyPrefix, url_requested); | |
| 69 | |
| 70 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | |
| 71 | |
| 72 // TODO(sergiu): Use sane time here when it's ready. | |
| 73 dict->SetDouble(kManagedUserAccessRequestTime, base::Time::Now().ToJsTime()); | |
| 74 | |
| 75 dict->SetString(kManagedUserName, name_); | |
| 76 | |
| 77 // Copy the notification setting of the custodian. | |
| 78 const base::Value* value = shared_settings_service_->GetValue( | |
| 79 managed_user_id_, kNotificationSetting); | |
| 80 bool notifications_enabled = false; | |
| 81 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 82 switches::kEnableAccessRequestNotifications)) { | |
| 83 notifications_enabled = true; | |
| 84 } else if (value) { | |
| 85 bool success = value->GetAsBoolean(¬ifications_enabled); | |
| 86 DCHECK(success); | |
| 87 } | |
| 88 dict->SetBoolean(kNotificationSetting, notifications_enabled); | |
| 89 | |
| 90 settings_service_->UploadItem(key, dict.PassAs<base::Value>()); | |
| 91 | |
| 92 callback.Run(GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | |
| 93 } | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 // static | |
| 98 scoped_ptr<PermissionRequestCreator> | |
| 99 PermissionRequestCreator::CreateWithSettingsService( | |
| 100 ManagedUserSettingsService* settings_service, | |
| 101 ManagedUserSharedSettingsService* shared_settings_service, | |
| 102 const std::string& name, | |
| 103 const std::string& managed_user_id) { | |
| 104 scoped_ptr<PermissionRequestCreator> creator(new PermissionRequestCreatorImpl( | |
| 105 settings_service, shared_settings_service, name, managed_user_id)); | |
| 106 return creator.Pass(); | |
| 107 } | |
| OLD | NEW |