• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CHROMEOS_LOGIN_CAPTCHA_VIEW_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_CAPTCHA_VIEW_H_
7 #pragma once
8 
9 #include <string>
10 
11 #include "chrome/browser/chromeos/login/image_decoder.h"
12 #include "googleurl/src/gurl.h"
13 #include "views/controls/button/button.h"
14 #include "views/controls/textfield/textfield_controller.h"
15 #include "views/window/dialog_delegate.h"
16 
17 namespace views {
18 class ImageView;
19 class TextButton;
20 class View;
21 class Window;
22 }  // namespace views
23 
24 namespace chromeos {
25 
26 // A dialog box that shows a CAPTCHA image and allows user to input response.
27 class CaptchaView : public views::View,
28                     public views::DialogDelegate,
29                     public views::TextfieldController,
30                     public ImageDecoder::Delegate,
31                     public views::ButtonListener {
32  public:
33   class Delegate {
34    public:
35     // Called when CAPTCHA answer has been entered.
36     virtual void OnCaptchaEntered(const std::string& captcha) = 0;
37 
38    protected:
~Delegate()39      virtual ~Delegate() {}
40   };
41 
42   // |captcha_url| represents CAPTCHA image URL.
43   // |is_standalone| is true when CaptchaView is not presented as dialog.
44   CaptchaView(const GURL& captcha_url, bool is_standalone);
~CaptchaView()45   virtual ~CaptchaView() {}
46 
47   // views::DialogDelegate:
48   virtual bool Accept();
IsModal()49   virtual bool IsModal() const { return true; }
GetContentsView()50   virtual views::View* GetContentsView() { return this; }
51   virtual std::wstring GetWindowTitle() const;
52 
53   // views::TextfieldController:
ContentsChanged(views::Textfield * sender,const string16 & new_contents)54   virtual void ContentsChanged(views::Textfield* sender,
55                                const string16& new_contents) {}
56   virtual bool HandleKeyEvent(views::Textfield* sender,
57                               const views::KeyEvent& key_event);
58 
59   // ImageDownloader::Delegate:
60   virtual void OnImageDecoded(const ImageDecoder* decoder,
61                               const SkBitmap& decoded_image);
62 
63   // views::ButtonListener:
64   virtual void ButtonPressed(views::Button* sender, const views::Event& event);
65 
66   // Initializes UI.
67   void Init();
68 
set_delegate(Delegate * delegate)69   void set_delegate(Delegate* delegate) {
70     delegate_ = delegate;
71   }
72 
73   // Instructs to download and display another captcha image.
74   // Is used when same CaptchaView is reused.
75   void SetCaptchaURL(const GURL& captcha_url);
76 
77  protected:
78   // views::View:
79   virtual gfx::Size GetPreferredSize();
80   virtual void ViewHierarchyChanged(bool is_add,
81                                     views::View* parent,
82                                     views::View* child);
83 
84  private:
85   Delegate* delegate_;
86   GURL captcha_url_;
87   views::ImageView* captcha_image_;
88   views::Textfield* captcha_textfield_;
89 
90   // True when view is not hosted inside dialog,
91   // thus should draw OK button/background.
92   bool is_standalone_;
93 
94   // Used in standalone mode.
95   views::TextButton* ok_button_;
96 
97   DISALLOW_COPY_AND_ASSIGN(CaptchaView);
98 };
99 
100 }  // namespace chromeos
101 
102 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_CAPTCHA_VIEW_H_
103