| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "google_apis/google_api_keys.h" | 21 #include "google_apis/google_api_keys.h" |
| 22 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
| 23 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
| 24 #include "unicode/uloc.h" | 24 #include "unicode/uloc.h" |
| 25 | 25 |
| 26 // Use the public URL to the Spelling service on Chromium. | 26 // Use the public URL to the Spelling service on Chromium. |
| 27 #ifndef SPELLING_SERVICE_URL | 27 #ifndef SPELLING_SERVICE_URL |
| 28 #define SPELLING_SERVICE_URL "https://www.googleapis.com/rpc" | 28 #define SPELLING_SERVICE_URL "https://www.googleapis.com/rpc" |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 SpellingServiceClient::SpellingServiceClient() : tag_(0) { | 31 SpellingServiceClient::SpellingServiceClient() { |
| 32 } | 32 } |
| 33 | 33 |
| 34 SpellingServiceClient::~SpellingServiceClient() { | 34 SpellingServiceClient::~SpellingServiceClient() { |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool SpellingServiceClient::RequestTextCheck( | 37 bool SpellingServiceClient::RequestTextCheck( |
| 38 Profile* profile, | 38 Profile* profile, |
| 39 int tag, | |
| 40 ServiceType type, | 39 ServiceType type, |
| 41 const string16& text, | 40 const string16& text, |
| 42 const TextCheckCompleteCallback& callback) { | 41 const TextCheckCompleteCallback& callback) { |
| 43 DCHECK(type == SUGGEST || type == SPELLCHECK); | 42 DCHECK(type == SUGGEST || type == SPELLCHECK); |
| 44 std::string locale = profile->GetPrefs()->GetString( | 43 std::string locale = profile->GetPrefs()->GetString( |
| 45 prefs::kSpellCheckDictionary); | 44 prefs::kSpellCheckDictionary); |
| 46 char language[ULOC_LANG_CAPACITY] = ULOC_ENGLISH; | 45 char language[ULOC_LANG_CAPACITY] = ULOC_ENGLISH; |
| 47 const char* country = "USA"; | 46 const char* country = "USA"; |
| 48 if (!locale.empty()) { | 47 if (!locale.empty()) { |
| 49 // Create the parameters needed by Spelling API. Spelling API needs three | 48 // Create the parameters needed by Spelling API. Spelling API needs three |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 api_key.c_str()); | 89 api_key.c_str()); |
| 91 | 90 |
| 92 static const char kSpellingServiceURL[] = SPELLING_SERVICE_URL; | 91 static const char kSpellingServiceURL[] = SPELLING_SERVICE_URL; |
| 93 GURL url = GURL(kSpellingServiceURL); | 92 GURL url = GURL(kSpellingServiceURL); |
| 94 fetcher_.reset(CreateURLFetcher(url)); | 93 fetcher_.reset(CreateURLFetcher(url)); |
| 95 fetcher_->SetRequestContext(profile->GetRequestContext()); | 94 fetcher_->SetRequestContext(profile->GetRequestContext()); |
| 96 fetcher_->SetUploadData("application/json", request); | 95 fetcher_->SetUploadData("application/json", request); |
| 97 fetcher_->SetLoadFlags( | 96 fetcher_->SetLoadFlags( |
| 98 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 97 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
| 99 fetcher_->Start(); | 98 fetcher_->Start(); |
| 100 tag_ = tag; | |
| 101 text_ = text; | 99 text_ = text; |
| 102 callback_ = callback; | 100 callback_ = callback; |
| 103 return true; | 101 return true; |
| 104 } | 102 } |
| 105 | 103 |
| 106 bool SpellingServiceClient::IsAvailable(Profile* profile, ServiceType type) { | 104 bool SpellingServiceClient::IsAvailable(Profile* profile, ServiceType type) { |
| 107 const PrefService* pref = profile->GetPrefs(); | 105 const PrefService* pref = profile->GetPrefs(); |
| 108 if (!pref->GetBoolean(prefs::kEnableContinuousSpellcheck) || | 106 if (!pref->GetBoolean(prefs::kEnableContinuousSpellcheck) || |
| 109 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) | 107 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService)) |
| 110 return false; | 108 return false; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 139 void SpellingServiceClient::OnURLFetchComplete( | 137 void SpellingServiceClient::OnURLFetchComplete( |
| 140 const net::URLFetcher* source) { | 138 const net::URLFetcher* source) { |
| 141 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); | 139 scoped_ptr<net::URLFetcher> clean_up_fetcher(fetcher_.release()); |
| 142 bool success = false; | 140 bool success = false; |
| 143 std::vector<SpellCheckResult> results; | 141 std::vector<SpellCheckResult> results; |
| 144 if (source->GetResponseCode() / 100 == 2) { | 142 if (source->GetResponseCode() / 100 == 2) { |
| 145 std::string data; | 143 std::string data; |
| 146 source->GetResponseAsString(&data); | 144 source->GetResponseAsString(&data); |
| 147 success = ParseResponse(data, &results); | 145 success = ParseResponse(data, &results); |
| 148 } | 146 } |
| 149 callback_.Run(tag_, success, text_, results); | 147 callback_.Run(success, text_, results); |
| 150 } | 148 } |
| 151 | 149 |
| 152 net::URLFetcher* SpellingServiceClient::CreateURLFetcher(const GURL& url) { | 150 net::URLFetcher* SpellingServiceClient::CreateURLFetcher(const GURL& url) { |
| 153 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); | 151 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); |
| 154 } | 152 } |
| 155 | 153 |
| 156 bool SpellingServiceClient::ParseResponse( | 154 bool SpellingServiceClient::ParseResponse( |
| 157 const std::string& data, | 155 const std::string& data, |
| 158 std::vector<SpellCheckResult>* results) { | 156 std::vector<SpellCheckResult>* results) { |
| 159 // When this JSON-RPC call finishes successfully, the Spelling service returns | 157 // When this JSON-RPC call finishes successfully, the Spelling service returns |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 if (!suggestions->GetDictionary(0, &suggestion) || | 221 if (!suggestions->GetDictionary(0, &suggestion) || |
| 224 !suggestion->GetString("suggestion", &replacement)) { | 222 !suggestion->GetString("suggestion", &replacement)) { |
| 225 return false; | 223 return false; |
| 226 } | 224 } |
| 227 SpellCheckResult result( | 225 SpellCheckResult result( |
| 228 SpellCheckResult::SPELLING, start, length, replacement); | 226 SpellCheckResult::SPELLING, start, length, replacement); |
| 229 results->push_back(result); | 227 results->push_back(result); |
| 230 } | 228 } |
| 231 return true; | 229 return true; |
| 232 } | 230 } |
| OLD | NEW |