root/chrome/browser/translate/translate_service.cc

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. Initialize
  2. Shutdown
  3. InitializeForTesting
  4. ShutdownForTesting
  5. OnResourceRequestsAllowed
  6. IsTranslateBubbleEnabled
  7. SetUseInfobar
  8. GetTargetLanguage

// Copyright 2014 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/translate/translate_service.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_split.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/translate/translate_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/translate/core/browser/translate_download_manager.h"

namespace {
// The singleton instance of TranslateService.
TranslateService* g_translate_service = NULL;
}

TranslateService::TranslateService() : use_infobar_(false) {
  resource_request_allowed_notifier_.Init(this);
}

TranslateService::~TranslateService() {}

// static
void TranslateService::Initialize() {
  if (g_translate_service)
    return;

  g_translate_service = new TranslateService;
  // Initialize the allowed state for resource requests.
  g_translate_service->OnResourceRequestsAllowed();
  TranslateDownloadManager* download_manager =
      TranslateDownloadManager::GetInstance();
  download_manager->set_request_context(
      g_browser_process->system_request_context());
  download_manager->set_application_locale(
      g_browser_process->GetApplicationLocale());
}

// static
void TranslateService::Shutdown(bool cleanup_pending_fetcher) {
  TranslateDownloadManager* download_manager =
      TranslateDownloadManager::GetInstance();
  if (cleanup_pending_fetcher) {
    download_manager->Shutdown();
  } else {
    // This path is only used by browser tests.
    download_manager->set_request_context(NULL);
  }
}

// static
void TranslateService::InitializeForTesting() {
  if (!g_translate_service) {
    TranslateService::Initialize();
  } else {
    TranslateDownloadManager::GetInstance()->ResetForTesting();
    g_translate_service->OnResourceRequestsAllowed();
  }
}

// static
void TranslateService::ShutdownForTesting() {
  TranslateDownloadManager::GetInstance()->Shutdown();
}

void TranslateService::OnResourceRequestsAllowed() {
  TranslateLanguageList* language_list =
      TranslateDownloadManager::GetInstance()->language_list();
  if (!language_list) {
    NOTREACHED();
    return;
  }

  language_list->SetResourceRequestsAllowed(
      resource_request_allowed_notifier_.ResourceRequestsAllowed());
}

// static
bool TranslateService::IsTranslateBubbleEnabled() {
#if defined(USE_AURA)
  Initialize();
  return !g_translate_service->use_infobar_;
#elif defined(OS_MACOSX)
  // The bubble UX is experimental on Mac OS X.
  return CommandLine::ForCurrentProcess()->HasSwitch(
      switches::kEnableTranslateNewUX);
#else
  // The bubble UX is not implemented on the non-Aura platforms.
  return false;
#endif
}

// static
void TranslateService::SetUseInfobar(bool value) {
  Initialize();
  g_translate_service->use_infobar_ = value;
}

// static
std::string TranslateService::GetTargetLanguage(PrefService* prefs) {
  std::vector<std::string> accept_languages_list;
  base::SplitString(prefs->GetString(prefs::kAcceptLanguages), ',',
                    &accept_languages_list);
  return TranslateManager::GetTargetLanguage(accept_languages_list);
}

/* [<][>][^][v][top][bottom][index][help] */