1 // Copyright 2017 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/extensions/api/settings_private/chromeos_resolve_time_zone_by_geolocation_on_off.h" 6 7 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process_platform_part.h" 9 #include "chrome/browser/chromeos/system/timezone_resolver_manager.h" 10 #include "chrome/browser/extensions/api/settings_private/generated_pref.h" 11 #include "chrome/browser/extensions/api/settings_private/generated_time_zone_pref_base.h" 12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/common/extensions/api/settings_private.h" 15 #include "chrome/common/pref_names.h" 16 #include "components/prefs/pref_service.h" 17 #include "components/user_manager/user_manager.h" 18 19 namespace extensions { 20 21 namespace settings_api = api::settings_private; 22 23 namespace settings_private { 24 namespace { 25 26 // Implements kResolveTimezoneByGeolocationOnOff generated preference. 27 class GeneratedResolveTimezoneByGeolocationOnOff 28 : public GeneratedTimeZonePrefBase { 29 public: 30 explicit GeneratedResolveTimezoneByGeolocationOnOff(Profile* profile); 31 ~GeneratedResolveTimezoneByGeolocationOnOff() override; 32 33 // GeneratedPref implementation: 34 std::unique_ptr<settings_api::PrefObject> GetPrefObject() const override; 35 SetPrefResult SetPref(const base::Value* value) override; 36 37 private: 38 DISALLOW_COPY_AND_ASSIGN(GeneratedResolveTimezoneByGeolocationOnOff); 39 }; 40 41 GeneratedResolveTimezoneByGeolocationOnOff:: GeneratedResolveTimezoneByGeolocationOnOff(Profile * profile)42 GeneratedResolveTimezoneByGeolocationOnOff(Profile* profile) 43 : GeneratedTimeZonePrefBase(kResolveTimezoneByGeolocationOnOff, profile) {} 44 45 GeneratedResolveTimezoneByGeolocationOnOff:: 46 ~GeneratedResolveTimezoneByGeolocationOnOff() = default; 47 48 std::unique_ptr<settings_api::PrefObject> GetPrefObject() const49GeneratedResolveTimezoneByGeolocationOnOff::GetPrefObject() const { 50 std::unique_ptr<settings_api::PrefObject> pref_object = 51 std::make_unique<settings_api::PrefObject>(); 52 53 pref_object->key = pref_name_; 54 pref_object->type = settings_api::PREF_TYPE_BOOLEAN; 55 pref_object->value = 56 std::make_unique<base::Value>(g_browser_process->platform_part() 57 ->GetTimezoneResolverManager() 58 ->TimeZoneResolverShouldBeRunning()); 59 60 UpdateTimeZonePrefControlledBy(pref_object.get()); 61 62 return pref_object; 63 } 64 SetPref(const base::Value * value)65SetPrefResult GeneratedResolveTimezoneByGeolocationOnOff::SetPref( 66 const base::Value* value) { 67 if (!value->is_bool()) 68 return SetPrefResult::PREF_TYPE_MISMATCH; 69 70 // Check if preference is policy or primary-user controlled, and therefore 71 // cannot deactivate automatic timezone. 72 if (chromeos::system::TimeZoneResolverManager:: 73 IsTimeZoneResolutionPolicyControlled() || 74 !profile_->IsSameOrParent(ProfileManager::GetPrimaryUserProfile())) { 75 return SetPrefResult::PREF_NOT_MODIFIABLE; 76 } 77 78 const bool new_value = value->GetBool(); 79 const bool current_value = g_browser_process->platform_part() 80 ->GetTimezoneResolverManager() 81 ->TimeZoneResolverShouldBeRunning(); 82 if (new_value == current_value) 83 return SetPrefResult::SUCCESS; 84 85 profile_->GetPrefs()->SetInteger( 86 ::prefs::kResolveTimezoneByGeolocationMethod, 87 static_cast<int>(new_value ? chromeos::system::TimeZoneResolverManager:: 88 TimeZoneResolveMethod::IP_ONLY 89 : chromeos::system::TimeZoneResolverManager:: 90 TimeZoneResolveMethod::DISABLED)); 91 92 return SetPrefResult::SUCCESS; 93 } 94 95 } // anonymous namespace 96 97 const char kResolveTimezoneByGeolocationOnOff[] = 98 "generated.resolve_timezone_by_geolocation_on_off"; 99 CreateGeneratedResolveTimezoneByGeolocationOnOff(Profile * profile)100std::unique_ptr<GeneratedPref> CreateGeneratedResolveTimezoneByGeolocationOnOff( 101 Profile* profile) { 102 return std::make_unique<GeneratedResolveTimezoneByGeolocationOnOff>(profile); 103 } 104 105 } // namespace settings_private 106 } // namespace extensions 107