cloud_print_setup_flow.h revision dc0f95d653279beabeb9817299e2902918ba123e
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/time.h"
12#include "chrome/browser/ui/webui/html_dialog_ui.h"
13#include "chrome/common/net/gaia/gaia_auth_consumer.h"
14#include "chrome/common/net/gaia/gaia_auth_fetcher.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17#include "ui/gfx/native_widget_types.h"
18
19class GaiaAuthFetcher;
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 Web UI.
35// 5. Responding to actions received in the message handler.
36//
37// The architecture for WebUI is designed such that only the message handler
38// can access the WebUI. 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 WebUI, the WebUI object is given to this object by the
41// message handler through the Attach(WebUI*) 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 GetWebUIMessageHandlers(
66      std::vector<WebUIMessageHandler*>* 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  virtual bool ShouldShowDialogTitle() const;
74
75  // GaiaAuthConsumer implementation.
76  virtual void OnClientLoginFailure(
77      const GoogleServiceAuthError& error);
78  virtual void OnClientLoginSuccess(
79      const GaiaAuthConsumer::ClientLoginResult& credentials);
80
81 private:
82  friend class CloudPrintServiceProcessHelper;
83  friend class CloudPrintSetupMessageHandler;
84
85  // Use static Run method to get an instance.
86  CloudPrintSetupFlow(const std::string& args, Profile* profile,
87                      Delegate* delegate, bool setup_done);
88
89  // Called CloudPrintSetupMessageHandler when a DOM is attached. This method
90  // is called when the HTML page is fully loaded. We then operate on this
91  // WebUI object directly.
92  void Attach(WebUI* web_ui);
93
94  // Called by CloudPrintSetupMessageHandler when user authentication is
95  // registered.
96  void OnUserSubmittedAuth(const std::string& user,
97                           const std::string& password,
98                           const std::string& captcha);
99
100  // Called by CloudPrintSetupMessageHandler when the user clicks on various
101  // pieces of UI during setup.
102  void OnUserClickedLearnMore();
103  void OnUserClickedPrintTestPage();
104
105  // The following methods control which iframe is visible.
106  void ShowGaiaLogin(const DictionaryValue& args);
107  void ShowGaiaSuccessAndSettingUp();
108  void ShowGaiaFailed(const GoogleServiceAuthError& error);
109  void ShowSetupDone();
110  void ExecuteJavascriptInIFrame(const std::wstring& iframe_xpath,
111                                 const std::wstring& js);
112
113  // Pointer to the Web UI. This is provided by CloudPrintSetupMessageHandler
114  // when attached.
115  WebUI* web_ui_;
116
117  // The args to pass to the initial page.
118  std::string dialog_start_args_;
119  Profile* profile_;
120
121  // Fetcher to obtain the Chromoting Directory token.
122  scoped_ptr<GaiaAuthFetcher> authenticator_;
123  std::string login_;
124  std::string lsid_;
125
126  // Are we in the done state?
127  bool setup_done_;
128
129  // Handle to the ServiceProcessControl which talks to the service process.
130  ServiceProcessControl* process_control_;
131  Delegate* delegate_;
132
133  DISALLOW_COPY_AND_ASSIGN(CloudPrintSetupFlow);
134};
135
136#endif  // CHROME_BROWSER_PRINTING_CLOUD_PRINT_CLOUD_PRINT_SETUP_FLOW_H_
137