blob: be9d33c2a7fc84079c939a150b5af83fff20081b [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_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/callback.h"
13#include "base/compiler_specific.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/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(
70 int /* tag */,
[email protected]80fe2b362012-05-02 04:19:3871 bool /* success */,
[email protected]8ca27e632012-05-28 07:39:3572 const string16& /* text */,
[email protected]7ed49c22012-02-17 02:14:5373 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0774 TextCheckCompleteCallback;
75
76 SpellingServiceClient();
77 virtual ~SpellingServiceClient();
78
[email protected]15fb2aa2012-05-22 22:52:5979 // net::URLFetcherDelegate implementation.
[email protected]10c2d692012-05-11 05:32:2380 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
[email protected]65d988362012-02-03 11:20:0781
82 // Sends a text-check request to the Spelling service. When we send a request
83 // to the Spelling service successfully, this function returns true. (This
84 // does not mean the service finishes checking text successfully.) We will
85 // call |callback| when we receive a text-check response from the service.
86 bool RequestTextCheck(Profile* profile,
87 int tag,
[email protected]182bdf02012-04-07 01:40:4188 ServiceType type,
[email protected]65d988362012-02-03 11:20:0789 const string16& text,
90 const TextCheckCompleteCallback& callback);
91
[email protected]12cc36532012-06-14 05:38:0492 // Returns whether the specified service is available for the given profile.
93 static bool IsAvailable(Profile* profile, ServiceType type);
94
[email protected]65d988362012-02-03 11:20:0795 private:
[email protected]80fe2b362012-05-02 04:19:3896 // Creates a URLFetcher object used for sending a JSON-RPC request. This
97 // function is overriden by unit tests to prevent them from actually sending
98 // requests to the Spelling service.
[email protected]15fb2aa2012-05-22 22:52:5999 virtual net::URLFetcher* CreateURLFetcher(const GURL& url);
[email protected]80fe2b362012-05-02 04:19:38100
[email protected]65d988362012-02-03 11:20:07101 // Parses a JSON-RPC response from the Spelling service.
102 bool ParseResponse(const std::string& data,
[email protected]7ed49c22012-02-17 02:14:53103 std::vector<SpellCheckResult>* results);
[email protected]65d988362012-02-03 11:20:07104
105 // The URLFetcher object used for sending a JSON-RPC request.
[email protected]15fb2aa2012-05-22 22:52:59106 scoped_ptr<net::URLFetcher> fetcher_;
[email protected]65d988362012-02-03 11:20:07107
108 // The callback function to be called when we receive a response from the
109 // Spelling service and parse it.
110 TextCheckCompleteCallback callback_;
111
[email protected]8ca27e632012-05-28 07:39:35112 // The text checked by the Spelling service.
113 string16 text_;
114
[email protected]65d988362012-02-03 11:20:07115 // The identifier provided by users so they can identify a text-check request.
116 // When a JSON-RPC call finishes successfully, this value is used as the
117 // first parameter to |callback_|.
118 int tag_;
119};
120
121#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_