blob: 6edeae9784650d04d5b19ed450365ce2873a8c79 [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 Profile;
20class TextCheckClientDelegate;
[email protected]7ed49c22012-02-17 02:14:5321struct SpellCheckResult;
[email protected]65d988362012-02-03 11:20:0722
[email protected]15fb2aa2012-05-22 22:52:5923namespace net {
24class URLFetcher;
25} // namespace net
26
[email protected]65d988362012-02-03 11:20:0727// A class that encapsulates a JSON-RPC call to the Spelling service to check
28// text there. This class creates a JSON-RPC request, sends the request to the
29// service with URLFetcher, parses a response from the service, and calls a
30// provided callback method. When a user deletes this object before it finishes
31// a JSON-RPC call, this class cancels the JSON-RPC call without calling the
32// callback method. A simple usage is creating a SpellingServiceClient and
33// calling its RequestTextCheck method as listed in the following snippet.
34//
35// class MyClient {
36// public:
37// MyClient();
38// virtual ~MyClient();
39//
40// void OnTextCheckComplete(
41// int tag,
[email protected]80fe2b362012-05-02 04:19:3842// bool success,
[email protected]7ed49c22012-02-17 02:14:5343// const std::vector<SpellCheckResult>& results) {
[email protected]65d988362012-02-03 11:20:0744// ...
45// }
46//
47// void MyTextCheck(Profile* profile, const string16& text) {
48// client_.reset(new SpellingServiceClient);
49// client_->RequestTextCheck(profile, 0, text,
50// base::Bind(&MyClient::OnTextCheckComplete,
51// base::Unretained(this));
52// }
53// private:
54// scoped_ptr<SpellingServiceClient> client_;
55// };
56//
[email protected]15fb2aa2012-05-22 22:52:5957class SpellingServiceClient : public net::URLFetcherDelegate {
[email protected]65d988362012-02-03 11:20:0758 public:
[email protected]182bdf02012-04-07 01:40:4159 // Service types provided by the Spelling service. The Spelling service
60 // consists of a couple of backends:
61 // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and;
62 // * SPELLCHECK: Spellchecking text (used by Google Docs).
63 // This type is used for choosing a backend when sending a JSON-RPC request to
64 // the service.
65 enum ServiceType {
66 SUGGEST = 1,
67 SPELLCHECK = 2,
68 };
[email protected]65d988362012-02-03 11:20:0769 typedef base::Callback<void(
[email protected]80fe2b362012-05-02 04:19:3870 bool /* success */,
[email protected]8ca27e632012-05-28 07:39:3571 const string16& /* text */,
[email protected]7ed49c22012-02-17 02:14:5372 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0773 TextCheckCompleteCallback;
74
75 SpellingServiceClient();
76 virtual ~SpellingServiceClient();
77
[email protected]65d988362012-02-03 11:20:0778 // Sends a text-check request to the Spelling service. When we send a request
79 // to the Spelling service successfully, this function returns true. (This
80 // does not mean the service finishes checking text successfully.) We will
81 // call |callback| when we receive a text-check response from the service.
82 bool RequestTextCheck(Profile* profile,
[email protected]182bdf02012-04-07 01:40:4183 ServiceType type,
[email protected]65d988362012-02-03 11:20:0784 const string16& text,
85 const TextCheckCompleteCallback& callback);
86
[email protected]12cc36532012-06-14 05:38:0487 // Returns whether the specified service is available for the given profile.
88 static bool IsAvailable(Profile* profile, ServiceType type);
89
[email protected]427276ee2013-05-31 20:27:0390 protected:
91 // Parses a JSON-RPC response from the Spelling service.
92 bool ParseResponse(const std::string& data,
93 std::vector<SpellCheckResult>* results);
94
[email protected]65d988362012-02-03 11:20:0795 private:
[email protected]5cc8fb22013-03-06 00:24:5896 struct TextCheckCallbackData {
97 TextCheckCallbackData(TextCheckCompleteCallback callback, string16 text);
98 ~TextCheckCallbackData();
99
100 // The callback function to be called when we receive a response from the
101 // Spelling service and parse it.
102 TextCheckCompleteCallback callback;
103
104 // The text checked by the Spelling service.
105 string16 text;
106 };
107
108 // net::URLFetcherDelegate implementation.
109 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
110
[email protected]80fe2b362012-05-02 04:19:38111 // Creates a URLFetcher object used for sending a JSON-RPC request. This
[email protected]5cc8fb22013-03-06 00:24:58112 // function is overridden by unit tests to prevent them from actually sending
[email protected]80fe2b362012-05-02 04:19:38113 // requests to the Spelling service.
[email protected]15fb2aa2012-05-22 22:52:59114 virtual net::URLFetcher* CreateURLFetcher(const GURL& url);
[email protected]80fe2b362012-05-02 04:19:38115
[email protected]65d988362012-02-03 11:20:07116 // The URLFetcher object used for sending a JSON-RPC request.
[email protected]5cc8fb22013-03-06 00:24:58117 std::map<const net::URLFetcher*, TextCheckCallbackData*> spellcheck_fetchers_;
[email protected]65d988362012-02-03 11:20:07118};
119
120#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_