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" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
20 #include "chrome/common/spellcheck_common.h" | 19 #include "chrome/common/spellcheck_common.h" |
21 #include "chrome/common/spellcheck_result.h" | 20 #include "chrome/common/spellcheck_result.h" |
| 21 #include "components/user_prefs/user_prefs.h" |
| 22 #include "content/public/browser/browser_context.h" |
22 #include "google_apis/google_api_keys.h" | 23 #include "google_apis/google_api_keys.h" |
23 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
24 #include "net/url_request/url_fetcher.h" | 25 #include "net/url_request/url_fetcher.h" |
| 26 #include "url/gurl.h" |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 // The URL for requesting spell checking and sending user feedback. | 30 // The URL for requesting spell checking and sending user feedback. |
29 const char kSpellingServiceURL[] = "https://www.googleapis.com/rpc"; | 31 const char kSpellingServiceURL[] = "https://www.googleapis.com/rpc"; |
30 | 32 |
31 // The location of spellcheck suggestions in JSON response from spelling | 33 // The location of spellcheck suggestions in JSON response from spelling |
32 // service. | 34 // service. |
33 const char kMisspellingsPath[] = "result.spellingCheckResponse.misspellings"; | 35 const char kMisspellingsPath[] = "result.spellingCheckResponse.misspellings"; |
34 | 36 |
35 // The location of error messages in JSON response from spelling service. | 37 // The location of error messages in JSON response from spelling service. |
36 const char kErrorPath[] = "error"; | 38 const char kErrorPath[] = "error"; |
37 | 39 |
38 } // namespace | 40 } // namespace |
39 | 41 |
40 SpellingServiceClient::SpellingServiceClient() { | 42 SpellingServiceClient::SpellingServiceClient() { |
41 } | 43 } |
42 | 44 |
43 SpellingServiceClient::~SpellingServiceClient() { | 45 SpellingServiceClient::~SpellingServiceClient() { |
44 STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(), | 46 STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(), |
45 spellcheck_fetchers_.end()); | 47 spellcheck_fetchers_.end()); |
46 } | 48 } |
47 | 49 |
48 bool SpellingServiceClient::RequestTextCheck( | 50 bool SpellingServiceClient::RequestTextCheck( |
49 Profile* profile, | 51 content::BrowserContext* context, |
50 ServiceType type, | 52 ServiceType type, |
51 const string16& text, | 53 const string16& text, |
52 const TextCheckCompleteCallback& callback) { | 54 const TextCheckCompleteCallback& callback) { |
53 DCHECK(type == SUGGEST || type == SPELLCHECK); | 55 DCHECK(type == SUGGEST || type == SPELLCHECK); |
54 if (!profile || !IsAvailable(profile, type)) { | 56 if (!context || !IsAvailable(context, type)) { |
55 callback.Run(false, text, std::vector<SpellCheckResult>()); | 57 callback.Run(false, text, std::vector<SpellCheckResult>()); |
56 return false; | 58 return false; |
57 } | 59 } |
58 | 60 |
| 61 const PrefService* pref = user_prefs::UserPrefs::Get(context); |
| 62 DCHECK(pref); |
| 63 |
59 std::string language_code; | 64 std::string language_code; |
60 std::string country_code; | 65 std::string country_code; |
61 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( | 66 chrome::spellcheck_common::GetISOLanguageCountryCodeFromLocale( |
62 profile->GetPrefs()->GetString(prefs::kSpellCheckDictionary), | 67 pref->GetString(prefs::kSpellCheckDictionary), |
63 &language_code, | 68 &language_code, |
64 &country_code); | 69 &country_code); |
65 | 70 |
66 // Format the JSON request to be sent to the Spelling service. | 71 // Format the JSON request to be sent to the Spelling service. |
67 std::string encoded_text; | 72 std::string encoded_text; |
68 base::JsonDoubleQuote(text, false, &encoded_text); | 73 base::JsonDoubleQuote(text, false, &encoded_text); |
69 | 74 |
70 static const char kSpellingRequest[] = | 75 static const char kSpellingRequest[] = |
71 "{" | 76 "{" |
72 "\"method\":\"spelling.check\"," | 77 "\"method\":\"spelling.check\"," |
73 "\"apiVersion\":\"v%d\"," | 78 "\"apiVersion\":\"v%d\"," |
74 "\"params\":{" | 79 "\"params\":{" |
75 "\"text\":\"%s\"," | 80 "\"text\":\"%s\"," |
76 "\"language\":\"%s\"," | 81 "\"language\":\"%s\"," |
77 "\"originCountry\":\"%s\"," | 82 "\"originCountry\":\"%s\"," |
78 "\"key\":%s" | 83 "\"key\":%s" |
79 "}" | 84 "}" |
80 "}"; | 85 "}"; |
81 std::string api_key = base::GetDoubleQuotedJson(google_apis::GetAPIKey()); | 86 std::string api_key = base::GetDoubleQuotedJson(google_apis::GetAPIKey()); |
82 std::string request = base::StringPrintf( | 87 std::string request = base::StringPrintf( |
83 kSpellingRequest, | 88 kSpellingRequest, |
84 type, | 89 type, |
85 encoded_text.c_str(), | 90 encoded_text.c_str(), |
86 language_code.c_str(), | 91 language_code.c_str(), |
87 country_code.c_str(), | 92 country_code.c_str(), |
88 api_key.c_str()); | 93 api_key.c_str()); |
89 | 94 |
90 GURL url = GURL(kSpellingServiceURL); | 95 GURL url = GURL(kSpellingServiceURL); |
91 net::URLFetcher* fetcher = CreateURLFetcher(url); | 96 net::URLFetcher* fetcher = CreateURLFetcher(url); |
92 fetcher->SetRequestContext(profile->GetRequestContext()); | 97 fetcher->SetRequestContext(context->GetRequestContext()); |
93 fetcher->SetUploadData("application/json", request); | 98 fetcher->SetUploadData("application/json", request); |
94 fetcher->SetLoadFlags( | 99 fetcher->SetLoadFlags( |
95 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | 100 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
96 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text); | 101 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text); |
97 fetcher->Start(); | 102 fetcher->Start(); |
98 return true; | 103 return true; |
99 } | 104 } |
100 | 105 |
101 bool SpellingServiceClient::IsAvailable(Profile* profile, ServiceType type) { | 106 bool SpellingServiceClient::IsAvailable( |
102 const PrefService* pref = profile->GetPrefs(); | 107 content::BrowserContext* context, |
103 // If prefs don't allow spellchecking or if the profile is off the record, | 108 ServiceType type) { |
| 109 const PrefService* pref = user_prefs::UserPrefs::Get(context); |
| 110 DCHECK(pref); |
| 111 // If prefs don't allow spellchecking or if the context is off the record, |
104 // the spelling service should be unavailable. | 112 // the spelling service should be unavailable. |
105 if (!pref->GetBoolean(prefs::kEnableContinuousSpellcheck) || | 113 if (!pref->GetBoolean(prefs::kEnableContinuousSpellcheck) || |
106 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService) || | 114 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService) || |
107 profile->IsOffTheRecord()) | 115 context->IsOffTheRecord()) |
108 return false; | 116 return false; |
109 | 117 |
110 // If the locale for spelling has not been set, the user has not decided to | 118 // If the locale for spelling has not been set, the user has not decided to |
111 // use spellcheck so we don't do anything remote (suggest or spelling). | 119 // use spellcheck so we don't do anything remote (suggest or spelling). |
112 std::string locale = pref->GetString(prefs::kSpellCheckDictionary); | 120 std::string locale = pref->GetString(prefs::kSpellCheckDictionary); |
113 if (locale.empty()) | 121 if (locale.empty()) |
114 return false; | 122 return false; |
115 | 123 |
116 // If we do not have the spelling service enabled, then we are only available | 124 // If we do not have the spelling service enabled, then we are only available |
117 // for SUGGEST. | 125 // for SUGGEST. |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 fetcher->GetResponseAsString(&data); | 255 fetcher->GetResponseAsString(&data); |
248 success = ParseResponse(data, &results); | 256 success = ParseResponse(data, &results); |
249 } | 257 } |
250 callback_data->callback.Run(success, callback_data->text, results); | 258 callback_data->callback.Run(success, callback_data->text, results); |
251 spellcheck_fetchers_.erase(fetcher.get()); | 259 spellcheck_fetchers_.erase(fetcher.get()); |
252 } | 260 } |
253 | 261 |
254 net::URLFetcher* SpellingServiceClient::CreateURLFetcher(const GURL& url) { | 262 net::URLFetcher* SpellingServiceClient::CreateURLFetcher(const GURL& url) { |
255 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); | 263 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); |
256 } | 264 } |
OLD | NEW |