blob: 279ae983218dbcf51aa8205bee2babcddbe17bf4 [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"
16#include "content/public/common/url_fetcher_delegate.h"
17
[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
23// A class that encapsulates a JSON-RPC call to the Spelling service to check
24// text there. This class creates a JSON-RPC request, sends the request to the
25// service with URLFetcher, parses a response from the service, and calls a
26// provided callback method. When a user deletes this object before it finishes
27// a JSON-RPC call, this class cancels the JSON-RPC call without calling the
28// callback method. A simple usage is creating a SpellingServiceClient and
29// calling its RequestTextCheck method as listed in the following snippet.
30//
31// class MyClient {
32// public:
33// MyClient();
34// virtual ~MyClient();
35//
36// void OnTextCheckComplete(
37// int tag,
[email protected]80fe2b362012-05-02 04:19:3838// bool success,
[email protected]7ed49c22012-02-17 02:14:5339// const std::vector<SpellCheckResult>& results) {
[email protected]65d988362012-02-03 11:20:0740// ...
41// }
42//
43// void MyTextCheck(Profile* profile, const string16& text) {
44// client_.reset(new SpellingServiceClient);
45// client_->RequestTextCheck(profile, 0, text,
46// base::Bind(&MyClient::OnTextCheckComplete,
47// base::Unretained(this));
48// }
49// private:
50// scoped_ptr<SpellingServiceClient> client_;
51// };
52//
53class SpellingServiceClient : public content::URLFetcherDelegate {
54 public:
[email protected]182bdf02012-04-07 01:40:4155 // Service types provided by the Spelling service. The Spelling service
56 // consists of a couple of backends:
57 // * SUGGEST: Retrieving suggestions for a word (used by Google Search), and;
58 // * SPELLCHECK: Spellchecking text (used by Google Docs).
59 // This type is used for choosing a backend when sending a JSON-RPC request to
60 // the service.
61 enum ServiceType {
62 SUGGEST = 1,
63 SPELLCHECK = 2,
64 };
[email protected]65d988362012-02-03 11:20:0765 typedef base::Callback<void(
66 int /* tag */,
[email protected]80fe2b362012-05-02 04:19:3867 bool /* success */,
[email protected]7ed49c22012-02-17 02:14:5368 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0769 TextCheckCompleteCallback;
70
71 SpellingServiceClient();
72 virtual ~SpellingServiceClient();
73
74 // content::URLFetcherDelegate implementation.
75 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
76
77 // Sends a text-check request to the Spelling service. When we send a request
78 // to the Spelling service successfully, this function returns true. (This
79 // does not mean the service finishes checking text successfully.) We will
80 // call |callback| when we receive a text-check response from the service.
81 bool RequestTextCheck(Profile* profile,
82 int tag,
[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
87 private:
[email protected]80fe2b362012-05-02 04:19:3888 // Creates a URLFetcher object used for sending a JSON-RPC request. This
89 // function is overriden by unit tests to prevent them from actually sending
90 // requests to the Spelling service.
91 virtual content::URLFetcher* CreateURLFetcher(const GURL& url);
92
[email protected]65d988362012-02-03 11:20:0793 // Parses a JSON-RPC response from the Spelling service.
94 bool ParseResponse(const std::string& data,
[email protected]7ed49c22012-02-17 02:14:5395 std::vector<SpellCheckResult>* results);
[email protected]65d988362012-02-03 11:20:0796
97 // The URLFetcher object used for sending a JSON-RPC request.
98 scoped_ptr<content::URLFetcher> fetcher_;
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 identifier provided by users so they can identify a text-check request.
105 // When a JSON-RPC call finishes successfully, this value is used as the
106 // first parameter to |callback_|.
107 int tag_;
108};
109
110#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_