OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/spellchecker/spelling_service_client.h" | 5 #include "chrome/browser/spellchecker/spelling_service_client.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/string_escape.h" | 8 #include "base/json/string_escape.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // Also, we convert the given text to a JSON string, i.e. quote all its | 65 // Also, we convert the given text to a JSON string, i.e. quote all its |
66 // non-ASCII characters. | 66 // non-ASCII characters. |
67 UErrorCode error = U_ZERO_ERROR; | 67 UErrorCode error = U_ZERO_ERROR; |
68 char id[ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY]; | 68 char id[ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY]; |
69 uloc_addLikelySubtags(locale.c_str(), id, arraysize(id), &error); | 69 uloc_addLikelySubtags(locale.c_str(), id, arraysize(id), &error); |
70 | 70 |
71 error = U_ZERO_ERROR; | 71 error = U_ZERO_ERROR; |
72 uloc_getLanguage(id, language, arraysize(language), &error); | 72 uloc_getLanguage(id, language, arraysize(language), &error); |
73 country = uloc_getISO3Country(id); | 73 country = uloc_getISO3Country(id); |
74 } | 74 } |
75 if (type == SPELLCHECK && base::strcasecmp(language, ULOC_ENGLISH)) | 75 if (!IsAvailable(profile, SPELLCHECK)) |
76 return false; | 76 return false; |
77 | 77 |
78 // Format the JSON request to be sent to the Spelling service. | 78 // Format the JSON request to be sent to the Spelling service. |
79 std::string encoded_text; | 79 std::string encoded_text; |
80 base::JsonDoubleQuote(text, false, &encoded_text); | 80 base::JsonDoubleQuote(text, false, &encoded_text); |
81 | 81 |
82 static const char kSpellingRequest[] = | 82 static const char kSpellingRequest[] = |
83 "{" | 83 "{" |
84 "\"method\":\"spelling.check\"," | 84 "\"method\":\"spelling.check\"," |
85 "\"apiVersion\":\"v%d\"," | 85 "\"apiVersion\":\"v%d\"," |
(...skipping 19 matching lines...) Expand all Loading... |
105 fetcher_->SetUploadData("application/json", request); | 105 fetcher_->SetUploadData("application/json", request); |
106 fetcher_->SetLoadFlags( | 106 fetcher_->SetLoadFlags( |
107 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 107 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
108 fetcher_->Start(); | 108 fetcher_->Start(); |
109 tag_ = tag; | 109 tag_ = tag; |
110 text_ = text; | 110 text_ = text; |
111 callback_ = callback; | 111 callback_ = callback; |
112 return true; | 112 return true; |
113 } | 113 } |
114 | 114 |
| 115 bool SpellingServiceClient::IsAvailable(Profile* profile, ServiceType type) { |
| 116 const PrefService* pref = profile->GetPrefs(); |
| 117 if (!pref->GetBoolean(prefs::kEnableSpellCheck) || |
| 118 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) |
| 119 return false; |
| 120 |
| 121 // Enable the suggest service only on languages not supported by the |
| 122 // spellcheck service. When this client calls the spellcheck service, it |
| 123 // returns not only spellcheck results but also spelling suggestions provided |
| 124 // by the suggest service. That is, it is not useful to use the suggest |
| 125 // service when this client can use the spellcheck service. |
| 126 std::string locale = pref->GetString(prefs::kSpellCheckDictionary); |
| 127 bool spellcheck_available = locale.empty() || !locale.compare(0, 2, "en"); |
| 128 return type == SUGGEST ? !spellcheck_available : spellcheck_available; |
| 129 } |
| 130 |
115 void SpellingServiceClient::OnURLFetchComplete( | 131 void SpellingServiceClient::OnURLFetchComplete( |
116 const net::URLFetcher* source) { | 132 const net::URLFetcher* source) { |
117 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); | 133 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); |
118 bool success = false; | 134 bool success = false; |
119 std::vector<SpellCheckResult> results; | 135 std::vector<SpellCheckResult> results; |
120 if (source->GetResponseCode() / 100 == 2) { | 136 if (source->GetResponseCode() / 100 == 2) { |
121 std::string data; | 137 std::string data; |
122 source->GetResponseAsString(&data); | 138 source->GetResponseAsString(&data); |
123 success = ParseResponse(data, &results); | 139 success = ParseResponse(data, &results); |
124 } | 140 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 if (!suggestions->GetDictionary(0, &suggestion) || | 215 if (!suggestions->GetDictionary(0, &suggestion) || |
200 !suggestion->GetString("suggestion", &replacement)) { | 216 !suggestion->GetString("suggestion", &replacement)) { |
201 return false; | 217 return false; |
202 } | 218 } |
203 SpellCheckResult result( | 219 SpellCheckResult result( |
204 SpellCheckResult::SPELLING, start, length, replacement); | 220 SpellCheckResult::SPELLING, start, length, replacement); |
205 results->push_back(result); | 221 results->push_back(result); |
206 } | 222 } |
207 return true; | 223 return true; |
208 } | 224 } |
OLD | NEW |