Home | History | Annotate | Download | only in omnibox
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/omnibox/location_bar_util.h"
      6 
      7 #include "base/i18n/rtl.h"
      8 #include "base/utf_string_conversions.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/search_engines/template_url.h"
     11 #include "chrome/browser/search_engines/template_url_model.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 
     14 namespace location_bar_util {
     15 
     16 std::wstring GetKeywordName(Profile* profile, const std::wstring& keyword) {
     17 // Make sure the TemplateURL still exists.
     18 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel
     19 // to track changes to the model, this should become a DCHECK.
     20   const TemplateURL* template_url =
     21       profile->GetTemplateURLModel()->GetTemplateURLForKeyword(
     22           WideToUTF16Hack(keyword));
     23   if (template_url)
     24     return UTF16ToWideHack(template_url->AdjustedShortNameForLocaleDirection());
     25   return std::wstring();
     26 }
     27 
     28 std::wstring CalculateMinString(const std::wstring& description) {
     29   // Chop at the first '.' or whitespace.
     30   const size_t dot_index = description.find(L'.');
     31   const size_t ws_index = description.find_first_of(kWhitespaceWide);
     32   size_t chop_index = std::min(dot_index, ws_index);
     33   std::wstring min_string;
     34   if (chop_index == std::wstring::npos) {
     35     // No dot or whitespace, truncate to at most 3 chars.
     36     min_string = UTF16ToWideHack(
     37         l10n_util::TruncateString(WideToUTF16Hack(description), 3));
     38   } else {
     39     min_string = description.substr(0, chop_index);
     40   }
     41   base::i18n::AdjustStringForLocaleDirection(&min_string);
     42   return min_string;
     43 }
     44 
     45 }  // namespace location_bar_util
     46