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 "components/spellcheck/browser/spelling_service_client.h" | 5 #include "components/spellcheck/browser/spelling_service_client.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/string_escape.h" | 12 #include "base/json/string_escape.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" |
14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "components/data_use_measurement/core/data_use_user_data.h" | 20 #include "components/data_use_measurement/core/data_use_user_data.h" |
20 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
21 #include "components/spellcheck/browser/pref_names.h" | 22 #include "components/spellcheck/browser/pref_names.h" |
22 #include "components/spellcheck/common/spellcheck_common.h" | 23 #include "components/spellcheck/common/spellcheck_common.h" |
23 #include "components/spellcheck/common/spellcheck_result.h" | 24 #include "components/spellcheck/common/spellcheck_result.h" |
(...skipping 17 matching lines...) Expand all Loading... |
41 // The location of error messages in JSON response from spelling service. | 42 // The location of error messages in JSON response from spelling service. |
42 const char kErrorPath[] = "error"; | 43 const char kErrorPath[] = "error"; |
43 | 44 |
44 // Languages currently supported by SPELLCHECK. | 45 // Languages currently supported by SPELLCHECK. |
45 const char* const kValidLanguages[] = {"en", "es", "fi", "da"}; | 46 const char* const kValidLanguages[] = {"en", "es", "fi", "da"}; |
46 | 47 |
47 } // namespace | 48 } // namespace |
48 | 49 |
49 SpellingServiceClient::SpellingServiceClient() {} | 50 SpellingServiceClient::SpellingServiceClient() {} |
50 | 51 |
51 SpellingServiceClient::~SpellingServiceClient() { | 52 SpellingServiceClient::~SpellingServiceClient() {} |
52 base::STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(), | |
53 spellcheck_fetchers_.end()); | |
54 } | |
55 | 53 |
56 bool SpellingServiceClient::RequestTextCheck( | 54 bool SpellingServiceClient::RequestTextCheck( |
57 content::BrowserContext* context, | 55 content::BrowserContext* context, |
58 ServiceType type, | 56 ServiceType type, |
59 const base::string16& text, | 57 const base::string16& text, |
60 const TextCheckCompleteCallback& callback) { | 58 const TextCheckCompleteCallback& callback) { |
61 DCHECK(type == SUGGEST || type == SPELLCHECK); | 59 DCHECK(type == SUGGEST || type == SPELLCHECK); |
62 if (!context || !IsAvailable(context, type)) { | 60 if (!context || !IsAvailable(context, type)) { |
63 callback.Run(false, text, std::vector<SpellCheckResult>()); | 61 callback.Run(false, text, std::vector<SpellCheckResult>()); |
64 return false; | 62 return false; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 GURL url = GURL(kSpellingServiceURL); | 104 GURL url = GURL(kSpellingServiceURL); |
107 net::URLFetcher* fetcher = CreateURLFetcher(url).release(); | 105 net::URLFetcher* fetcher = CreateURLFetcher(url).release(); |
108 data_use_measurement::DataUseUserData::AttachToFetcher( | 106 data_use_measurement::DataUseUserData::AttachToFetcher( |
109 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER); | 107 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER); |
110 fetcher->SetRequestContext( | 108 fetcher->SetRequestContext( |
111 content::BrowserContext::GetDefaultStoragePartition(context) | 109 content::BrowserContext::GetDefaultStoragePartition(context) |
112 ->GetURLRequestContext()); | 110 ->GetURLRequestContext()); |
113 fetcher->SetUploadData("application/json", request); | 111 fetcher->SetUploadData("application/json", request); |
114 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 112 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
115 net::LOAD_DO_NOT_SAVE_COOKIES); | 113 net::LOAD_DO_NOT_SAVE_COOKIES); |
116 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text); | 114 spellcheck_fetchers_[fetcher] = base::MakeUnique<TextCheckCallbackData>( |
| 115 base::WrapUnique(fetcher), callback, text); |
117 fetcher->Start(); | 116 fetcher->Start(); |
118 return true; | 117 return true; |
119 } | 118 } |
120 | 119 |
121 bool SpellingServiceClient::IsAvailable(content::BrowserContext* context, | 120 bool SpellingServiceClient::IsAvailable(content::BrowserContext* context, |
122 ServiceType type) { | 121 ServiceType type) { |
123 const PrefService* pref = user_prefs::UserPrefs::Get(context); | 122 const PrefService* pref = user_prefs::UserPrefs::Get(context); |
124 DCHECK(pref); | 123 DCHECK(pref); |
125 // If prefs don't allow spellchecking, if the context is off the record, or if | 124 // If prefs don't allow spellchecking, if the context is off the record, or if |
126 // multilingual spellchecking is enabled the spelling service should be | 125 // multilingual spellchecking is enabled the spelling service should be |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 return false; | 235 return false; |
237 } | 236 } |
238 SpellCheckResult result(SpellCheckResult::SPELLING, start, length, | 237 SpellCheckResult result(SpellCheckResult::SPELLING, start, length, |
239 replacement); | 238 replacement); |
240 results->push_back(result); | 239 results->push_back(result); |
241 } | 240 } |
242 return true; | 241 return true; |
243 } | 242 } |
244 | 243 |
245 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData( | 244 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData( |
| 245 std::unique_ptr<net::URLFetcher> fetcher, |
246 TextCheckCompleteCallback callback, | 246 TextCheckCompleteCallback callback, |
247 base::string16 text) | 247 base::string16 text) |
248 : callback(callback), text(text) {} | 248 : fetcher(std::move(fetcher)), callback(callback), text(text) {} |
249 | 249 |
250 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() {} | 250 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() {} |
251 | 251 |
252 void SpellingServiceClient::OnURLFetchComplete(const net::URLFetcher* source) { | 252 void SpellingServiceClient::OnURLFetchComplete(const net::URLFetcher* source) { |
253 DCHECK(spellcheck_fetchers_[source]); | 253 DCHECK(base::ContainsKey(spellcheck_fetchers_, source)); |
254 std::unique_ptr<const net::URLFetcher> fetcher(source); | 254 std::unique_ptr<TextCheckCallbackData> callback_data = |
255 std::unique_ptr<TextCheckCallbackData> callback_data( | 255 std::move(spellcheck_fetchers_[source]); |
256 spellcheck_fetchers_[fetcher.get()]); | 256 spellcheck_fetchers_.erase(source); |
| 257 |
257 bool success = false; | 258 bool success = false; |
258 std::vector<SpellCheckResult> results; | 259 std::vector<SpellCheckResult> results; |
259 if (fetcher->GetResponseCode() / 100 == 2) { | 260 if (source->GetResponseCode() / 100 == 2) { |
260 std::string data; | 261 std::string data; |
261 fetcher->GetResponseAsString(&data); | 262 source->GetResponseAsString(&data); |
262 success = ParseResponse(data, &results); | 263 success = ParseResponse(data, &results); |
263 } | 264 } |
264 spellcheck_fetchers_.erase(fetcher.get()); | |
265 | 265 |
266 // The callback may release the last (transitive) dependency on |this|. It | 266 // The callback may release the last (transitive) dependency on |this|. It |
267 // MUST be the last function called. | 267 // MUST be the last function called. |
268 callback_data->callback.Run(success, callback_data->text, results); | 268 callback_data->callback.Run(success, callback_data->text, results); |
269 } | 269 } |
270 | 270 |
271 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher( | 271 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher( |
272 const GURL& url) { | 272 const GURL& url) { |
273 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); | 273 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); |
274 } | 274 } |
OLD | NEW |