Home | History | Annotate | Download | only in login
      1 // 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_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/bind.h"
     11 #include "base/bind_helpers.h"
     12 #include "base/callback.h"
     13 #include "base/strings/string16.h"
     14 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler_utils.h"
     15 #include "content/public/browser/web_ui.h"
     16 #include "content/public/browser/web_ui_message_handler.h"
     17 #include "ui/gfx/native_widget_types.h"
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 class ListValue;
     22 class Value;
     23 }
     24 
     25 namespace chromeos {
     26 
     27 // Class that collects Localized Values for translation.
     28 class LocalizedValuesBuilder {
     29  public:
     30   explicit LocalizedValuesBuilder(base::DictionaryValue* dict);
     31   // Method to declare localized value. |key| is the i18n key used in html.
     32   // |message| is text of the message.
     33   void Add(const std::string& key, const std::string& message);
     34 
     35   // Method to declare localized value. |key| is the i18n key used in html.
     36   // |message| is text of the message.
     37   void Add(const std::string& key, const string16& message);
     38 
     39   // Method to declare localized value. |key| is the i18n key used in html.
     40   // |message_id| is a resource id of message.
     41   void Add(const std::string& key, int message_id);
     42 
     43   // Method to declare localized value. |key| is the i18n key used in html.
     44   // |message_id| is a resource id of message. Message is expected to have
     45   // one format parameter subsituted by |a|.
     46   void AddF(const std::string& key,
     47             int message_id,
     48             const string16& a);
     49 
     50   // Method to declare localized value. |key| is the i18n key used in html.
     51   // |message_id| is a resource id of message. Message is expected to have
     52   // two format parameters subsituted by |a| and |b| respectively.
     53   void AddF(const std::string& key,
     54             int message_id,
     55             const string16& a,
     56             const string16& b);
     57 
     58   // Method to declare localized value. |key| is the i18n key used in html.
     59   // |message_id| is a resource id of message. Message is expected to have
     60   // one format parameter subsituted by resource identified by |message_id_a|.
     61   void AddF(const std::string& key,
     62             int message_id,
     63             int message_id_a);
     64 
     65   // Method to declare localized value. |key| is the i18n key used in html.
     66   // |message_id| is a resource id of message. Message is expected to have
     67   // two format parameters subsituted by resource identified by |message_id_a|
     68   // and |message_id_b| respectively.
     69   void AddF(const std::string& key,
     70             int message_id,
     71             int message_id_a,
     72             int message_id_b);
     73  private:
     74   // Not owned.
     75   base::DictionaryValue* dict_;
     76 };
     77 
     78 // Base class for the OOBE/Login WebUI handlers.
     79 class BaseScreenHandler : public content::WebUIMessageHandler {
     80  public:
     81   // C-tor used when JS screen prefix is not needed.
     82   BaseScreenHandler();
     83 
     84   // C-tor used when JS screen prefix is needed.
     85   explicit BaseScreenHandler(const std::string& js_screen_path);
     86 
     87   virtual ~BaseScreenHandler();
     88 
     89   // Gets localized strings to be used on the page.
     90   void GetLocalizedStrings(
     91       base::DictionaryValue* localized_strings);
     92 
     93   // This method is called when page is ready. It propagates to inherited class
     94   // via virtual Initialize() method (see below).
     95   void InitializeBase();
     96 
     97  protected:
     98   // All subclasses should implement this method to provide localized values.
     99   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) = 0;
    100 
    101   // Subclasses can override these methods to pass additional parameters
    102   // to loadTimeData. Generally, it is a bad approach, and it should be replaced
    103   // with Context at some point.
    104   virtual void GetAdditionalParameters(base::DictionaryValue* parameters);
    105 
    106   // Shortcut for calling JS methods on WebUI side.
    107   void CallJS(const std::string& method);
    108 
    109   template<typename A1>
    110   void CallJS(const std::string& method, const A1& arg1) {
    111     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1));
    112   }
    113 
    114   template<typename A1, typename A2>
    115   void CallJS(const std::string& method, const A1& arg1, const A2& arg2) {
    116     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1),
    117                                      MakeValue(arg2));
    118   }
    119 
    120   template<typename A1, typename A2, typename A3>
    121   void CallJS(const std::string& method,
    122               const A1& arg1,
    123               const A2& arg2,
    124               const A3& arg3) {
    125     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    126                                      MakeValue(arg1),
    127                                      MakeValue(arg2),
    128                                      MakeValue(arg3));
    129   }
    130 
    131   template<typename A1, typename A2, typename A3, typename A4>
    132   void CallJS(const std::string& method,
    133               const A1& arg1,
    134               const A2& arg2,
    135               const A3& arg3,
    136               const A4& arg4) {
    137     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    138                                      MakeValue(arg1),
    139                                      MakeValue(arg2),
    140                                      MakeValue(arg3),
    141                                      MakeValue(arg4));
    142   }
    143 
    144   // Shortcut methods for adding WebUI callbacks.
    145   template<typename T>
    146   void AddRawCallback(const std::string& name,
    147                       void (T::*method)(const base::ListValue* args)) {
    148     web_ui()->RegisterMessageCallback(
    149         name,
    150         base::Bind(method, base::Unretained(static_cast<T*>(this))));
    151   }
    152 
    153   template<typename T>
    154   void AddCallback(const std::string& name, void (T::*method)()) {
    155     base::Callback<void()> callback =
    156         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    157     web_ui()->RegisterMessageCallback(
    158         name, base::Bind(&CallbackWrapper0, callback));
    159   }
    160 
    161   template<typename T, typename A1>
    162   void AddCallback(const std::string& name, void (T::*method)(A1 arg1)) {
    163     base::Callback<void(A1)> callback =
    164         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    165     web_ui()->RegisterMessageCallback(
    166         name, base::Bind(&CallbackWrapper1<A1>, callback));
    167   }
    168 
    169   template<typename T, typename A1, typename A2>
    170   void AddCallback(const std::string& name,
    171                    void (T::*method)(A1 arg1, A2 arg2)) {
    172     base::Callback<void(A1, A2)> callback =
    173         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    174     web_ui()->RegisterMessageCallback(
    175         name, base::Bind(&CallbackWrapper2<A1, A2>, callback));
    176   }
    177 
    178   template<typename T, typename A1, typename A2, typename A3>
    179   void AddCallback(const std::string& name,
    180                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3)) {
    181     base::Callback<void(A1, A2, A3)> callback =
    182         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    183     web_ui()->RegisterMessageCallback(
    184         name, base::Bind(&CallbackWrapper3<A1, A2, A3>, callback));
    185   }
    186 
    187   template<typename T, typename A1, typename A2, typename A3, typename A4>
    188   void AddCallback(const std::string& name,
    189                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3, A4 arg4)) {
    190     base::Callback<void(A1, A2, A3, A4)> callback =
    191         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    192     web_ui()->RegisterMessageCallback(
    193         name, base::Bind(&CallbackWrapper4<A1, A2, A3, A4>, callback));
    194   }
    195 
    196   // Called when the page is ready and handler can do initialization.
    197   virtual void Initialize() = 0;
    198 
    199   // Show selected WebUI |screen|. Optionally it can pass screen initialization
    200   // data via |data| parameter.
    201   void ShowScreen(const char* screen, const base::DictionaryValue* data);
    202 
    203   // Whether page is ready.
    204   bool page_is_ready() const { return page_is_ready_; }
    205 
    206   // Returns the window which shows us.
    207   virtual gfx::NativeWindow GetNativeWindow();
    208 
    209  private:
    210   // Returns full name of JS method based on screen and method
    211   // names.
    212   std::string FullMethodPath(const std::string& method) const;
    213 
    214   // Keeps whether page is ready.
    215   bool page_is_ready_;
    216 
    217   base::DictionaryValue* localized_values_;
    218 
    219   // Full name of the corresponding JS screen object. Can be empty, if
    220   // there are no corresponding screen object or several different
    221   // objects.
    222   std::string js_screen_path_prefix_;
    223 
    224   DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler);
    225 };
    226 
    227 }  // namespace chromeos
    228 
    229 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
    230