OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/browser/gpu/gpu_control_list.h" |
| 6 |
| 7 #include "base/cpu.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/string_number_conversions.h" |
| 11 #include "base/string_util.h" |
| 12 #include "base/strings/string_split.h" |
| 13 #include "base/sys_info.h" |
| 14 #include "content/browser/gpu/gpu_util.h" |
| 15 #include "content/public/common/gpu_info.h" |
| 16 |
| 17 namespace content { |
| 18 namespace { |
| 19 |
| 20 // Break a version string into segments. Return true if each segment is |
| 21 // a valid number. |
| 22 bool ProcessVersionString(const std::string& version_string, |
| 23 char splitter, |
| 24 std::vector<std::string>* version) { |
| 25 DCHECK(version); |
| 26 base::SplitString(version_string, splitter, version); |
| 27 if (version->size() == 0) |
| 28 return false; |
| 29 // If the splitter is '-', we assume it's a date with format "mm-dd-yyyy"; |
| 30 // we split it into the order of "yyyy", "mm", "dd". |
| 31 if (splitter == '-') { |
| 32 std::string year = (*version)[version->size() - 1]; |
| 33 for (int i = version->size() - 1; i > 0; --i) { |
| 34 (*version)[i] = (*version)[i - 1]; |
| 35 } |
| 36 (*version)[0] = year; |
| 37 } |
| 38 for (size_t i = 0; i < version->size(); ++i) { |
| 39 unsigned num = 0; |
| 40 if (!base::StringToUint((*version)[i], &num)) |
| 41 return false; |
| 42 } |
| 43 return true; |
| 44 } |
| 45 |
| 46 // Compare two number strings using numerical ordering. |
| 47 // Return 0 if number = number_ref, |
| 48 // 1 if number > number_ref, |
| 49 // -1 if number < number_ref. |
| 50 int CompareNumericalNumberStrings( |
| 51 const std::string& number, const std::string& number_ref) { |
| 52 unsigned value1 = 0; |
| 53 unsigned value2 = 0; |
| 54 bool valid = base::StringToUint(number, &value1); |
| 55 DCHECK(valid); |
| 56 valid = base::StringToUint(number_ref, &value2); |
| 57 DCHECK(valid); |
| 58 if (value1 == value2) |
| 59 return 0; |
| 60 if (value1 > value2) |
| 61 return 1; |
| 62 return -1; |
| 63 } |
| 64 |
| 65 // Compare two number strings using lexical ordering. |
| 66 // Return 0 if number = number_ref, |
| 67 // 1 if number > number_ref, |
| 68 // -1 if number < number_ref. |
| 69 // We only compare as many digits as number_ref contains. |
| 70 // If number_ref is xxx, it's considered as xxx* |
| 71 // For example: CompareLexicalNumberStrings("121", "12") returns 0, |
| 72 // CompareLexicalNumberStrings("12", "121") returns -1. |
| 73 int CompareLexicalNumberStrings( |
| 74 const std::string& number, const std::string& number_ref) { |
| 75 for (size_t i = 0; i < number_ref.length(); ++i) { |
| 76 unsigned value1 = 0; |
| 77 if (i < number.length()) |
| 78 value1 = number[i] - '0'; |
| 79 unsigned value2 = number_ref[i] - '0'; |
| 80 if (value1 > value2) |
| 81 return 1; |
| 82 if (value1 < value2) |
| 83 return -1; |
| 84 } |
| 85 return 0; |
| 86 } |
| 87 |
| 88 bool GpuUnmatched(uint32 vendor_id, const std::vector<uint32>& device_id_list, |
| 89 const GPUInfo::GPUDevice& gpu) { |
| 90 if (vendor_id == 0) |
| 91 return false; |
| 92 if (vendor_id != gpu.vendor_id) |
| 93 return true; |
| 94 bool device_specified = false; |
| 95 for (size_t i = 0; i < device_id_list.size(); ++i) { |
| 96 if (device_id_list[i] == 0) |
| 97 continue; |
| 98 if (device_id_list[i] == gpu.device_id) |
| 99 return false; |
| 100 device_specified = true; |
| 101 } |
| 102 return device_specified; |
| 103 } |
| 104 |
| 105 const char kMultiGpuStyleStringAMDSwitchable[] = "amd_switchable"; |
| 106 const char kMultiGpuStyleStringOptimus[] = "optimus"; |
| 107 |
| 108 const char kMultiGpuCategoryStringPrimary[] = "primary"; |
| 109 const char kMultiGpuCategoryStringSecondary[] = "secondary"; |
| 110 const char kMultiGpuCategoryStringAny[] = "any"; |
| 111 |
| 112 const char kVersionStyleStringNumerical[] = "numerical"; |
| 113 const char kVersionStyleStringLexical[] = "lexical"; |
| 114 |
| 115 const char kOp[] = "op"; |
| 116 |
| 117 } // namespace anonymous |
| 118 |
| 119 GpuControlList::VersionInfo::VersionInfo( |
| 120 const std::string& version_op, |
| 121 const std::string& version_style, |
| 122 const std::string& version_string, |
| 123 const std::string& version_string2) |
| 124 : version_style_(kVersionStyleNumerical) { |
| 125 op_ = StringToNumericOp(version_op); |
| 126 if (op_ == kUnknown || op_ == kAny) |
| 127 return; |
| 128 version_style_ = StringToVersionStyle(version_style); |
| 129 if (!ProcessVersionString(version_string, '.', &version_)) { |
| 130 op_ = kUnknown; |
| 131 return; |
| 132 } |
| 133 if (op_ == kBetween) { |
| 134 if (!ProcessVersionString(version_string2, '.', &version2_)) |
| 135 op_ = kUnknown; |
| 136 } |
| 137 } |
| 138 |
| 139 GpuControlList::VersionInfo::~VersionInfo() { |
| 140 } |
| 141 |
| 142 bool GpuControlList::VersionInfo::Contains( |
| 143 const std::string& version_string) const { |
| 144 return Contains(version_string, '.'); |
| 145 } |
| 146 |
| 147 bool GpuControlList::VersionInfo::Contains( |
| 148 const std::string& version_string, char splitter) const { |
| 149 if (op_ == kUnknown) |
| 150 return false; |
| 151 if (op_ == kAny) |
| 152 return true; |
| 153 std::vector<std::string> version; |
| 154 if (!ProcessVersionString(version_string, splitter, &version)) |
| 155 return false; |
| 156 int relation = Compare(version, version_, version_style_); |
| 157 if (op_ == kEQ) |
| 158 return (relation == 0); |
| 159 else if (op_ == kLT) |
| 160 return (relation < 0); |
| 161 else if (op_ == kLE) |
| 162 return (relation <= 0); |
| 163 else if (op_ == kGT) |
| 164 return (relation > 0); |
| 165 else if (op_ == kGE) |
| 166 return (relation >= 0); |
| 167 // op_ == kBetween |
| 168 if (relation < 0) |
| 169 return false; |
| 170 return Compare(version, version2_, version_style_) <= 0; |
| 171 } |
| 172 |
| 173 bool GpuControlList::VersionInfo::IsValid() const { |
| 174 return (op_ != kUnknown && version_style_ != kVersionStyleUnknown); |
| 175 } |
| 176 |
| 177 bool GpuControlList::VersionInfo::IsLexical() const { |
| 178 return version_style_ == kVersionStyleLexical; |
| 179 } |
| 180 |
| 181 // static |
| 182 int GpuControlList::VersionInfo::Compare( |
| 183 const std::vector<std::string>& version, |
| 184 const std::vector<std::string>& version_ref, |
| 185 VersionStyle version_style) { |
| 186 DCHECK(version.size() > 0 && version_ref.size() > 0); |
| 187 DCHECK(version_style != kVersionStyleUnknown); |
| 188 for (size_t i = 0; i < version_ref.size(); ++i) { |
| 189 if (i >= version.size()) |
| 190 return 0; |
| 191 int ret = 0; |
| 192 // We assume both versions are checked by ProcessVersionString(). |
| 193 if (i > 0 && version_style == kVersionStyleLexical) |
| 194 ret = CompareLexicalNumberStrings(version[i], version_ref[i]); |
| 195 else |
| 196 ret = CompareNumericalNumberStrings(version[i], version_ref[i]); |
| 197 if (ret != 0) |
| 198 return ret; |
| 199 } |
| 200 return 0; |
| 201 } |
| 202 |
| 203 // static |
| 204 GpuControlList::VersionInfo::VersionStyle |
| 205 GpuControlList::VersionInfo::StringToVersionStyle( |
| 206 const std::string& version_style) { |
| 207 if (version_style.empty() || version_style == kVersionStyleStringNumerical) |
| 208 return kVersionStyleNumerical; |
| 209 if (version_style == kVersionStyleStringLexical) |
| 210 return kVersionStyleLexical; |
| 211 return kVersionStyleUnknown; |
| 212 } |
| 213 |
| 214 GpuControlList::OsInfo::OsInfo(const std::string& os, |
| 215 const std::string& version_op, |
| 216 const std::string& version_string, |
| 217 const std::string& version_string2) { |
| 218 type_ = StringToOsType(os); |
| 219 if (type_ != kOsUnknown) { |
| 220 version_info_.reset( |
| 221 new VersionInfo(version_op, "", version_string, version_string2)); |
| 222 } |
| 223 } |
| 224 |
| 225 GpuControlList::OsInfo::~OsInfo() {} |
| 226 |
| 227 bool GpuControlList::OsInfo::Contains(OsType type, |
| 228 const std::string& version) const { |
| 229 if (!IsValid()) |
| 230 return false; |
| 231 if (type_ != type && type_ != kOsAny) |
| 232 return false; |
| 233 return version_info_->Contains(version); |
| 234 } |
| 235 |
| 236 bool GpuControlList::OsInfo::IsValid() const { |
| 237 return type_ != kOsUnknown && version_info_->IsValid(); |
| 238 } |
| 239 |
| 240 GpuControlList::OsType GpuControlList::OsInfo::type() const { |
| 241 return type_; |
| 242 } |
| 243 |
| 244 GpuControlList::OsType GpuControlList::OsInfo::StringToOsType( |
| 245 const std::string& os) { |
| 246 if (os == "win") |
| 247 return kOsWin; |
| 248 else if (os == "macosx") |
| 249 return kOsMacosx; |
| 250 else if (os == "android") |
| 251 return kOsAndroid; |
| 252 else if (os == "linux") |
| 253 return kOsLinux; |
| 254 else if (os == "chromeos") |
| 255 return kOsChromeOS; |
| 256 else if (os == "any") |
| 257 return kOsAny; |
| 258 return kOsUnknown; |
| 259 } |
| 260 |
| 261 GpuControlList::MachineModelInfo::MachineModelInfo( |
| 262 const std::string& name_op, |
| 263 const std::string& name_value, |
| 264 const std::string& version_op, |
| 265 const std::string& version_string, |
| 266 const std::string& version_string2) { |
| 267 name_info_.reset(new StringInfo(name_op, name_value)); |
| 268 version_info_.reset( |
| 269 new VersionInfo(version_op, "", version_string, version_string2)); |
| 270 } |
| 271 |
| 272 GpuControlList::MachineModelInfo::~MachineModelInfo() {} |
| 273 |
| 274 bool GpuControlList::MachineModelInfo::Contains( |
| 275 const std::string& name, const std::string& version) const { |
| 276 if (!IsValid()) |
| 277 return false; |
| 278 if (!name_info_->Contains(name)) |
| 279 return false; |
| 280 return version_info_->Contains(version); |
| 281 } |
| 282 |
| 283 bool GpuControlList::MachineModelInfo::IsValid() const { |
| 284 return name_info_->IsValid() && version_info_->IsValid(); |
| 285 } |
| 286 |
| 287 GpuControlList::StringInfo::StringInfo(const std::string& string_op, |
| 288 const std::string& string_value) { |
| 289 op_ = StringToOp(string_op); |
| 290 value_ = StringToLowerASCII(string_value); |
| 291 } |
| 292 |
| 293 bool GpuControlList::StringInfo::Contains(const std::string& value) const { |
| 294 std::string my_value = StringToLowerASCII(value); |
| 295 switch (op_) { |
| 296 case kContains: |
| 297 return strstr(my_value.c_str(), value_.c_str()) != NULL; |
| 298 case kBeginWith: |
| 299 return StartsWithASCII(my_value, value_, false); |
| 300 case kEndWith: |
| 301 return EndsWith(my_value, value_, false); |
| 302 case kEQ: |
| 303 return value_ == my_value; |
| 304 default: |
| 305 return false; |
| 306 } |
| 307 } |
| 308 |
| 309 bool GpuControlList::StringInfo::IsValid() const { |
| 310 return op_ != kUnknown; |
| 311 } |
| 312 |
| 313 GpuControlList::StringInfo::Op GpuControlList::StringInfo::StringToOp( |
| 314 const std::string& string_op) { |
| 315 if (string_op == "=") |
| 316 return kEQ; |
| 317 else if (string_op == "contains") |
| 318 return kContains; |
| 319 else if (string_op == "beginwith") |
| 320 return kBeginWith; |
| 321 else if (string_op == "endwith") |
| 322 return kEndWith; |
| 323 return kUnknown; |
| 324 } |
| 325 |
| 326 GpuControlList::FloatInfo::FloatInfo(const std::string& float_op, |
| 327 const std::string& float_value, |
| 328 const std::string& float_value2) |
| 329 : op_(kUnknown), |
| 330 value_(0.f), |
| 331 value2_(0.f) { |
| 332 double dvalue = 0; |
| 333 if (!base::StringToDouble(float_value, &dvalue)) { |
| 334 op_ = kUnknown; |
| 335 return; |
| 336 } |
| 337 value_ = static_cast<float>(dvalue); |
| 338 op_ = StringToNumericOp(float_op); |
| 339 if (op_ == kBetween) { |
| 340 if (!base::StringToDouble(float_value2, &dvalue)) { |
| 341 op_ = kUnknown; |
| 342 return; |
| 343 } |
| 344 value2_ = static_cast<float>(dvalue); |
| 345 } |
| 346 } |
| 347 |
| 348 bool GpuControlList::FloatInfo::Contains(float value) const { |
| 349 if (op_ == kUnknown) |
| 350 return false; |
| 351 if (op_ == kAny) |
| 352 return true; |
| 353 if (op_ == kEQ) |
| 354 return (value == value_); |
| 355 if (op_ == kLT) |
| 356 return (value < value_); |
| 357 if (op_ == kLE) |
| 358 return (value <= value_); |
| 359 if (op_ == kGT) |
| 360 return (value > value_); |
| 361 if (op_ == kGE) |
| 362 return (value >= value_); |
| 363 DCHECK(op_ == kBetween); |
| 364 return ((value_ <= value && value <= value2_) || |
| 365 (value2_ <= value && value <= value_)); |
| 366 } |
| 367 |
| 368 bool GpuControlList::FloatInfo::IsValid() const { |
| 369 return op_ != kUnknown; |
| 370 } |
| 371 |
| 372 GpuControlList::IntInfo::IntInfo(const std::string& int_op, |
| 373 const std::string& int_value, |
| 374 const std::string& int_value2) |
| 375 : op_(kUnknown), |
| 376 value_(0), |
| 377 value2_(0) { |
| 378 if (!base::StringToInt(int_value, &value_)) { |
| 379 op_ = kUnknown; |
| 380 return; |
| 381 } |
| 382 op_ = StringToNumericOp(int_op); |
| 383 if (op_ == kBetween && |
| 384 !base::StringToInt(int_value2, &value2_)) |
| 385 op_ = kUnknown; |
| 386 } |
| 387 |
| 388 bool GpuControlList::IntInfo::Contains(int value) const { |
| 389 if (op_ == kUnknown) |
| 390 return false; |
| 391 if (op_ == kAny) |
| 392 return true; |
| 393 if (op_ == kEQ) |
| 394 return (value == value_); |
| 395 if (op_ == kLT) |
| 396 return (value < value_); |
| 397 if (op_ == kLE) |
| 398 return (value <= value_); |
| 399 if (op_ == kGT) |
| 400 return (value > value_); |
| 401 if (op_ == kGE) |
| 402 return (value >= value_); |
| 403 DCHECK(op_ == kBetween); |
| 404 return ((value_ <= value && value <= value2_) || |
| 405 (value2_ <= value && value <= value_)); |
| 406 } |
| 407 |
| 408 bool GpuControlList::IntInfo::IsValid() const { |
| 409 return op_ != kUnknown; |
| 410 } |
| 411 |
| 412 // static |
| 413 GpuControlList::ScopedGpuControlListEntry |
| 414 GpuControlList::GpuControlListEntry::GetEntryFromValue( |
| 415 const base::DictionaryValue* value, bool top_level, |
| 416 const FeatureMap& feature_map) { |
| 417 DCHECK(value); |
| 418 ScopedGpuControlListEntry entry(new GpuControlListEntry()); |
| 419 |
| 420 size_t dictionary_entry_count = 0; |
| 421 |
| 422 if (top_level) { |
| 423 uint32 id; |
| 424 if (!value->GetInteger("id", reinterpret_cast<int*>(&id)) || |
| 425 !entry->SetId(id)) { |
| 426 LOG(WARNING) << "Malformed id entry " << entry->id(); |
| 427 return NULL; |
| 428 } |
| 429 dictionary_entry_count++; |
| 430 |
| 431 bool disabled; |
| 432 if (value->GetBoolean("disabled", &disabled)) { |
| 433 entry->SetDisabled(disabled); |
| 434 dictionary_entry_count++; |
| 435 } |
| 436 } |
| 437 |
| 438 std::string description; |
| 439 if (value->GetString("description", &description)) { |
| 440 entry->description_ = description; |
| 441 dictionary_entry_count++; |
| 442 } else { |
| 443 entry->description_ = "The GPU is unavailable for an unexplained reason."; |
| 444 } |
| 445 |
| 446 const base::ListValue* cr_bugs; |
| 447 if (value->GetList("cr_bugs", &cr_bugs)) { |
| 448 for (size_t i = 0; i < cr_bugs->GetSize(); ++i) { |
| 449 int bug_id; |
| 450 if (cr_bugs->GetInteger(i, &bug_id)) { |
| 451 entry->cr_bugs_.push_back(bug_id); |
| 452 } else { |
| 453 LOG(WARNING) << "Malformed cr_bugs entry " << entry->id(); |
| 454 return NULL; |
| 455 } |
| 456 } |
| 457 dictionary_entry_count++; |
| 458 } |
| 459 |
| 460 const base::ListValue* webkit_bugs; |
| 461 if (value->GetList("webkit_bugs", &webkit_bugs)) { |
| 462 for (size_t i = 0; i < webkit_bugs->GetSize(); ++i) { |
| 463 int bug_id; |
| 464 if (webkit_bugs->GetInteger(i, &bug_id)) { |
| 465 entry->webkit_bugs_.push_back(bug_id); |
| 466 } else { |
| 467 LOG(WARNING) << "Malformed webkit_bugs entry " << entry->id(); |
| 468 return NULL; |
| 469 } |
| 470 } |
| 471 dictionary_entry_count++; |
| 472 } |
| 473 |
| 474 const base::DictionaryValue* os_value = NULL; |
| 475 if (value->GetDictionary("os", &os_value)) { |
| 476 std::string os_type; |
| 477 std::string os_version_op = "any"; |
| 478 std::string os_version_string; |
| 479 std::string os_version_string2; |
| 480 os_value->GetString("type", &os_type); |
| 481 const base::DictionaryValue* os_version_value = NULL; |
| 482 if (os_value->GetDictionary("version", &os_version_value)) { |
| 483 os_version_value->GetString(kOp, &os_version_op); |
| 484 os_version_value->GetString("number", &os_version_string); |
| 485 os_version_value->GetString("number2", &os_version_string2); |
| 486 } |
| 487 if (!entry->SetOsInfo(os_type, os_version_op, os_version_string, |
| 488 os_version_string2)) { |
| 489 LOG(WARNING) << "Malformed os entry " << entry->id(); |
| 490 return NULL; |
| 491 } |
| 492 dictionary_entry_count++; |
| 493 } |
| 494 |
| 495 std::string vendor_id; |
| 496 if (value->GetString("vendor_id", &vendor_id)) { |
| 497 if (!entry->SetVendorId(vendor_id)) { |
| 498 LOG(WARNING) << "Malformed vendor_id entry " << entry->id(); |
| 499 return NULL; |
| 500 } |
| 501 dictionary_entry_count++; |
| 502 } |
| 503 |
| 504 const base::ListValue* device_id_list; |
| 505 if (value->GetList("device_id", &device_id_list)) { |
| 506 for (size_t i = 0; i < device_id_list->GetSize(); ++i) { |
| 507 std::string device_id; |
| 508 if (!device_id_list->GetString(i, &device_id) || |
| 509 !entry->AddDeviceId(device_id)) { |
| 510 LOG(WARNING) << "Malformed device_id entry " << entry->id(); |
| 511 return NULL; |
| 512 } |
| 513 } |
| 514 dictionary_entry_count++; |
| 515 } |
| 516 |
| 517 std::string multi_gpu_style; |
| 518 if (value->GetString("multi_gpu_style", &multi_gpu_style)) { |
| 519 if (!entry->SetMultiGpuStyle(multi_gpu_style)) { |
| 520 LOG(WARNING) << "Malformed multi_gpu_style entry " << entry->id(); |
| 521 return NULL; |
| 522 } |
| 523 dictionary_entry_count++; |
| 524 } |
| 525 |
| 526 std::string multi_gpu_category; |
| 527 if (value->GetString("multi_gpu_category", &multi_gpu_category)) { |
| 528 if (!entry->SetMultiGpuCategory(multi_gpu_category)) { |
| 529 LOG(WARNING) << "Malformed multi_gpu_category entry " << entry->id(); |
| 530 return NULL; |
| 531 } |
| 532 dictionary_entry_count++; |
| 533 } |
| 534 |
| 535 const base::DictionaryValue* driver_vendor_value = NULL; |
| 536 if (value->GetDictionary("driver_vendor", &driver_vendor_value)) { |
| 537 std::string vendor_op; |
| 538 std::string vendor_value; |
| 539 driver_vendor_value->GetString(kOp, &vendor_op); |
| 540 driver_vendor_value->GetString("value", &vendor_value); |
| 541 if (!entry->SetDriverVendorInfo(vendor_op, vendor_value)) { |
| 542 LOG(WARNING) << "Malformed driver_vendor entry " << entry->id(); |
| 543 return NULL; |
| 544 } |
| 545 dictionary_entry_count++; |
| 546 } |
| 547 |
| 548 const base::DictionaryValue* driver_version_value = NULL; |
| 549 if (value->GetDictionary("driver_version", &driver_version_value)) { |
| 550 std::string driver_version_op = "any"; |
| 551 std::string driver_version_style; |
| 552 std::string driver_version_string; |
| 553 std::string driver_version_string2; |
| 554 driver_version_value->GetString(kOp, &driver_version_op); |
| 555 driver_version_value->GetString("style", &driver_version_style); |
| 556 driver_version_value->GetString("number", &driver_version_string); |
| 557 driver_version_value->GetString("number2", &driver_version_string2); |
| 558 if (!entry->SetDriverVersionInfo(driver_version_op, |
| 559 driver_version_style, |
| 560 driver_version_string, |
| 561 driver_version_string2)) { |
| 562 LOG(WARNING) << "Malformed driver_version entry " << entry->id(); |
| 563 return NULL; |
| 564 } |
| 565 dictionary_entry_count++; |
| 566 } |
| 567 |
| 568 const base::DictionaryValue* driver_date_value = NULL; |
| 569 if (value->GetDictionary("driver_date", &driver_date_value)) { |
| 570 std::string driver_date_op = "any"; |
| 571 std::string driver_date_string; |
| 572 std::string driver_date_string2; |
| 573 driver_date_value->GetString(kOp, &driver_date_op); |
| 574 driver_date_value->GetString("number", &driver_date_string); |
| 575 driver_date_value->GetString("number2", &driver_date_string2); |
| 576 if (!entry->SetDriverDateInfo(driver_date_op, driver_date_string, |
| 577 driver_date_string2)) { |
| 578 LOG(WARNING) << "Malformed driver_date entry " << entry->id(); |
| 579 return NULL; |
| 580 } |
| 581 dictionary_entry_count++; |
| 582 } |
| 583 |
| 584 const base::DictionaryValue* gl_vendor_value = NULL; |
| 585 if (value->GetDictionary("gl_vendor", &gl_vendor_value)) { |
| 586 std::string vendor_op; |
| 587 std::string vendor_value; |
| 588 gl_vendor_value->GetString(kOp, &vendor_op); |
| 589 gl_vendor_value->GetString("value", &vendor_value); |
| 590 if (!entry->SetGLVendorInfo(vendor_op, vendor_value)) { |
| 591 LOG(WARNING) << "Malformed gl_vendor entry " << entry->id(); |
| 592 return NULL; |
| 593 } |
| 594 dictionary_entry_count++; |
| 595 } |
| 596 |
| 597 const base::DictionaryValue* gl_renderer_value = NULL; |
| 598 if (value->GetDictionary("gl_renderer", &gl_renderer_value)) { |
| 599 std::string renderer_op; |
| 600 std::string renderer_value; |
| 601 gl_renderer_value->GetString(kOp, &renderer_op); |
| 602 gl_renderer_value->GetString("value", &renderer_value); |
| 603 if (!entry->SetGLRendererInfo(renderer_op, renderer_value)) { |
| 604 LOG(WARNING) << "Malformed gl_renderer entry " << entry->id(); |
| 605 return NULL; |
| 606 } |
| 607 dictionary_entry_count++; |
| 608 } |
| 609 |
| 610 const base::DictionaryValue* cpu_brand_value = NULL; |
| 611 if (value->GetDictionary("cpu_info", &cpu_brand_value)) { |
| 612 std::string cpu_op; |
| 613 std::string cpu_value; |
| 614 cpu_brand_value->GetString(kOp, &cpu_op); |
| 615 cpu_brand_value->GetString("value", &cpu_value); |
| 616 if (!entry->SetCpuBrand(cpu_op, cpu_value)) { |
| 617 LOG(WARNING) << "Malformed cpu_brand entry " << entry->id(); |
| 618 return NULL; |
| 619 } |
| 620 dictionary_entry_count++; |
| 621 } |
| 622 |
| 623 const base::DictionaryValue* perf_graphics_value = NULL; |
| 624 if (value->GetDictionary("perf_graphics", &perf_graphics_value)) { |
| 625 std::string op; |
| 626 std::string float_value; |
| 627 std::string float_value2; |
| 628 perf_graphics_value->GetString(kOp, &op); |
| 629 perf_graphics_value->GetString("value", &float_value); |
| 630 perf_graphics_value->GetString("value2", &float_value2); |
| 631 if (!entry->SetPerfGraphicsInfo(op, float_value, float_value2)) { |
| 632 LOG(WARNING) << "Malformed perf_graphics entry " << entry->id(); |
| 633 return NULL; |
| 634 } |
| 635 dictionary_entry_count++; |
| 636 } |
| 637 |
| 638 const base::DictionaryValue* perf_gaming_value = NULL; |
| 639 if (value->GetDictionary("perf_gaming", &perf_gaming_value)) { |
| 640 std::string op; |
| 641 std::string float_value; |
| 642 std::string float_value2; |
| 643 perf_gaming_value->GetString(kOp, &op); |
| 644 perf_gaming_value->GetString("value", &float_value); |
| 645 perf_gaming_value->GetString("value2", &float_value2); |
| 646 if (!entry->SetPerfGamingInfo(op, float_value, float_value2)) { |
| 647 LOG(WARNING) << "Malformed perf_gaming entry " << entry->id(); |
| 648 return NULL; |
| 649 } |
| 650 dictionary_entry_count++; |
| 651 } |
| 652 |
| 653 const base::DictionaryValue* perf_overall_value = NULL; |
| 654 if (value->GetDictionary("perf_overall", &perf_overall_value)) { |
| 655 std::string op; |
| 656 std::string float_value; |
| 657 std::string float_value2; |
| 658 perf_overall_value->GetString(kOp, &op); |
| 659 perf_overall_value->GetString("value", &float_value); |
| 660 perf_overall_value->GetString("value2", &float_value2); |
| 661 if (!entry->SetPerfOverallInfo(op, float_value, float_value2)) { |
| 662 LOG(WARNING) << "Malformed perf_overall entry " << entry->id(); |
| 663 return NULL; |
| 664 } |
| 665 dictionary_entry_count++; |
| 666 } |
| 667 |
| 668 const base::DictionaryValue* machine_model_value = NULL; |
| 669 if (value->GetDictionary("machine_model", &machine_model_value)) { |
| 670 std::string name_op; |
| 671 std::string name_value; |
| 672 const base::DictionaryValue* name = NULL; |
| 673 if (machine_model_value->GetDictionary("name", &name)) { |
| 674 name->GetString(kOp, &name_op); |
| 675 name->GetString("value", &name_value); |
| 676 } |
| 677 |
| 678 std::string version_op = "any"; |
| 679 std::string version_string; |
| 680 std::string version_string2; |
| 681 const base::DictionaryValue* version_value = NULL; |
| 682 if (machine_model_value->GetDictionary("version", &version_value)) { |
| 683 version_value->GetString(kOp, &version_op); |
| 684 version_value->GetString("number", &version_string); |
| 685 version_value->GetString("number2", &version_string2); |
| 686 } |
| 687 if (!entry->SetMachineModelInfo( |
| 688 name_op, name_value, version_op, version_string, version_string2)) { |
| 689 LOG(WARNING) << "Malformed machine_model entry " << entry->id(); |
| 690 return NULL; |
| 691 } |
| 692 dictionary_entry_count++; |
| 693 } |
| 694 |
| 695 const base::DictionaryValue* gpu_count_value = NULL; |
| 696 if (value->GetDictionary("gpu_count", &gpu_count_value)) { |
| 697 std::string op; |
| 698 std::string int_value; |
| 699 std::string int_value2; |
| 700 gpu_count_value->GetString(kOp, &op); |
| 701 gpu_count_value->GetString("value", &int_value); |
| 702 gpu_count_value->GetString("value2", &int_value2); |
| 703 if (!entry->SetGpuCountInfo(op, int_value, int_value2)) { |
| 704 LOG(WARNING) << "Malformed gpu_count entry " << entry->id(); |
| 705 return NULL; |
| 706 } |
| 707 dictionary_entry_count++; |
| 708 } |
| 709 |
| 710 if (top_level) { |
| 711 const base::ListValue* feature_value = NULL; |
| 712 if (value->GetList("features", &feature_value)) { |
| 713 std::vector<std::string> feature_list; |
| 714 for (size_t i = 0; i < feature_value->GetSize(); ++i) { |
| 715 std::string feature; |
| 716 if (feature_value->GetString(i, &feature)) { |
| 717 feature_list.push_back(feature); |
| 718 } else { |
| 719 LOG(WARNING) << "Malformed feature entry " << entry->id(); |
| 720 return NULL; |
| 721 } |
| 722 } |
| 723 if (!entry->SetFeatures(feature_list, feature_map)) { |
| 724 LOG(WARNING) << "Malformed feature entry " << entry->id(); |
| 725 return NULL; |
| 726 } |
| 727 dictionary_entry_count++; |
| 728 } |
| 729 } |
| 730 |
| 731 if (top_level) { |
| 732 const base::ListValue* exception_list_value = NULL; |
| 733 if (value->GetList("exceptions", &exception_list_value)) { |
| 734 for (size_t i = 0; i < exception_list_value->GetSize(); ++i) { |
| 735 const base::DictionaryValue* exception_value = NULL; |
| 736 if (!exception_list_value->GetDictionary(i, &exception_value)) { |
| 737 LOG(WARNING) << "Malformed exceptions entry " << entry->id(); |
| 738 return NULL; |
| 739 } |
| 740 ScopedGpuControlListEntry exception( |
| 741 GetEntryFromValue(exception_value, false, feature_map)); |
| 742 if (exception == NULL) { |
| 743 LOG(WARNING) << "Malformed exceptions entry " << entry->id(); |
| 744 return NULL; |
| 745 } |
| 746 if (exception->contains_unknown_fields_) { |
| 747 LOG(WARNING) << "Exception with unknown fields " << entry->id(); |
| 748 entry->contains_unknown_fields_ = true; |
| 749 } else { |
| 750 entry->AddException(exception); |
| 751 } |
| 752 } |
| 753 dictionary_entry_count++; |
| 754 } |
| 755 |
| 756 const base::DictionaryValue* browser_version_value = NULL; |
| 757 // browser_version is processed in LoadGpuControlList(). |
| 758 if (value->GetDictionary("browser_version", &browser_version_value)) |
| 759 dictionary_entry_count++; |
| 760 } |
| 761 |
| 762 if (value->size() != dictionary_entry_count) { |
| 763 LOG(WARNING) << "Entry with unknown fields " << entry->id(); |
| 764 entry->contains_unknown_fields_ = true; |
| 765 } |
| 766 return entry; |
| 767 } |
| 768 |
| 769 GpuControlList::GpuControlListEntry::GpuControlListEntry() |
| 770 : id_(0), |
| 771 disabled_(false), |
| 772 vendor_id_(0), |
| 773 multi_gpu_style_(kMultiGpuStyleNone), |
| 774 multi_gpu_category_(kMultiGpuCategoryPrimary), |
| 775 contains_unknown_fields_(false), |
| 776 contains_unknown_features_(false) { |
| 777 } |
| 778 |
| 779 GpuControlList::GpuControlListEntry::~GpuControlListEntry() { } |
| 780 |
| 781 bool GpuControlList::GpuControlListEntry::SetId(uint32 id) { |
| 782 if (id != 0) { |
| 783 id_ = id; |
| 784 return true; |
| 785 } |
| 786 return false; |
| 787 } |
| 788 |
| 789 void GpuControlList::GpuControlListEntry::SetDisabled(bool disabled) { |
| 790 disabled_ = disabled; |
| 791 } |
| 792 |
| 793 bool GpuControlList::GpuControlListEntry::SetOsInfo( |
| 794 const std::string& os, |
| 795 const std::string& version_op, |
| 796 const std::string& version_string, |
| 797 const std::string& version_string2) { |
| 798 os_info_.reset(new OsInfo(os, version_op, version_string, version_string2)); |
| 799 return os_info_->IsValid(); |
| 800 } |
| 801 |
| 802 bool GpuControlList::GpuControlListEntry::SetVendorId( |
| 803 const std::string& vendor_id_string) { |
| 804 vendor_id_ = 0; |
| 805 return base::HexStringToInt(vendor_id_string, |
| 806 reinterpret_cast<int*>(&vendor_id_)); |
| 807 } |
| 808 |
| 809 bool GpuControlList::GpuControlListEntry::AddDeviceId( |
| 810 const std::string& device_id_string) { |
| 811 uint32 device_id = 0; |
| 812 if (base::HexStringToInt(device_id_string, |
| 813 reinterpret_cast<int*>(&device_id))) { |
| 814 device_id_list_.push_back(device_id); |
| 815 return true; |
| 816 } |
| 817 return false; |
| 818 } |
| 819 |
| 820 bool GpuControlList::GpuControlListEntry::SetMultiGpuStyle( |
| 821 const std::string& multi_gpu_style_string) { |
| 822 MultiGpuStyle style = StringToMultiGpuStyle(multi_gpu_style_string); |
| 823 if (style == kMultiGpuStyleNone) |
| 824 return false; |
| 825 multi_gpu_style_ = style; |
| 826 return true; |
| 827 } |
| 828 |
| 829 bool GpuControlList::GpuControlListEntry::SetMultiGpuCategory( |
| 830 const std::string& multi_gpu_category_string) { |
| 831 MultiGpuCategory category = |
| 832 StringToMultiGpuCategory(multi_gpu_category_string); |
| 833 if (category == kMultiGpuCategoryNone) |
| 834 return false; |
| 835 multi_gpu_category_ = category; |
| 836 return true; |
| 837 } |
| 838 |
| 839 bool GpuControlList::GpuControlListEntry::SetDriverVendorInfo( |
| 840 const std::string& vendor_op, |
| 841 const std::string& vendor_value) { |
| 842 driver_vendor_info_.reset(new StringInfo(vendor_op, vendor_value)); |
| 843 return driver_vendor_info_->IsValid(); |
| 844 } |
| 845 |
| 846 bool GpuControlList::GpuControlListEntry::SetDriverVersionInfo( |
| 847 const std::string& version_op, |
| 848 const std::string& version_style, |
| 849 const std::string& version_string, |
| 850 const std::string& version_string2) { |
| 851 driver_version_info_.reset(new VersionInfo( |
| 852 version_op, version_style, version_string, version_string2)); |
| 853 return driver_version_info_->IsValid(); |
| 854 } |
| 855 |
| 856 bool GpuControlList::GpuControlListEntry::SetDriverDateInfo( |
| 857 const std::string& date_op, |
| 858 const std::string& date_string, |
| 859 const std::string& date_string2) { |
| 860 driver_date_info_.reset( |
| 861 new VersionInfo(date_op, "", date_string, date_string2)); |
| 862 return driver_date_info_->IsValid(); |
| 863 } |
| 864 |
| 865 bool GpuControlList::GpuControlListEntry::SetGLVendorInfo( |
| 866 const std::string& vendor_op, |
| 867 const std::string& vendor_value) { |
| 868 gl_vendor_info_.reset(new StringInfo(vendor_op, vendor_value)); |
| 869 return gl_vendor_info_->IsValid(); |
| 870 } |
| 871 |
| 872 bool GpuControlList::GpuControlListEntry::SetGLRendererInfo( |
| 873 const std::string& renderer_op, |
| 874 const std::string& renderer_value) { |
| 875 gl_renderer_info_.reset(new StringInfo(renderer_op, renderer_value)); |
| 876 return gl_renderer_info_->IsValid(); |
| 877 } |
| 878 |
| 879 bool GpuControlList::GpuControlListEntry::SetCpuBrand( |
| 880 const std::string& cpu_op, |
| 881 const std::string& cpu_value) { |
| 882 cpu_brand_.reset(new StringInfo(cpu_op, cpu_value)); |
| 883 return cpu_brand_->IsValid(); |
| 884 } |
| 885 |
| 886 bool GpuControlList::GpuControlListEntry::SetPerfGraphicsInfo( |
| 887 const std::string& op, |
| 888 const std::string& float_string, |
| 889 const std::string& float_string2) { |
| 890 perf_graphics_info_.reset(new FloatInfo(op, float_string, float_string2)); |
| 891 return perf_graphics_info_->IsValid(); |
| 892 } |
| 893 |
| 894 bool GpuControlList::GpuControlListEntry::SetPerfGamingInfo( |
| 895 const std::string& op, |
| 896 const std::string& float_string, |
| 897 const std::string& float_string2) { |
| 898 perf_gaming_info_.reset(new FloatInfo(op, float_string, float_string2)); |
| 899 return perf_gaming_info_->IsValid(); |
| 900 } |
| 901 |
| 902 bool GpuControlList::GpuControlListEntry::SetPerfOverallInfo( |
| 903 const std::string& op, |
| 904 const std::string& float_string, |
| 905 const std::string& float_string2) { |
| 906 perf_overall_info_.reset(new FloatInfo(op, float_string, float_string2)); |
| 907 return perf_overall_info_->IsValid(); |
| 908 } |
| 909 |
| 910 bool GpuControlList::GpuControlListEntry::SetMachineModelInfo( |
| 911 const std::string& name_op, |
| 912 const std::string& name_value, |
| 913 const std::string& version_op, |
| 914 const std::string& version_string, |
| 915 const std::string& version_string2) { |
| 916 machine_model_info_.reset(new MachineModelInfo( |
| 917 name_op, name_value, version_op, version_string, version_string2)); |
| 918 return machine_model_info_->IsValid(); |
| 919 } |
| 920 |
| 921 bool GpuControlList::GpuControlListEntry::SetGpuCountInfo( |
| 922 const std::string& op, |
| 923 const std::string& int_string, |
| 924 const std::string& int_string2) { |
| 925 gpu_count_info_.reset(new IntInfo(op, int_string, int_string2)); |
| 926 return gpu_count_info_->IsValid(); |
| 927 } |
| 928 |
| 929 bool GpuControlList::GpuControlListEntry::SetFeatures( |
| 930 const std::vector<std::string>& feature_strings, |
| 931 const FeatureMap& feature_map) { |
| 932 size_t size = feature_strings.size(); |
| 933 if (size == 0) |
| 934 return false; |
| 935 int features = 0; |
| 936 for (size_t i = 0; i < size; ++i) { |
| 937 int feature = 0; |
| 938 bool valid = StringToFeature(feature_strings[i], &feature, feature_map); |
| 939 if (valid) |
| 940 features |= feature; |
| 941 else |
| 942 contains_unknown_features_ = true; |
| 943 } |
| 944 features_ = features; |
| 945 return true; |
| 946 } |
| 947 |
| 948 void GpuControlList::GpuControlListEntry::AddException( |
| 949 ScopedGpuControlListEntry exception) { |
| 950 exceptions_.push_back(exception); |
| 951 } |
| 952 |
| 953 // static |
| 954 GpuControlList::GpuControlListEntry::MultiGpuStyle |
| 955 GpuControlList::GpuControlListEntry::StringToMultiGpuStyle( |
| 956 const std::string& style) { |
| 957 if (style == kMultiGpuStyleStringOptimus) |
| 958 return kMultiGpuStyleOptimus; |
| 959 if (style == kMultiGpuStyleStringAMDSwitchable) |
| 960 return kMultiGpuStyleAMDSwitchable; |
| 961 return kMultiGpuStyleNone; |
| 962 } |
| 963 |
| 964 // static |
| 965 GpuControlList::GpuControlListEntry::MultiGpuCategory |
| 966 GpuControlList::GpuControlListEntry::StringToMultiGpuCategory( |
| 967 const std::string& category) { |
| 968 if (category == kMultiGpuCategoryStringPrimary) |
| 969 return kMultiGpuCategoryPrimary; |
| 970 if (category == kMultiGpuCategoryStringSecondary) |
| 971 return kMultiGpuCategorySecondary; |
| 972 if (category == kMultiGpuCategoryStringAny) |
| 973 return kMultiGpuCategoryAny; |
| 974 return kMultiGpuCategoryNone; |
| 975 } |
| 976 |
| 977 bool GpuControlList::GpuControlListEntry::Contains( |
| 978 OsType os_type, const std::string& os_version, |
| 979 const GPUInfo& gpu_info) const { |
| 980 DCHECK(os_type != kOsAny); |
| 981 if (os_info_.get() != NULL && !os_info_->Contains(os_type, os_version)) |
| 982 return false; |
| 983 bool is_not_primary_gpu = |
| 984 GpuUnmatched(vendor_id_, device_id_list_, gpu_info.gpu); |
| 985 bool is_not_secondary_gpu = true; |
| 986 for (size_t i = 0; i < gpu_info.secondary_gpus.size(); ++i) { |
| 987 is_not_secondary_gpu = is_not_secondary_gpu && |
| 988 GpuUnmatched(vendor_id_, device_id_list_, gpu_info.secondary_gpus[i]); |
| 989 } |
| 990 switch (multi_gpu_category_) { |
| 991 case kMultiGpuCategoryPrimary: |
| 992 if (is_not_primary_gpu) |
| 993 return false; |
| 994 break; |
| 995 case kMultiGpuCategorySecondary: |
| 996 if (is_not_secondary_gpu) |
| 997 return false; |
| 998 break; |
| 999 case kMultiGpuCategoryAny: |
| 1000 if (is_not_primary_gpu && is_not_secondary_gpu) |
| 1001 return false; |
| 1002 break; |
| 1003 case kMultiGpuCategoryNone: |
| 1004 break; |
| 1005 } |
| 1006 switch (multi_gpu_style_) { |
| 1007 case kMultiGpuStyleOptimus: |
| 1008 if (!gpu_info.optimus) |
| 1009 return false; |
| 1010 break; |
| 1011 case kMultiGpuStyleAMDSwitchable: |
| 1012 if (!gpu_info.amd_switchable) |
| 1013 return false; |
| 1014 break; |
| 1015 case kMultiGpuStyleNone: |
| 1016 break; |
| 1017 } |
| 1018 if (driver_vendor_info_.get() != NULL && !gpu_info.driver_vendor.empty() && |
| 1019 !driver_vendor_info_->Contains(gpu_info.driver_vendor)) |
| 1020 return false; |
| 1021 if (driver_version_info_.get() != NULL && !gpu_info.driver_version.empty()) { |
| 1022 if (!driver_version_info_->Contains(gpu_info.driver_version)) |
| 1023 return false; |
| 1024 } |
| 1025 if (driver_date_info_.get() != NULL && !gpu_info.driver_date.empty()) { |
| 1026 if (!driver_date_info_->Contains(gpu_info.driver_date, '-')) |
| 1027 return false; |
| 1028 } |
| 1029 if (gl_vendor_info_.get() != NULL && !gpu_info.gl_vendor.empty() && |
| 1030 !gl_vendor_info_->Contains(gpu_info.gl_vendor)) |
| 1031 return false; |
| 1032 if (gl_renderer_info_.get() != NULL && !gpu_info.gl_renderer.empty() && |
| 1033 !gl_renderer_info_->Contains(gpu_info.gl_renderer)) |
| 1034 return false; |
| 1035 if (perf_graphics_info_.get() != NULL && |
| 1036 (gpu_info.performance_stats.graphics == 0.0 || |
| 1037 !perf_graphics_info_->Contains(gpu_info.performance_stats.graphics))) |
| 1038 return false; |
| 1039 if (perf_gaming_info_.get() != NULL && |
| 1040 (gpu_info.performance_stats.gaming == 0.0 || |
| 1041 !perf_gaming_info_->Contains(gpu_info.performance_stats.gaming))) |
| 1042 return false; |
| 1043 if (perf_overall_info_.get() != NULL && |
| 1044 (gpu_info.performance_stats.overall == 0.0 || |
| 1045 !perf_overall_info_->Contains(gpu_info.performance_stats.overall))) |
| 1046 return false; |
| 1047 if (machine_model_info_.get() != NULL) { |
| 1048 std::vector<std::string> name_version; |
| 1049 base::SplitString(gpu_info.machine_model, ' ', &name_version); |
| 1050 if (name_version.size() == 2 && |
| 1051 !machine_model_info_->Contains(name_version[0], name_version[1])) |
| 1052 return false; |
| 1053 } |
| 1054 if (gpu_count_info_.get() != NULL && |
| 1055 !gpu_count_info_->Contains(gpu_info.secondary_gpus.size() + 1)) |
| 1056 return false; |
| 1057 if (cpu_brand_.get() != NULL) { |
| 1058 base::CPU cpu_info; |
| 1059 if (!cpu_brand_->Contains(cpu_info.cpu_brand())) |
| 1060 return false; |
| 1061 } |
| 1062 |
| 1063 for (size_t i = 0; i < exceptions_.size(); ++i) { |
| 1064 if (exceptions_[i]->Contains(os_type, os_version, gpu_info) && |
| 1065 !exceptions_[i]->NeedsMoreInfo(gpu_info)) |
| 1066 return false; |
| 1067 } |
| 1068 return true; |
| 1069 } |
| 1070 |
| 1071 bool GpuControlList::GpuControlListEntry::NeedsMoreInfo( |
| 1072 const GPUInfo& gpu_info) const { |
| 1073 // We only check for missing info that might be collected with a gl context. |
| 1074 // If certain info is missing due to some error, say, we fail to collect |
| 1075 // vendor_id/device_id, then even if we launch GPU process and create a gl |
| 1076 // context, we won't gather such missing info, so we still return false. |
| 1077 if (driver_vendor_info_.get() && gpu_info.driver_vendor.empty()) |
| 1078 return true; |
| 1079 if (driver_version_info_.get() && gpu_info.driver_version.empty()) |
| 1080 return true; |
| 1081 if (gl_vendor_info_.get() && gpu_info.gl_vendor.empty()) |
| 1082 return true; |
| 1083 if (gl_renderer_info_.get() && gpu_info.gl_renderer.empty()) |
| 1084 return true; |
| 1085 for (size_t i = 0; i < exceptions_.size(); ++i) { |
| 1086 if (exceptions_[i]->NeedsMoreInfo(gpu_info)) |
| 1087 return true; |
| 1088 } |
| 1089 return false; |
| 1090 } |
| 1091 |
| 1092 GpuControlList::OsType GpuControlList::GpuControlListEntry::GetOsType() const { |
| 1093 if (os_info_.get() == NULL) |
| 1094 return kOsAny; |
| 1095 return os_info_->type(); |
| 1096 } |
| 1097 |
| 1098 uint32 GpuControlList::GpuControlListEntry::id() const { |
| 1099 return id_; |
| 1100 } |
| 1101 |
| 1102 bool GpuControlList::GpuControlListEntry::disabled() const { |
| 1103 return disabled_; |
| 1104 } |
| 1105 |
| 1106 int GpuControlList::GpuControlListEntry::GetFeatures() const { |
| 1107 return features_; |
| 1108 } |
| 1109 |
| 1110 // static |
| 1111 bool GpuControlList::GpuControlListEntry::StringToFeature( |
| 1112 const std::string& feature_name, int* feature_id, |
| 1113 const FeatureMap& feature_map) { |
| 1114 FeatureMap::const_iterator iter = feature_map.find(feature_name); |
| 1115 if (iter != feature_map.end()) { |
| 1116 *feature_id = iter->second; |
| 1117 return true; |
| 1118 } |
| 1119 return false; |
| 1120 } |
| 1121 |
| 1122 GpuControlList::GpuControlList() |
| 1123 : max_entry_id_(0), |
| 1124 contains_unknown_fields_(false), |
| 1125 needs_more_info_(false) { |
| 1126 } |
| 1127 |
| 1128 GpuControlList::~GpuControlList() { |
| 1129 Clear(); |
| 1130 } |
| 1131 |
| 1132 bool GpuControlList::LoadList( |
| 1133 const std::string& json_context, GpuControlList::OsFilter os_filter) { |
| 1134 const std::string browser_version_string = "0"; |
| 1135 return LoadList(browser_version_string, json_context, os_filter); |
| 1136 } |
| 1137 |
| 1138 bool GpuControlList::LoadList( |
| 1139 const std::string& browser_version_string, |
| 1140 const std::string& json_context, |
| 1141 GpuControlList::OsFilter os_filter) { |
| 1142 std::vector<std::string> pieces; |
| 1143 if (!ProcessVersionString(browser_version_string, '.', &pieces)) |
| 1144 return false; |
| 1145 browser_version_ = browser_version_string; |
| 1146 |
| 1147 scoped_ptr<base::Value> root; |
| 1148 root.reset(base::JSONReader::Read(json_context)); |
| 1149 if (root.get() == NULL || !root->IsType(base::Value::TYPE_DICTIONARY)) |
| 1150 return false; |
| 1151 |
| 1152 base::DictionaryValue* root_dictionary = |
| 1153 static_cast<DictionaryValue*>(root.get()); |
| 1154 DCHECK(root_dictionary); |
| 1155 return LoadList(*root_dictionary, os_filter); |
| 1156 } |
| 1157 |
| 1158 bool GpuControlList::LoadList(const base::DictionaryValue& parsed_json, |
| 1159 GpuControlList::OsFilter os_filter) { |
| 1160 std::vector<ScopedGpuControlListEntry> entries; |
| 1161 |
| 1162 parsed_json.GetString("version", &version_); |
| 1163 std::vector<std::string> pieces; |
| 1164 if (!ProcessVersionString(version_, '.', &pieces)) |
| 1165 return false; |
| 1166 |
| 1167 const base::ListValue* list = NULL; |
| 1168 if (!parsed_json.GetList("entries", &list)) |
| 1169 return false; |
| 1170 |
| 1171 uint32 max_entry_id = 0; |
| 1172 bool contains_unknown_fields = false; |
| 1173 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 1174 const base::DictionaryValue* list_item = NULL; |
| 1175 bool valid = list->GetDictionary(i, &list_item); |
| 1176 if (!valid || list_item == NULL) |
| 1177 return false; |
| 1178 // Check browser version compatibility: if the entry is not for the |
| 1179 // current browser version, don't process it. |
| 1180 BrowserVersionSupport browser_version_support = |
| 1181 IsEntrySupportedByCurrentBrowserVersion(list_item); |
| 1182 if (browser_version_support == kMalformed) |
| 1183 return false; |
| 1184 if (browser_version_support == kUnsupported) |
| 1185 continue; |
| 1186 DCHECK(browser_version_support == kSupported); |
| 1187 ScopedGpuControlListEntry entry( |
| 1188 GpuControlListEntry::GetEntryFromValue(list_item, true, feature_map_)); |
| 1189 if (entry == NULL) |
| 1190 return false; |
| 1191 if (entry->id() > max_entry_id) |
| 1192 max_entry_id = entry->id(); |
| 1193 // If an unknown field is encountered, skip the entry; if an unknown |
| 1194 // feature is encountered, ignore the feature, but keep the entry. |
| 1195 if (entry->contains_unknown_fields()) { |
| 1196 contains_unknown_fields = true; |
| 1197 continue; |
| 1198 } |
| 1199 if (entry->contains_unknown_features()) |
| 1200 contains_unknown_fields = true; |
| 1201 entries.push_back(entry); |
| 1202 } |
| 1203 |
| 1204 Clear(); |
| 1205 OsType my_os = GetOsType(); |
| 1206 for (size_t i = 0; i < entries.size(); ++i) { |
| 1207 OsType entry_os = entries[i]->GetOsType(); |
| 1208 if (os_filter == GpuControlList::kAllOs || |
| 1209 entry_os == kOsAny || entry_os == my_os) |
| 1210 feature_list_.push_back(entries[i]); |
| 1211 } |
| 1212 max_entry_id_ = max_entry_id; |
| 1213 contains_unknown_fields_ = contains_unknown_fields; |
| 1214 return true; |
| 1215 } |
| 1216 |
| 1217 int GpuControlList::MakeDecision( |
| 1218 GpuControlList::OsType os, |
| 1219 std::string os_version, |
| 1220 const GPUInfo& gpu_info) { |
| 1221 active_entries_.clear(); |
| 1222 int features = 0; |
| 1223 |
| 1224 needs_more_info_ = false; |
| 1225 int possible_features = 0; |
| 1226 |
| 1227 if (os == kOsAny) |
| 1228 os = GetOsType(); |
| 1229 if (os_version.empty()) { |
| 1230 os_version = base::SysInfo::OperatingSystemVersion(); |
| 1231 size_t pos = os_version.find_first_not_of("0123456789."); |
| 1232 if (pos != std::string::npos) |
| 1233 os_version = os_version.substr(0, pos); |
| 1234 } |
| 1235 std::vector<std::string> pieces; |
| 1236 if (!ProcessVersionString(os_version, '.', &pieces)) |
| 1237 os_version = "0"; |
| 1238 |
| 1239 for (size_t i = 0; i < feature_list_.size(); ++i) { |
| 1240 if (feature_list_[i]->Contains(os, os_version, gpu_info)) { |
| 1241 if (!feature_list_[i]->disabled()) { |
| 1242 bool not_final = feature_list_[i]->NeedsMoreInfo(gpu_info); |
| 1243 if (not_final) |
| 1244 possible_features |= feature_list_[i]->GetFeatures(); |
| 1245 else |
| 1246 features |= feature_list_[i]->GetFeatures(); |
| 1247 } |
| 1248 active_entries_.push_back(feature_list_[i]); |
| 1249 } |
| 1250 } |
| 1251 |
| 1252 if (possible_features != 0 && (possible_features | features) != features) |
| 1253 needs_more_info_ = true; |
| 1254 |
| 1255 return features; |
| 1256 } |
| 1257 |
| 1258 void GpuControlList::GetDecisionEntries( |
| 1259 std::vector<uint32>* entry_ids, bool disabled) const { |
| 1260 DCHECK(entry_ids); |
| 1261 entry_ids->clear(); |
| 1262 for (size_t i = 0; i < active_entries_.size(); ++i) { |
| 1263 if (disabled == active_entries_[i]->disabled()) |
| 1264 entry_ids->push_back(active_entries_[i]->id()); |
| 1265 } |
| 1266 } |
| 1267 |
| 1268 void GpuControlList::GetReasons(base::ListValue* problem_list) const { |
| 1269 DCHECK(problem_list); |
| 1270 for (size_t i = 0; i < active_entries_.size(); ++i) { |
| 1271 GpuControlListEntry* entry = active_entries_[i]; |
| 1272 if (entry->disabled()) |
| 1273 continue; |
| 1274 base::DictionaryValue* problem = new base::DictionaryValue(); |
| 1275 |
| 1276 problem->SetString("description", entry->description()); |
| 1277 |
| 1278 base::ListValue* cr_bugs = new base::ListValue(); |
| 1279 for (size_t j = 0; j < entry->cr_bugs().size(); ++j) |
| 1280 cr_bugs->Append(new base::FundamentalValue(entry->cr_bugs()[j])); |
| 1281 problem->Set("crBugs", cr_bugs); |
| 1282 |
| 1283 base::ListValue* webkit_bugs = new base::ListValue(); |
| 1284 for (size_t j = 0; j < entry->webkit_bugs().size(); ++j) { |
| 1285 webkit_bugs->Append(new base::FundamentalValue(entry->webkit_bugs()[j])); |
| 1286 } |
| 1287 problem->Set("webkitBugs", webkit_bugs); |
| 1288 |
| 1289 problem_list->Append(problem); |
| 1290 } |
| 1291 } |
| 1292 |
| 1293 size_t GpuControlList::num_entries() const { |
| 1294 return feature_list_.size(); |
| 1295 } |
| 1296 |
| 1297 uint32 GpuControlList::max_entry_id() const { |
| 1298 return max_entry_id_; |
| 1299 } |
| 1300 |
| 1301 std::string GpuControlList::GetVersion() const { |
| 1302 return version_; |
| 1303 } |
| 1304 |
| 1305 GpuControlList::OsType GpuControlList::GetOsType() { |
| 1306 #if defined(OS_CHROMEOS) |
| 1307 return kOsChromeOS; |
| 1308 #elif defined(OS_WIN) |
| 1309 return kOsWin; |
| 1310 #elif defined(OS_ANDROID) |
| 1311 return kOsAndroid; |
| 1312 #elif defined(OS_LINUX) || defined(OS_OPENBSD) |
| 1313 return kOsLinux; |
| 1314 #elif defined(OS_MACOSX) |
| 1315 return kOsMacosx; |
| 1316 #else |
| 1317 return kOsUnknown; |
| 1318 #endif |
| 1319 } |
| 1320 |
| 1321 void GpuControlList::Clear() { |
| 1322 feature_list_.clear(); |
| 1323 active_entries_.clear(); |
| 1324 max_entry_id_ = 0; |
| 1325 contains_unknown_fields_ = false; |
| 1326 } |
| 1327 |
| 1328 GpuControlList::BrowserVersionSupport |
| 1329 GpuControlList::IsEntrySupportedByCurrentBrowserVersion( |
| 1330 const base::DictionaryValue* value) { |
| 1331 DCHECK(value); |
| 1332 const base::DictionaryValue* browser_version_value = NULL; |
| 1333 if (value->GetDictionary("browser_version", &browser_version_value)) { |
| 1334 std::string version_op = "any"; |
| 1335 std::string version_string; |
| 1336 std::string version_string2; |
| 1337 browser_version_value->GetString(kOp, &version_op); |
| 1338 browser_version_value->GetString("number", &version_string); |
| 1339 browser_version_value->GetString("number2", &version_string2); |
| 1340 scoped_ptr<VersionInfo> browser_version_info; |
| 1341 browser_version_info.reset( |
| 1342 new VersionInfo(version_op, "", version_string, version_string2)); |
| 1343 if (!browser_version_info->IsValid()) |
| 1344 return kMalformed; |
| 1345 if (browser_version_info->Contains(browser_version_)) |
| 1346 return kSupported; |
| 1347 return kUnsupported; |
| 1348 } |
| 1349 return kSupported; |
| 1350 } |
| 1351 |
| 1352 // static |
| 1353 GpuControlList::NumericOp GpuControlList::StringToNumericOp( |
| 1354 const std::string& op) { |
| 1355 if (op == "=") |
| 1356 return kEQ; |
| 1357 if (op == "<") |
| 1358 return kLT; |
| 1359 if (op == "<=") |
| 1360 return kLE; |
| 1361 if (op == ">") |
| 1362 return kGT; |
| 1363 if (op == ">=") |
| 1364 return kGE; |
| 1365 if (op == "any") |
| 1366 return kAny; |
| 1367 if (op == "between") |
| 1368 return kBetween; |
| 1369 return kUnknown; |
| 1370 } |
| 1371 |
| 1372 void GpuControlList::AddFeature( |
| 1373 const std::string& feature_name, int feature_id) { |
| 1374 feature_map_[feature_name] = feature_id; |
| 1375 } |
| 1376 |
| 1377 } // namespace content |
| 1378 |
OLD | NEW |