Home | History | Annotate | Download | only in cloud_print
      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_PRINTING_CLOUD_PRINT_CLOUD_PRINT_SETUP_FLOW_H_
      6 #define CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_SETUP_FLOW_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/time.h"
     13 #include "chrome/browser/ui/webui/html_dialog_ui.h"
     14 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
     15 #include "chrome/common/net/gaia/gaia_auth_fetcher.h"
     16 #include "chrome/common/net/gaia/google_service_auth_error.h"
     17 #include "grit/generated_resources.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 #include "ui/gfx/native_widget_types.h"
     20 
     21 class GaiaAuthFetcher;
     22 class CloudPrintServiceProcessHelper;
     23 class CloudPrintSetupMessageHandler;
     24 class ServiceProcessControl;
     25 class GoogleServiceAuthError;
     26 class Browser;
     27 
     28 // This class is responsible for showing a cloud print setup dialog
     29 // and perform operations to fill the content of the dialog and handle
     30 // user actions in the dialog.
     31 //
     32 // It is responsible for:
     33 // 1. Showing the setup dialog.
     34 // 2. Providing the URL for the content of the dialog.
     35 // 3. Providing a data source to provide the content HTML files.
     36 // 4. Providing a message handler to handle user actions in the Web UI.
     37 // 5. Responding to actions received in the message handler.
     38 //
     39 // The architecture for WebUI is designed such that only the message handler
     40 // can access the WebUI. This splits the flow control across the message
     41 // handler and this class. In order to centralize all the flow control and
     42 // content in the WebUI, the WebUI object is given to this object by the
     43 // message handler through the Attach(WebUI*) method.
     44 class CloudPrintSetupFlow : public HtmlDialogUIDelegate,
     45                             public GaiaAuthConsumer {
     46  public:
     47   class Delegate {
     48    public:
     49     virtual ~Delegate() {}
     50     // Called when the setup dialog is closed.
     51     virtual void OnDialogClosed() = 0;
     52   };
     53   virtual ~CloudPrintSetupFlow();
     54 
     55   // Runs a flow from |start| to |end|, and does the work of actually showing
     56   // the HTML dialog.  |container| is kept up-to-date with the lifetime of the
     57   // flow (e.g it is emptied on dialog close).
     58   static CloudPrintSetupFlow* OpenDialog(
     59       Profile* service,
     60       const base::WeakPtr<Delegate>& delegate,
     61       gfx::NativeWindow parent_window);
     62 
     63   // Focuses the dialog.  This is useful in cases where the dialog has been
     64   // obscured by a browser window.
     65   void Focus();
     66 
     67   // HtmlDialogUIDelegate implementation.
     68   virtual GURL GetDialogContentURL() const;
     69   virtual void GetWebUIMessageHandlers(
     70       std::vector<WebUIMessageHandler*>* handlers) const;
     71   virtual void GetDialogSize(gfx::Size* size) const;
     72   virtual std::string GetDialogArgs() const;
     73   virtual void OnDialogClosed(const std::string& json_retval);
     74   virtual void OnCloseContents(TabContents* source, bool* out_close_dialog);
     75   virtual std::wstring GetDialogTitle() const;
     76   virtual bool IsDialogModal() const;
     77   virtual bool ShouldShowDialogTitle() const;
     78 
     79   // GaiaAuthConsumer implementation.
     80   virtual void OnClientLoginSuccess(
     81       const GaiaAuthConsumer::ClientLoginResult& credentials);
     82   virtual void OnClientLoginFailure(
     83       const GoogleServiceAuthError& error);
     84 
     85  private:
     86   friend class CloudPrintServiceProcessHelper;
     87   friend class CloudPrintSetupMessageHandler;
     88 
     89   // Use static Run method to get an instance.
     90   CloudPrintSetupFlow(const std::string& args, Profile* profile,
     91                       const base::WeakPtr<Delegate>& delegate, bool setup_done);
     92 
     93   // Called CloudPrintSetupMessageHandler when a DOM is attached. This method
     94   // is called when the HTML page is fully loaded. We then operate on this
     95   // WebUI object directly.
     96   void Attach(WebUI* web_ui);
     97 
     98   // Called by CloudPrintSetupMessageHandler when user authentication is
     99   // registered.
    100   void OnUserSubmittedAuth(const std::string& user,
    101                            const std::string& password,
    102                            const std::string& captcha,
    103                            const std::string& access_code);
    104 
    105   // Called by CloudPrintSetupMessageHandler when the user clicks on various
    106   // pieces of UI during setup.
    107   void OnUserClickedLearnMore();
    108   void OnUserClickedPrintTestPage();
    109 
    110   // The following methods control which iframe is visible.
    111   void ShowGaiaLogin(const DictionaryValue& args);
    112   void ShowGaiaSuccessAndSettingUp();
    113   void ShowGaiaFailed(const GoogleServiceAuthError& error);
    114   void ShowSetupDone();
    115   void ExecuteJavascriptInIFrame(const string16& iframe_xpath,
    116                                  const string16& js);
    117 
    118   // Pointer to the Web UI. This is provided by CloudPrintSetupMessageHandler
    119   // when attached. We do not own the pointer, instead WebUI owns it's delegate
    120   // (us) and controls our lifetime.
    121   WebUI* web_ui_;
    122 
    123   // The args to pass to the initial page.
    124   std::string dialog_start_args_;
    125   Profile* profile_;
    126 
    127   // Fetcher to obtain the Cloud Print token.
    128   scoped_ptr<GaiaAuthFetcher> authenticator_;
    129   std::string login_;
    130   std::string lsid_;
    131 
    132   // The last captcha or error state encountered.
    133   GoogleServiceAuthError last_auth_error_;
    134 
    135   // Are we in the done state?
    136   bool setup_done_;
    137 
    138   // Handle to the ServiceProcessControl which talks to the service process.
    139   ServiceProcessControl* process_control_;
    140   base::WeakPtr<Delegate> delegate_;
    141 
    142   DISALLOW_COPY_AND_ASSIGN(CloudPrintSetupFlow);
    143 };
    144 
    145 #endif  // CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_SETUP_FLOW_H_
    146