<commit_before>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_resource/gpu_blacklist_updater.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "content/browser/browser_thread.h"
#include "content/browser/gpu/gpu_blacklist.h"
#include "content/browser/gpu/gpu_data_manager.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gl/gl_implementation.h"
#include "ui/gfx/gl/gl_switches.h"

namespace {

// Delay on first fetch so we don't interfere with startup.
static const int kStartGpuBlacklistFetchDelay = 6000;

// Delay between calls to update the gpu blacklist (48 hours).
static const int kCacheUpdateDelay = 48 * 60 * 60 * 1000;

std::string GetChromeVersionString() {
  chrome::VersionInfo version_info;
  std::string rt =  version_info.is_valid() ? version_info.Version() : "0";
  switch (version_info.GetChannel()) {
    case chrome::VersionInfo::CHANNEL_STABLE:
      rt += " stable";
      break;
    case chrome::VersionInfo::CHANNEL_BETA:
      rt += " beta";
      break;
    case chrome::VersionInfo::CHANNEL_DEV:
      rt += " dev";
      break;
    case chrome::VersionInfo::CHANNEL_CANARY:
      rt += " canary";
      break;
    default:
      rt += " unknown";
      break;
  }
  return rt;
}

}  // namespace anonymous

const char* GpuBlacklistUpdater::kDefaultGpuBlacklistURL =
    "https://dl.google.com/dl/edgedl/chrome/gpu/software_rendering_list.json";

GpuBlacklistUpdater::GpuBlacklistUpdater()
    : WebResourceService(g_browser_process->local_state(),
                         GpuBlacklistUpdater::kDefaultGpuBlacklistURL,
                         false,  // don't append locale to URL
                         chrome::NOTIFICATION_CHROME_END,
                         prefs::kGpuBlacklistUpdate,
                         kStartGpuBlacklistFetchDelay,
                         kCacheUpdateDelay) {
  prefs_->RegisterDictionaryPref(prefs::kGpuBlacklist);
  InitializeGpuBlacklist();
}

GpuBlacklistUpdater::~GpuBlacklistUpdater() { }

// static
void GpuBlacklistUpdater::SetupOnFileThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  // Initialize GpuDataManager instance, which collects preliminary
  // graphics information.  This has to happen on FILE thread.
  GpuDataManager::GetInstance();

  // Skip auto updates in tests.
  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
  if ((command_line.GetSwitchValueASCII(switches::kUseGL) ==
      gfx::kGLImplementationOSMesaName))
    return;

  // Post GpuBlacklistUpdate task on UI thread.  This has to happen
  // after GpuDataManager is initialized, otherwise it might be
  // initialzed on UI thread.
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      NewRunnableFunction(&GpuBlacklistUpdater::SetupOnUIThread));<commit_msg>Move IO op from UI thread to FILE in GpuBlacklistUpdater.<commit_after>// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/web_resource/gpu_blacklist_updater.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/prefs/scoped_user_pref_update.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "content/browser/browser_thread.h"
#include "content/browser/gpu/gpu_blacklist.h"
#include "content/browser/gpu/gpu_data_manager.h"
#include "grit/browser_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gl/gl_implementation.h"
#include "ui/gfx/gl/gl_switches.h"

namespace {

// Delay on first fetch so we don't interfere with startup.
static const int kStartGpuBlacklistFetchDelay = 6000;

// Delay between calls to update the gpu blacklist (48 hours).
static const int kCacheUpdateDelay = 48 * 60 * 60 * 1000;

std::string GetChromeVersionString() {
  static std::string cr_version;
  if (!cr_version.empty())
    return cr_version;
  chrome::VersionInfo version_info;
  cr_version =  version_info.is_valid() ? version_info.Version() : "0";
  switch (version_info.GetChannel()) {
    case chrome::VersionInfo::CHANNEL_STABLE:
      cr_version += " stable";
      break;
    case chrome::VersionInfo::CHANNEL_BETA:
      cr_version += " beta";
      break;
    case chrome::VersionInfo::CHANNEL_DEV:
      cr_version += " dev";
      break;
    case chrome::VersionInfo::CHANNEL_CANARY:
      cr_version += " canary";
      break;
    default:
      cr_version += " unknown";
      break;
  }
  return cr_version;
}

}  // namespace anonymous

const char* GpuBlacklistUpdater::kDefaultGpuBlacklistURL =
    "https://dl.google.com/dl/edgedl/chrome/gpu/software_rendering_list.json";

GpuBlacklistUpdater::GpuBlacklistUpdater()
    : WebResourceService(g_browser_process->local_state(),
                         GpuBlacklistUpdater::kDefaultGpuBlacklistURL,
                         false,  // don't append locale to URL
                         chrome::NOTIFICATION_CHROME_END,
                         prefs::kGpuBlacklistUpdate,
                         kStartGpuBlacklistFetchDelay,
                         kCacheUpdateDelay) {
  prefs_->RegisterDictionaryPref(prefs::kGpuBlacklist);
  InitializeGpuBlacklist();
}

GpuBlacklistUpdater::~GpuBlacklistUpdater() { }

// static
void GpuBlacklistUpdater::SetupOnFileThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  // Initialize GpuDataManager instance, which collects preliminary
  // graphics information.  This has to happen on FILE thread.
  GpuDataManager::GetInstance();

  // Skip auto updates in tests.
  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
  if ((command_line.GetSwitchValueASCII(switches::kUseGL) ==
      gfx::kGLImplementationOSMesaName))
    return;

  // Cache the chrome version string.  This has to happen on FILE thread
  // because of chrome::VersionInfo::GetChannel().
  GetChromeVersionString();

  // Post GpuBlacklistUpdate task on UI thread.  This has to happen
  // after GpuDataManager is initialized, otherwise it might be
  // initialzed on UI thread.
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      NewRunnableFunction(&GpuBlacklistUpdater::SetupOnUIThread));
{'fraction_non_alphanumeric': 0.10895815782083684, 'fraction_numerical': 0.005897219882055603, 'mean_word_length': 3.9056473829201104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525179', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2183, 'n_words': 468}