chrome_http_user_agent_settings.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/net/chrome_http_user_agent_settings.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/content_client.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ChromeHttpUserAgentSettings::ChromeHttpUserAgentSettings(PrefService* prefs) {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  pref_accept_language_.Init(prefs::kAcceptLanguages, prefs);
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_pref_accept_language_ = *pref_accept_language_;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_http_accept_language_ =
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::HttpUtil::GenerateAcceptLanguageHeader(last_pref_accept_language_);
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_language_.MoveToThread(
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::GetMessageLoopProxyForThread(
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          content::BrowserThread::IO));
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ChromeHttpUserAgentSettings::~ChromeHttpUserAgentSettings() {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ChromeHttpUserAgentSettings::CleanupOnUIThread() {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_language_.Destroy();
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ChromeHttpUserAgentSettings::GetAcceptLanguage() const {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string new_pref_accept_language = *pref_accept_language_;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (new_pref_accept_language != last_pref_accept_language_) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_http_accept_language_ =
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net::HttpUtil::GenerateAcceptLanguageHeader(new_pref_accept_language);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_pref_accept_language_ = new_pref_accept_language;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return last_http_accept_language_;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ChromeHttpUserAgentSettings::GetUserAgent(const GURL& url) const {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return content::GetUserAgent(url);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
49