blob: ac129270cb5cd7c7289f10b2617946b4ed0c1b32 [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
18class Profile;
19class TextCheckClientDelegate;
[email protected]7ed49c22012-02-17 02:14:5320struct SpellCheckResult;
[email protected]65d988362012-02-03 11:20:0721
22// A class that encapsulates a JSON-RPC call to the Spelling service to check
23// text there. This class creates a JSON-RPC request, sends the request to the
24// service with URLFetcher, parses a response from the service, and calls a
25// provided callback method. When a user deletes this object before it finishes
26// a JSON-RPC call, this class cancels the JSON-RPC call without calling the
27// callback method. A simple usage is creating a SpellingServiceClient and
28// calling its RequestTextCheck method as listed in the following snippet.
29//
30// class MyClient {
31// public:
32// MyClient();
33// virtual ~MyClient();
34//
35// void OnTextCheckComplete(
36// int tag,
[email protected]7ed49c22012-02-17 02:14:5337// const std::vector<SpellCheckResult>& results) {
[email protected]65d988362012-02-03 11:20:0738// ...
39// }
40//
41// void MyTextCheck(Profile* profile, const string16& text) {
42// client_.reset(new SpellingServiceClient);
43// client_->RequestTextCheck(profile, 0, text,
44// base::Bind(&MyClient::OnTextCheckComplete,
45// base::Unretained(this));
46// }
47// private:
48// scoped_ptr<SpellingServiceClient> client_;
49// };
50//
51class SpellingServiceClient : public content::URLFetcherDelegate {
52 public:
53 typedef base::Callback<void(
54 int /* tag */,
[email protected]7ed49c22012-02-17 02:14:5355 const std::vector<SpellCheckResult>& /* results */)>
[email protected]65d988362012-02-03 11:20:0756 TextCheckCompleteCallback;
57
58 SpellingServiceClient();
59 virtual ~SpellingServiceClient();
60
61 // content::URLFetcherDelegate implementation.
62 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
63
64 // Sends a text-check request to the Spelling service. When we send a request
65 // to the Spelling service successfully, this function returns true. (This
66 // does not mean the service finishes checking text successfully.) We will
67 // call |callback| when we receive a text-check response from the service.
68 bool RequestTextCheck(Profile* profile,
69 int tag,
70 const string16& text,
71 const TextCheckCompleteCallback& callback);
72
73 private:
74 // Parses a JSON-RPC response from the Spelling service.
75 bool ParseResponse(const std::string& data,
[email protected]7ed49c22012-02-17 02:14:5376 std::vector<SpellCheckResult>* results);
[email protected]65d988362012-02-03 11:20:0777
78 // The URLFetcher object used for sending a JSON-RPC request.
79 scoped_ptr<content::URLFetcher> fetcher_;
80
81 // The callback function to be called when we receive a response from the
82 // Spelling service and parse it.
83 TextCheckCompleteCallback callback_;
84
85 // The identifier provided by users so they can identify a text-check request.
86 // When a JSON-RPC call finishes successfully, this value is used as the
87 // first parameter to |callback_|.
88 int tag_;
89};
90
91#endif // CHROME_BROWSER_SPELLCHECKER_SPELLING_SERVICE_CLIENT_H_