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 <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 "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "components/data_use_measurement/core/data_use_user_data.h" | 21 #include "components/data_use_measurement/core/data_use_user_data.h" |
21 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.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 19 matching lines...) Expand all Loading... |
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 | 52 |
52 SpellingServiceClient::~SpellingServiceClient() { | 53 SpellingServiceClient::~SpellingServiceClient() { |
53 base::STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(), | |
54 spellcheck_fetchers_.end()); | |
55 } | 54 } |
56 | 55 |
57 bool SpellingServiceClient::RequestTextCheck( | 56 bool SpellingServiceClient::RequestTextCheck( |
58 content::BrowserContext* context, | 57 content::BrowserContext* context, |
59 ServiceType type, | 58 ServiceType type, |
60 const base::string16& text, | 59 const base::string16& text, |
61 const TextCheckCompleteCallback& callback) { | 60 const TextCheckCompleteCallback& callback) { |
62 DCHECK(type == SUGGEST || type == SPELLCHECK); | 61 DCHECK(type == SUGGEST || type == SPELLCHECK); |
63 if (!context || !IsAvailable(context, type)) { | 62 if (!context || !IsAvailable(context, type)) { |
64 callback.Run(false, text, std::vector<SpellCheckResult>()); | 63 callback.Run(false, text, std::vector<SpellCheckResult>()); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 GURL url = GURL(kSpellingServiceURL); | 109 GURL url = GURL(kSpellingServiceURL); |
111 net::URLFetcher* fetcher = CreateURLFetcher(url).release(); | 110 net::URLFetcher* fetcher = CreateURLFetcher(url).release(); |
112 data_use_measurement::DataUseUserData::AttachToFetcher( | 111 data_use_measurement::DataUseUserData::AttachToFetcher( |
113 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER); | 112 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER); |
114 fetcher->SetRequestContext( | 113 fetcher->SetRequestContext( |
115 content::BrowserContext::GetDefaultStoragePartition(context)-> | 114 content::BrowserContext::GetDefaultStoragePartition(context)-> |
116 GetURLRequestContext()); | 115 GetURLRequestContext()); |
117 fetcher->SetUploadData("application/json", request); | 116 fetcher->SetUploadData("application/json", request); |
118 fetcher->SetLoadFlags( | 117 fetcher->SetLoadFlags( |
119 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 118 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
120 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text); | 119 |
| 120 spellcheck_fetchers_[fetcher] = base::MakeUnique<TextCheckCallbackData>( |
| 121 base::WrapUnique(fetcher), callback, text); |
121 fetcher->Start(); | 122 fetcher->Start(); |
122 return true; | 123 return true; |
123 } | 124 } |
124 | 125 |
125 bool SpellingServiceClient::IsAvailable( | 126 bool SpellingServiceClient::IsAvailable( |
126 content::BrowserContext* context, | 127 content::BrowserContext* context, |
127 ServiceType type) { | 128 ServiceType type) { |
128 const PrefService* pref = user_prefs::UserPrefs::Get(context); | 129 const PrefService* pref = user_prefs::UserPrefs::Get(context); |
129 DCHECK(pref); | 130 DCHECK(pref); |
130 // If prefs don't allow spellchecking, if the context is off the record, or if | 131 // If prefs don't allow spellchecking, if the context is off the record, or if |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 return false; | 241 return false; |
241 } | 242 } |
242 SpellCheckResult result( | 243 SpellCheckResult result( |
243 SpellCheckResult::SPELLING, start, length, replacement); | 244 SpellCheckResult::SPELLING, start, length, replacement); |
244 results->push_back(result); | 245 results->push_back(result); |
245 } | 246 } |
246 return true; | 247 return true; |
247 } | 248 } |
248 | 249 |
249 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData( | 250 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData( |
| 251 std::unique_ptr<net::URLFetcher> fetcher, |
250 TextCheckCompleteCallback callback, | 252 TextCheckCompleteCallback callback, |
251 base::string16 text) | 253 base::string16 text) |
252 : callback(callback), | 254 : fetcher(std::move(fetcher)), callback(callback), text(text) {} |
253 text(text) { | |
254 } | |
255 | 255 |
256 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() { | 256 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() { |
257 } | 257 } |
258 | 258 |
259 void SpellingServiceClient::OnURLFetchComplete( | 259 void SpellingServiceClient::OnURLFetchComplete(const net::URLFetcher* source) { |
260 const net::URLFetcher* source) { | 260 DCHECK(base::ContainsKey(spellcheck_fetchers_, source)); |
261 DCHECK(spellcheck_fetchers_[source]); | 261 std::unique_ptr<TextCheckCallbackData> callback_data = |
262 std::unique_ptr<const net::URLFetcher> fetcher(source); | 262 std::move(spellcheck_fetchers_[source]); |
263 std::unique_ptr<TextCheckCallbackData> callback_data( | 263 spellcheck_fetchers_.erase(source); |
264 spellcheck_fetchers_[fetcher.get()]); | 264 |
265 bool success = false; | 265 bool success = false; |
266 std::vector<SpellCheckResult> results; | 266 std::vector<SpellCheckResult> results; |
267 if (fetcher->GetResponseCode() / 100 == 2) { | 267 if (source->GetResponseCode() / 100 == 2) { |
268 std::string data; | 268 std::string data; |
269 fetcher->GetResponseAsString(&data); | 269 source->GetResponseAsString(&data); |
270 success = ParseResponse(data, &results); | 270 success = ParseResponse(data, &results); |
271 } | 271 } |
272 spellcheck_fetchers_.erase(fetcher.get()); | |
273 | 272 |
274 // The callback may release the last (transitive) dependency on |this|. It | 273 // The callback may release the last (transitive) dependency on |this|. It |
275 // MUST be the last function called. | 274 // MUST be the last function called. |
276 callback_data->callback.Run(success, callback_data->text, results); | 275 callback_data->callback.Run(success, callback_data->text, results); |
277 } | 276 } |
278 | 277 |
279 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher( | 278 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher( |
280 const GURL& url) { | 279 const GURL& url) { |
281 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); | 280 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); |
282 } | 281 } |
OLD | NEW |