[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
timvolodine | 0eec9ec | 2016-08-17 16:18:48 | [diff] [blame] | 5 | #ifndef COMPONENTS_SPELLCHECK_BROWSER_SPELLING_SERVICE_CLIENT_H_ |
| 6 | #define COMPONENTS_SPELLCHECK_BROWSER_SPELLING_SERVICE_CLIENT_H_ |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 7 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 8 | #include <list> |
dcheng | 4d0d72ef | 2016-04-14 01:11:33 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/callback.h" |
| 14 | #include "base/compiler_specific.h" |
[email protected] | 24a555b6 | 2013-06-10 22:01:17 | [diff] [blame] | 15 | #include "base/strings/string16.h" |
rhalavati | a9b551d | 2017-02-09 12:03:00 | [diff] [blame] | 16 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Guillaume Jenkins | a51cc240 | 2019-04-04 15:26:37 | [diff] [blame] | 17 | #include "url/gurl.h" |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 18 | |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 19 | struct SpellCheckResult; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 20 | |
[email protected] | ee9cda5a | 2013-09-17 21:29:14 | [diff] [blame] | 21 | namespace content { |
| 22 | class BrowserContext; |
| 23 | } |
| 24 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 25 | namespace network { |
| 26 | class SharedURLLoaderFactory; |
| 27 | class SimpleURLLoader; |
| 28 | } // namespace network |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 29 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 30 | // A class that encapsulates a JSON-RPC call to the Spelling service to check |
| 31 | // text there. This class creates a JSON-RPC request, sends the request to the |
| 32 | // service with URLFetcher, parses a response from the service, and calls a |
| 33 | // provided callback method. When a user deletes this object before it finishes |
| 34 | // a JSON-RPC call, this class cancels the JSON-RPC call without calling the |
| 35 | // callback method. A simple usage is creating a SpellingServiceClient and |
| 36 | // calling its RequestTextCheck method as listed in the following snippet. |
| 37 | // |
| 38 | // class MyClient { |
| 39 | // public: |
| 40 | // MyClient(); |
| 41 | // virtual ~MyClient(); |
| 42 | // |
| 43 | // void OnTextCheckComplete( |
| 44 | // int tag, |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 45 | // bool success, |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 46 | // const std::vector<SpellCheckResult>& results) { |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 47 | // ... |
| 48 | // } |
| 49 | // |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 50 | // void MyTextCheck(BrowserContext* context, const base::string16& text) { |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 51 | // client_.reset(new SpellingServiceClient); |
[email protected] | ee9cda5a | 2013-09-17 21:29:14 | [diff] [blame] | 52 | // client_->RequestTextCheck(context, 0, text, |
Xiaocheng Hu | 3e37c3a | 2018-01-10 19:54:41 | [diff] [blame] | 53 | // base::BindOnce(&MyClient::OnTextCheckComplete, |
| 54 | // base::Unretained(this)); |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 55 | // } |
| 56 | // private: |
dcheng | 4d0d72ef | 2016-04-14 01:11:33 | [diff] [blame] | 57 | // std::unique_ptr<SpellingServiceClient> client_; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 58 | // }; |
| 59 | // |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 60 | class SpellingServiceClient { |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 61 | public: |
[email protected] | 182bdf0 | 2012-04-07 01:40:41 | [diff] [blame] | 62 | // Service types provided by the Spelling service. The Spelling service |
| 63 | // consists of a couple of backends: |
| 64 | // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and; |
| 65 | // * SPELLCHECK: Spellchecking text (used by Google Docs). |
| 66 | // This type is used for choosing a backend when sending a JSON-RPC request to |
| 67 | // the service. |
| 68 | enum ServiceType { |
| 69 | SUGGEST = 1, |
| 70 | SPELLCHECK = 2, |
| 71 | }; |
Guillaume Jenkins | 7f34b92 | 2019-03-26 14:32:05 | [diff] [blame] | 72 | // An enum to classify request responses. This is only used for metrics. |
| 73 | // * REQUEST_FAILURE: The server returned an error. |
| 74 | // * SUCCESS_EMPTY: The server returned an empty list of suggestions. |
| 75 | // * SUCCESS_WITH_SUGGESTIONS: The server returned some suggestions. |
| 76 | // These values are persisted to logs. Entries should not be renumbered and |
| 77 | // numeric values should never be reused. |
| 78 | enum class ServiceRequestResultType : int { |
| 79 | kRequestFailure = 0, |
| 80 | kSuccessEmpty = 1, |
| 81 | kSuccessWithSuggestions = 2, |
| 82 | kMaxValue = kSuccessWithSuggestions, |
| 83 | }; |
Xiaocheng Hu | 3e37c3a | 2018-01-10 19:54:41 | [diff] [blame] | 84 | typedef base::OnceCallback<void( |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 85 | bool /* success */, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 86 | const base::string16& /* text */, |
[email protected] | 7ed49c2 | 2012-02-17 02:14:53 | [diff] [blame] | 87 | const std::vector<SpellCheckResult>& /* results */)> |
Xiaocheng Hu | 3e37c3a | 2018-01-10 19:54:41 | [diff] [blame] | 88 | TextCheckCompleteCallback; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 89 | |
| 90 | SpellingServiceClient(); |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 91 | ~SpellingServiceClient(); |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 92 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 93 | // Sends a text-check request to the Spelling service. When we send a request |
| 94 | // to the Spelling service successfully, this function returns true. (This |
| 95 | // does not mean the service finishes checking text successfully.) We will |
| 96 | // call |callback| when we receive a text-check response from the service. |
[email protected] | ee9cda5a | 2013-09-17 21:29:14 | [diff] [blame] | 97 | bool RequestTextCheck(content::BrowserContext* context, |
[email protected] | 182bdf0 | 2012-04-07 01:40:41 | [diff] [blame] | 98 | ServiceType type, |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 99 | const base::string16& text, |
Xiaocheng Hu | 3e37c3a | 2018-01-10 19:54:41 | [diff] [blame] | 100 | TextCheckCompleteCallback callback); |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 101 | |
[email protected] | ee9cda5a | 2013-09-17 21:29:14 | [diff] [blame] | 102 | // Returns whether the specified service is available for the given context. |
| 103 | static bool IsAvailable(content::BrowserContext* context, ServiceType type); |
[email protected] | 12cc3653 | 2012-06-14 05:38:04 | [diff] [blame] | 104 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 105 | // Set the URL loader factory for tests. |
| 106 | void SetURLLoaderFactoryForTesting( |
| 107 | scoped_refptr<network::SharedURLLoaderFactory> |
| 108 | url_loader_factory_for_testing); |
| 109 | |
Guillaume Jenkins | a51cc240 | 2019-04-04 15:26:37 | [diff] [blame] | 110 | // Builds the endpoint URL to use for the service request. |
| 111 | GURL BuildEndpointUrl(int type); |
| 112 | |
[email protected] | 427276ee | 2013-05-31 20:27:03 | [diff] [blame] | 113 | protected: |
| 114 | // Parses a JSON-RPC response from the Spelling service. |
| 115 | bool ParseResponse(const std::string& data, |
| 116 | std::vector<SpellCheckResult>* results); |
| 117 | |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 118 | private: |
[email protected] | 5cc8fb2 | 2013-03-06 00:24:58 | [diff] [blame] | 119 | struct TextCheckCallbackData { |
Xiaocheng Hu | ef2b9b64 | 2018-02-09 20:29:49 | [diff] [blame] | 120 | public: |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 121 | TextCheckCallbackData( |
| 122 | std::unique_ptr<network::SimpleURLLoader> simple_url_loader, |
| 123 | TextCheckCompleteCallback callback, |
| 124 | base::string16 text); |
[email protected] | 5cc8fb2 | 2013-03-06 00:24:58 | [diff] [blame] | 125 | ~TextCheckCallbackData(); |
| 126 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 127 | // The URL loader used. |
| 128 | std::unique_ptr<network::SimpleURLLoader> simple_url_loader; |
avi | d99bd1a | 2016-08-17 22:44:06 | [diff] [blame] | 129 | |
[email protected] | 5cc8fb2 | 2013-03-06 00:24:58 | [diff] [blame] | 130 | // The callback function to be called when we receive a response from the |
| 131 | // Spelling service and parse it. |
| 132 | TextCheckCompleteCallback callback; |
| 133 | |
| 134 | // The text checked by the Spelling service. |
[email protected] | a04db82 | 2013-12-11 19:14:40 | [diff] [blame] | 135 | base::string16 text; |
Xiaocheng Hu | ef2b9b64 | 2018-02-09 20:29:49 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | DISALLOW_COPY_AND_ASSIGN(TextCheckCallbackData); |
[email protected] | 5cc8fb2 | 2013-03-06 00:24:58 | [diff] [blame] | 139 | }; |
| 140 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 141 | using SpellCheckLoaderList = |
| 142 | std::list<std::unique_ptr<TextCheckCallbackData>>; |
[email protected] | 5cc8fb2 | 2013-03-06 00:24:58 | [diff] [blame] | 143 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 144 | void OnSimpleLoaderComplete(SpellCheckLoaderList::iterator it, |
Guillaume Jenkins | 7f34b92 | 2019-03-26 14:32:05 | [diff] [blame] | 145 | base::TimeTicks request_start, |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 146 | std::unique_ptr<std::string> response_body); |
[email protected] | 80fe2b36 | 2012-05-02 04:19:38 | [diff] [blame] | 147 | |
Mark Pilgrim | 6f6e54e8 | 2018-06-14 17:30:45 | [diff] [blame] | 148 | // List of loaders in use. |
| 149 | SpellCheckLoaderList spellcheck_loaders_; |
| 150 | |
| 151 | // URL loader factory to use for fake network requests during testing. |
| 152 | scoped_refptr<network::SharedURLLoaderFactory> |
| 153 | url_loader_factory_for_testing_; |
[email protected] | 65d98836 | 2012-02-03 11:20:07 | [diff] [blame] | 154 | }; |
| 155 | |
timvolodine | 0eec9ec | 2016-08-17 16:18:48 | [diff] [blame] | 156 | #endif // COMPONENTS_SPELLCHECK_BROWSER_SPELLING_SERVICE_CLIENT_H_ |