OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
| 6 #define CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string16.h" |
| 13 #include "base/timer.h" |
| 14 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" |
| 15 #include "content/common/url_fetcher.h" |
| 16 |
| 17 class GURL; |
| 18 class RenderViewContextMenuProxy; |
| 19 |
| 20 // An observer that listens to events from the RenderViewContextMenu class and |
| 21 // shows suggestions from the Spelling ("do you mean") service to a context menu |
| 22 // while we show it. This class implements two interfaces: |
| 23 // * RenderViewContextMenuObserver |
| 24 // This interface is used for adding a menu item and update it while showing. |
| 25 // * URLFetcher::Delegate |
| 26 // This interface is used for sending a JSON_RPC request to the Spelling |
| 27 // service and retrieving its response. |
| 28 // These interfaces allow this class to make a JSON-RPC call to the Spelling |
| 29 // service in the background and update the context menu while showing. The |
| 30 // following snippet describes how to add this class to the observer list of the |
| 31 // RenderViewContextMenu class. |
| 32 // |
| 33 // void RenderViewContextMenu::InitMenu() { |
| 34 // spelling_menu_observer_.reset(new SpellingMenuObserver(this)); |
| 35 // if (spelling_menu_observer_.get()) |
| 36 // observers_.AddObserver(spelling_menu_observer.get()); |
| 37 // } |
| 38 // |
| 39 class SpellingMenuObserver : public RenderViewContextMenuObserver, |
| 40 public URLFetcher::Delegate { |
| 41 public: |
| 42 explicit SpellingMenuObserver(RenderViewContextMenuProxy* proxy); |
| 43 virtual ~SpellingMenuObserver(); |
| 44 |
| 45 // RenderViewContextMenuObserver implementation. |
| 46 virtual void InitMenu(const ContextMenuParams& params) OVERRIDE; |
| 47 virtual bool IsCommandIdSupported(int command_id) OVERRIDE; |
| 48 virtual bool IsCommandIdEnabled(int command_id) OVERRIDE; |
| 49 virtual void ExecuteCommand(int command_id) OVERRIDE; |
| 50 |
| 51 // URLFetcher::Delegate implementation. |
| 52 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 53 const GURL& url, |
| 54 const net::URLRequestStatus& status, |
| 55 int response_code, |
| 56 const net::ResponseCookies& cookies, |
| 57 const std::string& data) OVERRIDE; |
| 58 |
| 59 private: |
| 60 // Invokes a JSON-RPC call in the background. This function sends a JSON-RPC |
| 61 // request to the Spelling service. Chrome will call OnURLFetchComplete() when |
| 62 // it receives a response from the service. |
| 63 bool Invoke(const string16& text, |
| 64 const std::string& locale, |
| 65 net::URLRequestContextGetter* context); |
| 66 |
| 67 // Parses the specified response from the Spelling service. |
| 68 bool ParseResponse(int code, const std::string& data); |
| 69 |
| 70 // The callback function for base::RepeatingTimer<SpellingMenuClient>. This |
| 71 // function updates the "loading..." animation in the context-menu item. |
| 72 void OnAnimationTimerExpired(); |
| 73 |
| 74 // The interface to add a context-menu item and update it. This class uses |
| 75 // this interface to avoid accesing context-menu items directly. |
| 76 RenderViewContextMenuProxy* proxy_; |
| 77 |
| 78 // The string used for animation until we receive a response from the Spelling |
| 79 // service. The current animation just adds periods at the end of this string: |
| 80 // 'Loading' -> 'Loading.' -> 'Loading..' -> 'Loading...' (-> 'Loading') |
| 81 string16 loading_message_; |
| 82 int loading_frame_; |
| 83 |
| 84 // A flag represending whether this call finished successfully. This means we |
| 85 // receive an empty JSON string or a JSON string that consists of misspelled |
| 86 // words. ('spelling_menu_observer.cc' describes its format.) |
| 87 bool succeeded_; |
| 88 |
| 89 // The string representing the result of this call. This string is a |
| 90 // suggestion when this call finished successfully. Otherwise it is error |
| 91 // text. Until we receive a response from the Spelling service, this string |
| 92 // stores the input string. (Since the Spelling service sends only misspelled |
| 93 // words, we replace these misspelled words in the input text with the |
| 94 // suggested words to create suggestion text. |
| 95 string16 result_; |
| 96 |
| 97 // The URLFetcher object used for sending a JSON-RPC request. |
| 98 scoped_ptr<URLFetcher> fetcher_; |
| 99 |
| 100 // A timer used for loading animation. |
| 101 base::RepeatingTimer<SpellingMenuObserver> animation_timer_; |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserver); |
| 104 }; |
| 105 |
| 106 #endif // CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
OLD | NEW |