blob: 0ce8d2773a6a8d940d9a5b12c5e7d0648b995f24 [file] [log] [blame]
[email protected]65d988362012-02-03 11:20:071// 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
5#ifndef CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_
6#define CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_
[email protected]65d988362012-02-03 11:20:077
[email protected]5cc8fb22013-03-06 00:24:588#include <map>
[email protected]65d988362012-02-03 11:20:079#include <string>
10#include <vector>
11
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/memory/scoped_ptr.h"
[email protected]24a555b62013-06-10 22:01:1715#include "base/strings/string16.h"
[email protected]15fb2aa2012-05-22 22:52:5916#include "net/url_request/url_fetcher_delegate.h"
[email protected]65d988362012-02-03 11:20:0717
[email protected]80fe2b362012-05-02 04:19:3818class GURL;
[email protected]65d988362012-02-03 11:20:0719class TextCheckClientDelegate;
[email protected]7ed49c22012-02-17 02:14:5320struct SpellCheckResult;
[email protected]65d988362012-02-03 11:20:0721
[email protected]ee9cda5a2013-09-17 21:29:1422namespace content {
23class BrowserContext;
24}
25
[email protected]15fb2aa2012-05-22 22:52:5926namespace net {
27class URLFetcher;
28} // namespace net
29
[email protected]65d988362012-02-03 11:20:0730// 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]80fe2b362012-05-02 04:19:3845// bool success,
[email protected]7ed49c22012-02-17 02:14:5346// const std::vector<SpellCheckResult>& results) {
[email protected]65d988362012-02-03 11:20:0747// ...
48// }
49//
[email protected]a04db822013-12-11 19:14:4050// void MyTextCheck(BrowserContext* context, const base::string16& text) {
[email protected]65d988362012-02-03 11:20:0751// client_.reset(new SpellingServiceClient);
[email protected]ee9cda5a2013-09-17 21:29:1452// client_->RequestTextCheck(context, 0, text,
[email protected]65d988362012-02-03 11:20:0753// base::Bind(&MyClient::OnTextCheckComplete,
54// base::Unretained(this));
55// }
56// private:
57// scoped_ptr<SpellingServiceClient> client_;
58// };
59//
[email protected]15fb2aa2012-05-22 22:52:5960class SpellingServiceClient : public net::URLFetcherDelegate {
[email protected]65d988362012-02-03 11:20:0761 public:
[email protected]182bdf02012-04-07 01:40:4162 // 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 };
[email protected]65d988362012-02-03 11:20:0772 typedef base::Callback<void(
[email protected]80fe2b362012-05-02 04:19:3873 bool /* success */,
[email protected]a04db822013-12-11 19:14:4074 const base::string16& /* text */,
[email protected]7ed49c22012-02-17 02:14:5375 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0776 TextCheckCompleteCallback;
77
78 SpellingServiceClient();
79 virtual ~SpellingServiceClient();
80
[email protected]65d988362012-02-03 11:20:0781 // Sends a text-check request to the Spelling service. When we send a request
82 // to the Spelling service successfully, this function returns true. (This
83 // does not mean the service finishes checking text successfully.) We will
84 // call |callback| when we receive a text-check response from the service.
[email protected]ee9cda5a2013-09-17 21:29:1485 bool RequestTextCheck(content::BrowserContext* context,
[email protected]182bdf02012-04-07 01:40:4186 ServiceType type,
[email protected]a04db822013-12-11 19:14:4087 const base::string16& text,
[email protected]65d988362012-02-03 11:20:0788 const TextCheckCompleteCallback& callback);
89
[email protected]ee9cda5a2013-09-17 21:29:1490 // Returns whether the specified service is available for the given context.
91 static bool IsAvailable(content::BrowserContext* context, ServiceType type);
[email protected]12cc36532012-06-14 05:38:0492
[email protected]427276ee2013-05-31 20:27:0393 protected:
94 // Parses a JSON-RPC response from the Spelling service.
95 bool ParseResponse(const std::string& data,
96 std::vector<SpellCheckResult>* results);
97
[email protected]65d988362012-02-03 11:20:0798 private:
[email protected]5cc8fb22013-03-06 00:24:5899 struct TextCheckCallbackData {
[email protected]a04db822013-12-11 19:14:40100 TextCheckCallbackData(TextCheckCompleteCallback callback,
101 base::string16 text);
[email protected]5cc8fb22013-03-06 00:24:58102 ~TextCheckCallbackData();
103
104 // The callback function to be called when we receive a response from the
105 // Spelling service and parse it.
106 TextCheckCompleteCallback callback;
107
108 // The text checked by the Spelling service.
[email protected]a04db822013-12-11 19:14:40109 base::string16 text;
[email protected]5cc8fb22013-03-06 00:24:58110 };
111
112 // net::URLFetcherDelegate implementation.
113 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
114
[email protected]80fe2b362012-05-02 04:19:38115 // Creates a URLFetcher object used for sending a JSON-RPC request. This
[email protected]5cc8fb22013-03-06 00:24:58116 // function is overridden by unit tests to prevent them from actually sending
[email protected]80fe2b362012-05-02 04:19:38117 // requests to the Spelling service.
[email protected]15fb2aa2012-05-22 22:52:59118 virtual net::URLFetcher* CreateURLFetcher(const GURL& url);
[email protected]80fe2b362012-05-02 04:19:38119
[email protected]65d988362012-02-03 11:20:07120 // The URLFetcher object used for sending a JSON-RPC request.
[email protected]5cc8fb22013-03-06 00:24:58121 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_;
[email protected]65d988362012-02-03 11:20:07122};
123
124#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_