chrome_http_user_agent_settings.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/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));
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_language_.Init(prefs::kAcceptLanguages, prefs, NULL);
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_charset_.Init(prefs::kDefaultCharset, prefs, NULL);
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_pref_accept_language_ = *pref_accept_language_;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_http_accept_language_ =
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::HttpUtil::GenerateAcceptLanguageHeader(last_pref_accept_language_);
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_pref_accept_charset_ = *pref_accept_charset_;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  last_http_accept_charset_ =
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net::HttpUtil::GenerateAcceptCharsetHeader(last_pref_accept_charset_);
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_language_.MoveToThread(
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::GetMessageLoopProxyForThread(
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          content::BrowserThread::IO));
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_charset_.MoveToThread(
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      content::BrowserThread::GetMessageLoopProxyForThread(
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          content::BrowserThread::IO));
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ChromeHttpUserAgentSettings::~ChromeHttpUserAgentSettings() {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ChromeHttpUserAgentSettings::CleanupOnUIThread() {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_language_.Destroy();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  pref_accept_charset_.Destroy();
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ChromeHttpUserAgentSettings::GetAcceptLanguage() const {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string new_pref_accept_language = *pref_accept_language_;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (new_pref_accept_language != last_pref_accept_language_) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_http_accept_language_ =
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net::HttpUtil::GenerateAcceptLanguageHeader(new_pref_accept_language);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_pref_accept_language_ = new_pref_accept_language;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return last_http_accept_language_;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ChromeHttpUserAgentSettings::GetAcceptCharset() const {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string new_pref_accept_charset = *pref_accept_charset_;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (new_pref_accept_charset != last_pref_accept_charset_) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_http_accept_charset_ =
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        net::HttpUtil::GenerateAcceptCharsetHeader(new_pref_accept_charset);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    last_pref_accept_charset_ = new_pref_accept_charset;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return last_http_accept_charset_;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string ChromeHttpUserAgentSettings::GetUserAgent(const GURL& url) const {
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return content::GetUserAgent(url);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
68