cloud_print_setup_flow.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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 "app/l10n_util.h"
12#include "base/time.h"
13#include "chrome/browser/dom_ui/html_dialog_ui.h"
14#include "chrome/common/net/gaia/gaia_auth_consumer.h"
15#include "chrome/common/net/gaia/gaia_authenticator2.h"
16#include "gfx/native_widget_types.h"
17#include "grit/generated_resources.h"
18
19class GaiaAuthenticator2;
20class CloudPrintServiceProcessHelper;
21class CloudPrintSetupMessageHandler;
22class ServiceProcessControl;
23class GoogleServiceAuthError;
24class Browser;
25
26// This class is responsible for showing a cloud print setup dialog
27// and perform operations to fill the content of the dialog and handle
28// user actions in the dialog.
29//
30// It is responsible for:
31// 1. Showing the setup dialog.
32// 2. Providing the URL for the content of the dialog.
33// 3. Providing a data source to provide the content HTML files.
34// 4. Providing a message handler to handle user actions in the DOM UI.
35// 5. Responding to actions received in the message handler.
36//
37// The architecture for DOMUI is designed such that only the message handler
38// can access the DOMUI. This splits the flow control across the message
39// handler and this class. In order to centralize all the flow control and
40// content in the DOMUI, the DOMUI object is given to this object by the
41// message handler through the Attach(DOMUI*) method.
42class CloudPrintSetupFlow : public HtmlDialogUIDelegate,
43                            public GaiaAuthConsumer {
44 public:
45  class Delegate {
46   public:
47    virtual ~Delegate() {}
48    // Called when the setup dialog is closed.
49    virtual void OnDialogClosed() = 0;
50  };
51  virtual ~CloudPrintSetupFlow();
52
53  // Runs a flow from |start| to |end|, and does the work of actually showing
54  // the HTML dialog.  |container| is kept up-to-date with the lifetime of the
55  // flow (e.g it is emptied on dialog close).
56  static CloudPrintSetupFlow* OpenDialog(Profile* service, Delegate* delegate,
57                                         gfx::NativeWindow parent_window);
58
59  // Focuses the dialog.  This is useful in cases where the dialog has been
60  // obscured by a browser window.
61  void Focus();
62
63  // HtmlDialogUIDelegate implementation.
64  virtual GURL GetDialogContentURL() const;
65  virtual void GetDOMMessageHandlers(
66      std::vector<DOMMessageHandler*>* handlers) const;
67  virtual void GetDialogSize(gfx::Size* size) const;
68  virtual std::string GetDialogArgs() const;
69  virtual void OnDialogClosed(const std::string& json_retval);
70  virtual void OnCloseContents(TabContents* source, bool* out_close_dialog);
71  virtual std::wstring GetDialogTitle() const;
72  virtual bool IsDialogModal() const;
73
74  // GaiaAuthConsumer implementation.
75  virtual void OnClientLoginFailure(
76      const GoogleServiceAuthError& error);
77  virtual void OnClientLoginSuccess(
78      const GaiaAuthConsumer::ClientLoginResult& credentials);
79
80 private:
81  friend class CloudPrintServiceProcessHelper;
82  friend class CloudPrintSetupMessageHandler;
83
84  // Use static Run method to get an instance.
85  CloudPrintSetupFlow(const std::string& args, Profile* profile,
86                      Delegate* delegate);
87
88  // Called CloudPrintSetupMessageHandler when a DOM is attached. This method
89  // is called when the HTML page is fully loaded. We then operate on this
90  // DOMUI object directly.
91  void Attach(DOMUI* dom_ui);
92
93  // Called by CloudPrintSetupMessageHandler when user authentication is
94  // registered.
95  void OnUserSubmittedAuth(const std::string& user,
96                           const std::string& password,
97                           const std::string& captcha);
98
99  // The following methods control which iframe is visible.
100  void ShowGaiaLogin(const DictionaryValue& args);
101  void ShowGaiaSuccessAndSettingUp();
102  void ShowGaiaFailed(const GoogleServiceAuthError& error);
103  void ShowSetupDone();
104  void ExecuteJavascriptInIFrame(const std::wstring& iframe_xpath,
105                                 const std::wstring& js);
106
107  // Pointer to the DOM UI. This is provided by CloudPrintSetupMessageHandler
108  // when attached.
109  DOMUI* dom_ui_;
110
111  // The args to pass to the initial page.
112  std::string dialog_start_args_;
113  Profile* profile_;
114
115  // Fetcher to obtain the Chromoting Directory token.
116  scoped_ptr<GaiaAuthenticator2> authenticator_;
117  std::string login_;
118  std::string lsid_;
119
120  // Handle to the ServiceProcessControl which talks to the service process.
121  ServiceProcessControl* process_control_;
122  Delegate* delegate_;
123
124  DISALLOW_COPY_AND_ASSIGN(CloudPrintSetupFlow);
125};
126
127#endif  // CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_SETUP_FLOW_H_
128