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 base::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 base::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 base::string16& a,
     56             const base::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   void set_async_assets_load_id(const std::string& async_assets_load_id) {
     98     async_assets_load_id_ = async_assets_load_id;
     99   }
    100   const std::string& async_assets_load_id() const {
    101     return async_assets_load_id_;
    102   }
    103 
    104  protected:
    105   // All subclasses should implement this method to provide localized values.
    106   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) = 0;
    107 
    108   // Subclasses can override these methods to pass additional parameters
    109   // to loadTimeData. Generally, it is a bad approach, and it should be replaced
    110   // with Context at some point.
    111   virtual void GetAdditionalParameters(base::DictionaryValue* parameters);
    112 
    113   // Shortcut for calling JS methods on WebUI side.
    114   void CallJS(const std::string& method);
    115 
    116   template<typename A1>
    117   void CallJS(const std::string& method, const A1& arg1) {
    118     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1));
    119   }
    120 
    121   template<typename A1, typename A2>
    122   void CallJS(const std::string& method, const A1& arg1, const A2& arg2) {
    123     web_ui()->CallJavascriptFunction(FullMethodPath(method), MakeValue(arg1),
    124                                      MakeValue(arg2));
    125   }
    126 
    127   template<typename A1, typename A2, typename A3>
    128   void CallJS(const std::string& method,
    129               const A1& arg1,
    130               const A2& arg2,
    131               const A3& arg3) {
    132     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    133                                      MakeValue(arg1),
    134                                      MakeValue(arg2),
    135                                      MakeValue(arg3));
    136   }
    137 
    138   template<typename A1, typename A2, typename A3, typename A4>
    139   void CallJS(const std::string& method,
    140               const A1& arg1,
    141               const A2& arg2,
    142               const A3& arg3,
    143               const A4& arg4) {
    144     web_ui()->CallJavascriptFunction(FullMethodPath(method),
    145                                      MakeValue(arg1),
    146                                      MakeValue(arg2),
    147                                      MakeValue(arg3),
    148                                      MakeValue(arg4));
    149   }
    150 
    151   // Shortcut methods for adding WebUI callbacks.
    152   template<typename T>
    153   void AddRawCallback(const std::string& name,
    154                       void (T::*method)(const base::ListValue* args)) {
    155     web_ui()->RegisterMessageCallback(
    156         name,
    157         base::Bind(method, base::Unretained(static_cast<T*>(this))));
    158   }
    159 
    160   template<typename T>
    161   void AddCallback(const std::string& name, void (T::*method)()) {
    162     base::Callback<void()> callback =
    163         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    164     web_ui()->RegisterMessageCallback(
    165         name, base::Bind(&CallbackWrapper0, callback));
    166   }
    167 
    168   template<typename T, typename A1>
    169   void AddCallback(const std::string& name, void (T::*method)(A1 arg1)) {
    170     base::Callback<void(A1)> callback =
    171         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    172     web_ui()->RegisterMessageCallback(
    173         name, base::Bind(&CallbackWrapper1<A1>, callback));
    174   }
    175 
    176   template<typename T, typename A1, typename A2>
    177   void AddCallback(const std::string& name,
    178                    void (T::*method)(A1 arg1, A2 arg2)) {
    179     base::Callback<void(A1, A2)> callback =
    180         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    181     web_ui()->RegisterMessageCallback(
    182         name, base::Bind(&CallbackWrapper2<A1, A2>, callback));
    183   }
    184 
    185   template<typename T, typename A1, typename A2, typename A3>
    186   void AddCallback(const std::string& name,
    187                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3)) {
    188     base::Callback<void(A1, A2, A3)> callback =
    189         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    190     web_ui()->RegisterMessageCallback(
    191         name, base::Bind(&CallbackWrapper3<A1, A2, A3>, callback));
    192   }
    193 
    194   template<typename T, typename A1, typename A2, typename A3, typename A4>
    195   void AddCallback(const std::string& name,
    196                    void (T::*method)(A1 arg1, A2 arg2, A3 arg3, A4 arg4)) {
    197     base::Callback<void(A1, A2, A3, A4)> callback =
    198         base::Bind(method, base::Unretained(static_cast<T*>(this)));
    199     web_ui()->RegisterMessageCallback(
    200         name, base::Bind(&CallbackWrapper4<A1, A2, A3, A4>, callback));
    201   }
    202 
    203   // Called when the page is ready and handler can do initialization.
    204   virtual void Initialize() = 0;
    205 
    206   // Show selected WebUI |screen|. Optionally it can pass screen initialization
    207   // data via |data| parameter.
    208   void ShowScreen(const char* screen, const base::DictionaryValue* data);
    209 
    210   // Whether page is ready.
    211   bool page_is_ready() const { return page_is_ready_; }
    212 
    213   // Returns the window which shows us.
    214   virtual gfx::NativeWindow GetNativeWindow();
    215 
    216  private:
    217   // Returns full name of JS method based on screen and method
    218   // names.
    219   std::string FullMethodPath(const std::string& method) const;
    220 
    221   // Keeps whether page is ready.
    222   bool page_is_ready_;
    223 
    224   base::DictionaryValue* localized_values_;
    225 
    226   // Full name of the corresponding JS screen object. Can be empty, if
    227   // there are no corresponding screen object or several different
    228   // objects.
    229   std::string js_screen_path_prefix_;
    230 
    231   // The string id used in the async asset load in JS. If it is set to a
    232   // non empty value, the Initialize will be deferred until the underlying load
    233   // is finished.
    234   std::string async_assets_load_id_;
    235 
    236   DISALLOW_COPY_AND_ASSIGN(BaseScreenHandler);
    237 };
    238 
    239 }  // namespace chromeos
    240 
    241 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_BASE_SCREEN_HANDLER_H_
    242