blob: 5b55b573b6d75ca48ed711f008e3a6d8e164664b [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
8#include <string>
9#include <vector>
10
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/string16.h"
[email protected]15fb2aa2012-05-22 22:52:5915#include "net/url_request/url_fetcher_delegate.h"
[email protected]65d988362012-02-03 11:20:0716
[email protected]80fe2b362012-05-02 04:19:3817class GURL;
[email protected]65d988362012-02-03 11:20:0718class Profile;
19class TextCheckClientDelegate;
[email protected]7ed49c22012-02-17 02:14:5320struct SpellCheckResult;
[email protected]65d988362012-02-03 11:20:0721
[email protected]15fb2aa2012-05-22 22:52:5922namespace net {
23class URLFetcher;
24} // namespace net
25
[email protected]65d988362012-02-03 11:20:0726// A class that encapsulates a JSON-RPC call to the Spelling service to check
27// text there. This class creates a JSON-RPC request, sends the request to the
28// service with URLFetcher, parses a response from the service, and calls a
29// provided callback method. When a user deletes this object before it finishes
30// a JSON-RPC call, this class cancels the JSON-RPC call without calling the
31// callback method. A simple usage is creating a SpellingServiceClient and
32// calling its RequestTextCheck method as listed in the following snippet.
33//
34// class MyClient {
35// public:
36// MyClient();
37// virtual ~MyClient();
38//
39// void OnTextCheckComplete(
40// int tag,
[email protected]80fe2b362012-05-02 04:19:3841// bool success,
[email protected]7ed49c22012-02-17 02:14:5342// const std::vector<SpellCheckResult>& results) {
[email protected]65d988362012-02-03 11:20:0743// ...
44// }
45//
46// void MyTextCheck(Profile* profile, const string16& text) {
47// client_.reset(new SpellingServiceClient);
48// client_->RequestTextCheck(profile, 0, text,
49// base::Bind(&MyClient::OnTextCheckComplete,
50// base::Unretained(this));
51// }
52// private:
53// scoped_ptr<SpellingServiceClient> client_;
54// };
55//
[email protected]15fb2aa2012-05-22 22:52:5956class SpellingServiceClient : public net::URLFetcherDelegate {
[email protected]65d988362012-02-03 11:20:0757 public:
[email protected]182bdf02012-04-07 01:40:4158 // Service types provided by the Spelling service. The Spelling service
59 // consists of a couple of backends:
60 // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and;
61 // * SPELLCHECK: Spellchecking text (used by Google Docs).
62 // This type is used for choosing a backend when sending a JSON-RPC request to
63 // the service.
64 enum ServiceType {
65 SUGGEST = 1,
66 SPELLCHECK = 2,
67 };
[email protected]65d988362012-02-03 11:20:0768 typedef base::Callback<void(
[email protected]80fe2b362012-05-02 04:19:3869 bool /* success */,
[email protected]8ca27e632012-05-28 07:39:3570 const string16& /* text */,
[email protected]7ed49c22012-02-17 02:14:5371 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0772 TextCheckCompleteCallback;
73
74 SpellingServiceClient();
75 virtual ~SpellingServiceClient();
76
[email protected]15fb2aa2012-05-22 22:52:5977 // net::URLFetcherDelegate implementation.
[email protected]10c2d692012-05-11 05:32:2378 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
[email protected]65d988362012-02-03 11:20:0779
80 // Sends a text-check request to the Spelling service. When we send a request
81 // to the Spelling service successfully, this function returns true. (This
82 // does not mean the service finishes checking text successfully.) We will
83 // call |callback| when we receive a text-check response from the service.
84 bool RequestTextCheck(Profile* profile,
[email protected]182bdf02012-04-07 01:40:4185 ServiceType type,
[email protected]65d988362012-02-03 11:20:0786 const string16& text,
87 const TextCheckCompleteCallback& callback);
88
[email protected]12cc36532012-06-14 05:38:0489 // Returns whether the specified service is available for the given profile.
90 static bool IsAvailable(Profile* profile, ServiceType type);
91
[email protected]65d988362012-02-03 11:20:0792 private:
[email protected]80fe2b362012-05-02 04:19:3893 // Creates a URLFetcher object used for sending a JSON-RPC request. This
94 // function is overriden by unit tests to prevent them from actually sending
95 // requests to the Spelling service.
[email protected]15fb2aa2012-05-22 22:52:5996 virtual net::URLFetcher* CreateURLFetcher(const GURL& url);
[email protected]80fe2b362012-05-02 04:19:3897
[email protected]65d988362012-02-03 11:20:0798 // Parses a JSON-RPC response from the Spelling service.
99 bool ParseResponse(const std::string& data,
[email protected]7ed49c22012-02-17 02:14:53100 std::vector<SpellCheckResult>* results);
[email protected]65d988362012-02-03 11:20:07101
102 // The URLFetcher object used for sending a JSON-RPC request.
[email protected]15fb2aa2012-05-22 22:52:59103 scoped_ptr<net::URLFetcher> fetcher_;
[email protected]65d988362012-02-03 11:20:07104
105 // The callback function to be called when we receive a response from the
106 // Spelling service and parse it.
107 TextCheckCompleteCallback callback_;
108
[email protected]8ca27e632012-05-28 07:39:35109 // The text checked by the Spelling service.
110 string16 text_;
[email protected]65d988362012-02-03 11:20:07111};
112
113#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_