OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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_frame/ready_mode/internal/registry_ready_mode_state.h" |
| 6 |
| 7 #include "base/time.h" |
| 8 #include "base/task.h" |
| 9 #include "base/win/registry.h" |
| 10 #include "chrome_frame/ready_mode/internal/installation_state.h" |
| 11 #include "chrome_frame/ready_mode/ready_mode_manager.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const wchar_t kReadyModeStateValue[] = L"ReadyModeState"; |
| 16 |
| 17 }; // namespace |
| 18 |
| 19 RegistryReadyModeState::RegistryReadyModeState( |
| 20 const std::wstring& key_name, base::TimeDelta temporary_decline_duration, |
| 21 InstallationState* installation_state, Observer* observer) |
| 22 : key_name_(key_name), |
| 23 temporary_decline_duration_(temporary_decline_duration), |
| 24 installation_state_(installation_state), |
| 25 observer_(observer) { |
| 26 } |
| 27 |
| 28 RegistryReadyModeState::~RegistryReadyModeState() { |
| 29 } |
| 30 |
| 31 base::Time RegistryReadyModeState::GetNow() { |
| 32 return base::Time::Now(); |
| 33 } |
| 34 |
| 35 ReadyModeStatus RegistryReadyModeState::GetStatus() { |
| 36 if (installation_state_->IsProductInstalled()) |
| 37 return READY_MODE_ACCEPTED; |
| 38 |
| 39 if (!installation_state_->IsProductRegistered()) |
| 40 return READY_MODE_PERMANENTLY_DECLINED; |
| 41 |
| 42 bool exists = false; |
| 43 int64 value = 0; |
| 44 |
| 45 if (!GetValue(&value, &exists)) |
| 46 return READY_MODE_TEMPORARILY_DECLINED; |
| 47 |
| 48 if (!exists) |
| 49 return READY_MODE_ACTIVE; |
| 50 |
| 51 if (value == 0) |
| 52 return READY_MODE_PERMANENTLY_DECLINED; |
| 53 |
| 54 base::Time when_declined(base::Time::FromInternalValue(value)); |
| 55 base::Time now(GetNow()); |
| 56 |
| 57 // If the decline duration has passed, or is further in the future than |
| 58 // the total timeout, consider it expired. |
| 59 bool expired = (now - when_declined) > temporary_decline_duration_ || |
| 60 (when_declined - now) > temporary_decline_duration_; |
| 61 |
| 62 // To avoid a race-condition in GetValue (between ValueExists and ReadValue) |
| 63 // we never delete the temporary decline flag. |
| 64 if (expired) |
| 65 return READY_MODE_ACTIVE; |
| 66 |
| 67 return READY_MODE_TEMPORARILY_DECLINED; |
| 68 } |
| 69 |
| 70 bool RegistryReadyModeState::GetValue(int64* value, bool* exists) { |
| 71 *exists = false; |
| 72 *value = 0; |
| 73 |
| 74 base::win::RegKey config_key; |
| 75 if (!config_key.Open(HKEY_CURRENT_USER, key_name_.c_str(), KEY_QUERY_VALUE)) { |
| 76 DLOG(ERROR) << "Failed to open registry key " << key_name_; |
| 77 return false; |
| 78 } |
| 79 |
| 80 if (!config_key.ValueExists(kReadyModeStateValue)) |
| 81 return true; |
| 82 |
| 83 int64 temp; |
| 84 DWORD value_size = sizeof(temp); |
| 85 DWORD type = 0; |
| 86 if (!config_key.ReadValue(kReadyModeStateValue, &temp, &value_size, &type)) { |
| 87 DLOG(ERROR) << "Failed to open registry key " << key_name_; |
| 88 return false; |
| 89 } |
| 90 |
| 91 if (value_size != sizeof(temp) || type != REG_QWORD) { |
| 92 DLOG(ERROR) << "Unexpected state found under registry key " << key_name_ |
| 93 << " and value " << kReadyModeStateValue; |
| 94 config_key.DeleteValue(kReadyModeStateValue); |
| 95 return true; |
| 96 } |
| 97 |
| 98 *value = temp; |
| 99 *exists = true; |
| 100 return true; |
| 101 } |
| 102 |
| 103 bool RegistryReadyModeState::StoreValue(int64 value) { |
| 104 base::win::RegKey config_key; |
| 105 if (config_key.Open(HKEY_CURRENT_USER, key_name_.c_str(), KEY_SET_VALUE) && |
| 106 config_key.WriteValue(kReadyModeStateValue, &value, sizeof(value), |
| 107 REG_QWORD)) { |
| 108 return true; |
| 109 } |
| 110 |
| 111 DLOG(ERROR) << "Failed to open or write to registry key " << key_name_ |
| 112 << " and value " << kReadyModeStateValue; |
| 113 |
| 114 return false; |
| 115 } |
| 116 |
| 117 void RegistryReadyModeState::TemporarilyDeclineChromeFrame() { |
| 118 int64 value = GetNow().ToInternalValue(); |
| 119 |
| 120 if (StoreValue(value)) |
| 121 observer_->OnStateChange(); |
| 122 } |
| 123 |
| 124 void RegistryReadyModeState::PermanentlyDeclineChromeFrame() { |
| 125 bool success = false; |
| 126 |
| 127 // Either change, by itself, will deactivate Ready Mode, though we prefer to |
| 128 // also unregister, in order to free up resources. |
| 129 |
| 130 if (StoreValue(0)) |
| 131 success = true; |
| 132 |
| 133 if (installation_state_->UnregisterProduct()) |
| 134 success = true; |
| 135 |
| 136 if (success) |
| 137 observer_->OnStateChange(); |
| 138 } |
| 139 |
| 140 void RegistryReadyModeState::AcceptChromeFrame() { |
| 141 if (installation_state_->InstallProduct()) |
| 142 observer_->OnStateChange(); |
| 143 } |
OLD | NEW |