blob: 6bcbef59c178af34f710d84874c07c3721560232 [file] [log] [blame]
// Copyright 2016 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.
#import "ios/chrome/browser/voice/voice_search_url_rewriter.h"
#import <Foundation/Foundation.h>
#import "base/strings/sys_string_conversions.h"
#include "components/google/core/common/google_util.h"
#include "components/prefs/pref_service.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/voice/speech_input_locale_config.h"
#include "ios/public/provider/chrome/browser/voice/voice_search_prefs.h"
#include "net/base/url_util.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
bool VoiceSearchURLRewriter(GURL* url, web::BrowserState* browser_state) {
if (!google_util::IsGoogleSearchUrl(*url))
return false;
ios::ChromeBrowserState* chrome_browser_state =
ios::ChromeBrowserState::FromBrowserState(browser_state);
std::string language =
chrome_browser_state->GetPrefs()->GetString(prefs::kVoiceSearchLocale);
GURL rewritten_url(*url);
// The |hl| parameter will be overriden only if the voice search locale
// is not empty. If it is empty (indicating that voice search locale
// uses device language), the |hl| will keep the original value.
// If there is no |hl| in the query the |spknlang| will use the application
// locale as a fallback (instead of using the same locale for both |hl|
// and |spknlang|).
if (language.empty()) {
voice::SpeechInputLocaleConfig* locale_config =
voice::SpeechInputLocaleConfig::GetInstance();
if (locale_config)
language = locale_config->GetDefaultLocale().code;
if (!language.length()) {
NOTREACHED();
language = "en-US";
}
}
rewritten_url =
net::AppendOrReplaceQueryParameter(rewritten_url, "hl", language);
rewritten_url =
net::AppendQueryParameter(rewritten_url, "spknlang", language);
rewritten_url = net::AppendQueryParameter(rewritten_url, "inm", "vs");
rewritten_url = net::AppendQueryParameter(rewritten_url, "vse", "1");
*url = rewritten_url;
// Return false so other URLRewriters can update the url if necessary.
return false;
}