| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_idle_api.h" | 5 #include "chrome/browser/extensions/extension_idle_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 StringValue* CreateIdleValue(IdleState idle_state) { | 126 StringValue* CreateIdleValue(IdleState idle_state) { |
| 127 StringValue* result = new StringValue(IdleStateToDescription(idle_state)); | 127 StringValue* result = new StringValue(IdleStateToDescription(idle_state)); |
| 128 return result; | 128 return result; |
| 129 } | 129 } |
| 130 | 130 |
| 131 int CheckThresholdBounds(int timeout) { | 131 int CheckThresholdBounds(int timeout) { |
| 132 if (timeout < kMinThreshold) return kMinThreshold; | 132 if (timeout < kMinThreshold) return kMinThreshold; |
| 133 if (timeout > kMaxThreshold) return kMaxThreshold; | 133 if (timeout > kMaxThreshold) return kMaxThreshold; |
| 134 return timeout; | 134 return timeout; |
| 135 } | 135 } |
| 136 |
| 136 }; // namespace | 137 }; // namespace |
| 137 | 138 |
| 138 void ExtensionIdleQueryStateFunction::IdleStateCallback(int threshold, | 139 void ExtensionIdleEventRouter::OnIdleStateChange(Profile* profile, |
| 139 IdleState state) { | 140 IdleState state) { |
| 140 // If our state is not active, make sure we're running a polling task to check | 141 // Prepare the single argument of the current state. |
| 141 // for active state and report it when it changes to active. | 142 ListValue args; |
| 142 if (state != IDLE_STATE_ACTIVE) { | 143 args.Append(CreateIdleValue(state)); |
| 143 ExtensionIdlePollingTask::CreateNewPollTask(threshold, state, profile_); | 144 std::string json_args; |
| 144 } | 145 base::JSONWriter::Write(&args, &json_args); |
| 145 | 146 |
| 146 result_.reset(CreateIdleValue(state)); | 147 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 147 | 148 keys::kOnStateChanged, json_args, profile, GURL()); |
| 148 ExtensionIdleCache::UpdateCache(threshold, state); | |
| 149 | |
| 150 SendResponse(true); | |
| 151 } | 149 } |
| 152 | 150 |
| 153 bool ExtensionIdleQueryStateFunction::RunImpl() { | 151 bool ExtensionIdleQueryStateFunction::RunImpl() { |
| 154 int threshold; | 152 int threshold; |
| 155 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold)); | 153 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &threshold)); |
| 156 threshold = CheckThresholdBounds(threshold); | 154 threshold = CheckThresholdBounds(threshold); |
| 157 | 155 |
| 158 IdleState state = ExtensionIdleCache::CalculateIdleState(threshold); | 156 IdleState state = ExtensionIdleCache::CalculateIdleState(threshold); |
| 159 if (state != IDLE_STATE_UNKNOWN) { | 157 if (state != IDLE_STATE_UNKNOWN) { |
| 160 result_.reset(CreateIdleValue(state)); | 158 result_.reset(CreateIdleValue(state)); |
| 161 SendResponse(true); | 159 SendResponse(true); |
| 162 return true; | 160 return true; |
| 163 } | 161 } |
| 164 | 162 |
| 165 CalculateIdleState(threshold, | 163 CalculateIdleState(threshold, |
| 166 base::Bind(&ExtensionIdleQueryStateFunction::IdleStateCallback, | 164 base::Bind(&ExtensionIdleQueryStateFunction::IdleStateCallback, |
| 167 this, threshold)); | 165 this, threshold)); |
| 168 // Don't send the response, it'll be sent by our callback | 166 // Don't send the response, it'll be sent by our callback |
| 169 return true; | 167 return true; |
| 170 } | 168 } |
| 171 | 169 |
| 172 void ExtensionIdleEventRouter::OnIdleStateChange(Profile* profile, | 170 void ExtensionIdleQueryStateFunction::IdleStateCallback(int threshold, |
| 173 IdleState state) { | 171 IdleState state) { |
| 174 // Prepare the single argument of the current state. | 172 // If our state is not active, make sure we're running a polling task to check |
| 175 ListValue args; | 173 // for active state and report it when it changes to active. |
| 176 args.Append(CreateIdleValue(state)); | 174 if (state != IDLE_STATE_ACTIVE) { |
| 177 std::string json_args; | 175 ExtensionIdlePollingTask::CreateNewPollTask(threshold, state, profile_); |
| 178 base::JSONWriter::Write(&args, &json_args); | 176 } |
| 179 | 177 |
| 180 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 178 result_.reset(CreateIdleValue(state)); |
| 181 keys::kOnStateChanged, json_args, profile, GURL()); | 179 |
| 180 ExtensionIdleCache::UpdateCache(threshold, state); |
| 181 |
| 182 SendResponse(true); |
| 182 } | 183 } |
| 183 | 184 |
| 184 ExtensionIdleCache::CacheData ExtensionIdleCache::cached_data = | 185 ExtensionIdleCache::CacheData ExtensionIdleCache::cached_data = |
| 185 {-1, -1, -1, -1}; | 186 {-1, -1, -1, -1}; |
| 186 | 187 |
| 187 IdleState ExtensionIdleCache::CalculateIdleState(int threshold) { | 188 IdleState ExtensionIdleCache::CalculateIdleState(int threshold) { |
| 188 return CalculateState(threshold, base::Time::Now().ToDoubleT()); | 189 return CalculateState(threshold, base::Time::Now().ToDoubleT()); |
| 189 } | 190 } |
| 190 | 191 |
| 192 void ExtensionIdleCache::UpdateCache(int threshold, IdleState state) { |
| 193 Update(threshold, state, base::Time::Now().ToDoubleT()); |
| 194 } |
| 195 |
| 191 IdleState ExtensionIdleCache::CalculateState(int threshold, double now) { | 196 IdleState ExtensionIdleCache::CalculateState(int threshold, double now) { |
| 192 if (threshold < kMinThreshold) | 197 if (threshold < kMinThreshold) |
| 193 return IDLE_STATE_UNKNOWN; | 198 return IDLE_STATE_UNKNOWN; |
| 194 double threshold_moment = now - static_cast<double>(threshold); | 199 double threshold_moment = now - static_cast<double>(threshold); |
| 195 double throttle_interval = static_cast<double>(kThrottleInterval); | 200 double throttle_interval = static_cast<double>(kThrottleInterval); |
| 196 | 201 |
| 197 // We test for IDEL_STATE_LOCKED first, because the result should be | 202 // We test for IDEL_STATE_LOCKED first, because the result should be |
| 198 // independent of the data for idle and active state. If last state was | 203 // independent of the data for idle and active state. If last state was |
| 199 // LOCKED and test for LOCKED is satisfied we should always return LOCKED. | 204 // LOCKED and test for LOCKED is satisfied we should always return LOCKED. |
| 200 if (cached_data.latest_locked > 0 && | 205 if (cached_data.latest_locked > 0 && |
| (...skipping 13 matching lines...) Expand all Loading... |
| 214 double error_from_idle = | 219 double error_from_idle = |
| 215 QueryErrorFromIdle(cached_data.idle_interval_start, | 220 QueryErrorFromIdle(cached_data.idle_interval_start, |
| 216 cached_data.idle_interval_end, threshold_moment, now); | 221 cached_data.idle_interval_end, threshold_moment, now); |
| 217 if (cached_data.idle_interval_end > 0 && | 222 if (cached_data.idle_interval_end > 0 && |
| 218 error_from_idle < throttle_interval) | 223 error_from_idle < throttle_interval) |
| 219 return IDLE_STATE_IDLE; | 224 return IDLE_STATE_IDLE; |
| 220 | 225 |
| 221 return IDLE_STATE_UNKNOWN; | 226 return IDLE_STATE_UNKNOWN; |
| 222 } | 227 } |
| 223 | 228 |
| 224 void ExtensionIdleCache::UpdateCache(int threshold, IdleState state) { | |
| 225 Update(threshold, state, base::Time::Now().ToDoubleT()); | |
| 226 } | |
| 227 | |
| 228 void ExtensionIdleCache::Update(int threshold, IdleState state, double now) { | 229 void ExtensionIdleCache::Update(int threshold, IdleState state, double now) { |
| 229 if (threshold < kMinThreshold) | 230 if (threshold < kMinThreshold) |
| 230 return; | 231 return; |
| 231 double threshold_moment = now - static_cast<double>(threshold); | 232 double threshold_moment = now - static_cast<double>(threshold); |
| 232 switch (state) { | 233 switch (state) { |
| 233 case IDLE_STATE_IDLE: | 234 case IDLE_STATE_IDLE: |
| 234 if (threshold_moment > cached_data.idle_interval_end) { | 235 if (threshold_moment > cached_data.idle_interval_end) { |
| 235 // Cached and new interval don't overlap. We disregard the cached one. | 236 // Cached and new interval don't overlap. We disregard the cached one. |
| 236 cached_data.idle_interval_start = threshold_moment; | 237 cached_data.idle_interval_start = threshold_moment; |
| 237 } else { | 238 } else { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 261 } | 262 } |
| 262 } | 263 } |
| 263 | 264 |
| 264 int ExtensionIdleCache::get_min_threshold() { | 265 int ExtensionIdleCache::get_min_threshold() { |
| 265 return kMinThreshold; | 266 return kMinThreshold; |
| 266 } | 267 } |
| 267 | 268 |
| 268 double ExtensionIdleCache::get_throttle_interval() { | 269 double ExtensionIdleCache::get_throttle_interval() { |
| 269 return static_cast<double>(kThrottleInterval); | 270 return static_cast<double>(kThrottleInterval); |
| 270 } | 271 } |
| OLD | NEW |