| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
| 6 #define CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | 6 #define CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/timer.h" | 13 #include "base/timer.h" |
| 14 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" | 14 #include "chrome/browser/tab_contents/render_view_context_menu_observer.h" |
| 15 #include "content/common/net/url_fetcher.h" | 15 #include "content/public/common/url_fetcher_delegate.h" |
| 16 | 16 |
| 17 class GURL; | 17 class GURL; |
| 18 class RenderViewContextMenuProxy; | 18 class RenderViewContextMenuProxy; |
| 19 | 19 |
| 20 namespace net { |
| 21 class URLRequestContextGetter; |
| 22 } |
| 23 |
| 20 // An observer that listens to events from the RenderViewContextMenu class and | 24 // 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 | 25 // shows suggestions from the Spelling ("do you mean") service to a context menu |
| 22 // while we show it. This class implements two interfaces: | 26 // while we show it. This class implements two interfaces: |
| 23 // * RenderViewContextMenuObserver | 27 // * RenderViewContextMenuObserver |
| 24 // This interface is used for adding a menu item and update it while showing. | 28 // This interface is used for adding a menu item and update it while showing. |
| 25 // * URLFetcher::Delegate | 29 // * content::URLFetcherDelegate |
| 26 // This interface is used for sending a JSON_RPC request to the Spelling | 30 // This interface is used for sending a JSON_RPC request to the Spelling |
| 27 // service and retrieving its response. | 31 // service and retrieving its response. |
| 28 // These interfaces allow this class to make a JSON-RPC call to the Spelling | 32 // 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 | 33 // 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 | 34 // following snippet describes how to add this class to the observer list of the |
| 31 // RenderViewContextMenu class. | 35 // RenderViewContextMenu class. |
| 32 // | 36 // |
| 33 // void RenderViewContextMenu::InitMenu() { | 37 // void RenderViewContextMenu::InitMenu() { |
| 34 // spelling_menu_observer_.reset(new SpellingMenuObserver(this)); | 38 // spelling_menu_observer_.reset(new SpellingMenuObserver(this)); |
| 35 // if (spelling_menu_observer_.get()) | 39 // if (spelling_menu_observer_.get()) |
| 36 // observers_.AddObserver(spelling_menu_observer.get()); | 40 // observers_.AddObserver(spelling_menu_observer.get()); |
| 37 // } | 41 // } |
| 38 // | 42 // |
| 39 class SpellingMenuObserver : public RenderViewContextMenuObserver, | 43 class SpellingMenuObserver : public RenderViewContextMenuObserver, |
| 40 public URLFetcher::Delegate { | 44 public content::URLFetcherDelegate { |
| 41 public: | 45 public: |
| 42 explicit SpellingMenuObserver(RenderViewContextMenuProxy* proxy); | 46 explicit SpellingMenuObserver(RenderViewContextMenuProxy* proxy); |
| 43 virtual ~SpellingMenuObserver(); | 47 virtual ~SpellingMenuObserver(); |
| 44 | 48 |
| 45 // RenderViewContextMenuObserver implementation. | 49 // RenderViewContextMenuObserver implementation. |
| 46 virtual void InitMenu(const ContextMenuParams& params) OVERRIDE; | 50 virtual void InitMenu(const ContextMenuParams& params) OVERRIDE; |
| 47 virtual bool IsCommandIdSupported(int command_id) OVERRIDE; | 51 virtual bool IsCommandIdSupported(int command_id) OVERRIDE; |
| 48 virtual bool IsCommandIdEnabled(int command_id) OVERRIDE; | 52 virtual bool IsCommandIdEnabled(int command_id) OVERRIDE; |
| 49 virtual void ExecuteCommand(int command_id) OVERRIDE; | 53 virtual void ExecuteCommand(int command_id) OVERRIDE; |
| 50 | 54 |
| 51 // URLFetcher::Delegate implementation. | 55 // content::URLFetcherDelegate implementation. |
| 52 virtual void OnURLFetchComplete(const URLFetcher* source, | 56 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| 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 | 57 |
| 59 private: | 58 private: |
| 60 // Invokes a JSON-RPC call in the background. This function sends a JSON-RPC | 59 // 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 | 60 // request to the Spelling service. Chrome will call OnURLFetchComplete() when |
| 62 // it receives a response from the service. | 61 // it receives a response from the service. |
| 63 bool Invoke(const string16& text, | 62 bool Invoke(const string16& text, |
| 64 const std::string& locale, | 63 const std::string& locale, |
| 65 net::URLRequestContextGetter* context); | 64 net::URLRequestContextGetter* context); |
| 66 | 65 |
| 67 // Parses the specified response from the Spelling service. | 66 // Parses the specified response from the Spelling service. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 97 // The URLFetcher object used for sending a JSON-RPC request. | 96 // The URLFetcher object used for sending a JSON-RPC request. |
| 98 scoped_ptr<URLFetcher> fetcher_; | 97 scoped_ptr<URLFetcher> fetcher_; |
| 99 | 98 |
| 100 // A timer used for loading animation. | 99 // A timer used for loading animation. |
| 101 base::RepeatingTimer<SpellingMenuObserver> animation_timer_; | 100 base::RepeatingTimer<SpellingMenuObserver> animation_timer_; |
| 102 | 101 |
| 103 DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserver); | 102 DISALLOW_COPY_AND_ASSIGN(SpellingMenuObserver); |
| 104 }; | 103 }; |
| 105 | 104 |
| 106 #endif // CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ | 105 #endif // CHROME_BROWSER_TAB_CONTENTS_SPELLING_MENU_OBSERVER_H_ |
| OLD | NEW |