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#include "chrome/browser/chromeos/login/account_creation_view.h"
6
7#include "base/string_util.h"
8#include "chrome/common/autofill_messages.h"
9#include "webkit/glue/form_data.h"
10
11using webkit_glue::FormData;
12
13namespace chromeos {
14
15const char kCreateAccountFormName[] = "createaccount";
16const char kEmailFieldName[] = "Email";
17const char kDomainFieldName[] = "edk";
18
19class AccountCreationTabContents : public WizardWebPageViewTabContents {
20 public:
21  AccountCreationTabContents(Profile* profile,
22                             SiteInstance* site_instance,
23                             AccountCreationViewDelegate* delegate,
24                             WebPageDelegate* page_delegate)
25      : WizardWebPageViewTabContents(profile, site_instance, page_delegate),
26        delegate_(delegate) {
27  }
28
29  // Overriden from TabContents.
30  virtual bool OnMessageReceived(const IPC::Message& message) {
31    bool handled = true;
32    IPC_BEGIN_MESSAGE_MAP(AccountCreationTabContents, message)
33      IPC_MESSAGE_HANDLER(AutofillHostMsg_FormSubmitted, OnFormSubmitted)
34      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_FormsSeen, )
35      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_QueryFormFieldAutofill, )
36      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_ShowAutofillDialog, )
37      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_FillAutofillFormData, )
38      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_DidFillAutofillFormData, )
39      IPC_MESSAGE_HANDLER_GENERIC(AutofillHostMsg_DidShowAutofillSuggestions, )
40      IPC_MESSAGE_UNHANDLED(handled = false)
41    IPC_END_MESSAGE_MAP()
42
43    if (handled)
44      return true;
45    return TabContents::OnMessageReceived(message);
46  }
47
48 private:
49  void OnFormSubmitted(const FormData& form) {
50    if (UTF16ToASCII(form.name) == kCreateAccountFormName) {
51      std::string user_name;
52      std::string domain;
53      for (size_t i = 0; i < form.fields.size(); i++) {
54        std::string name = UTF16ToASCII(form.fields[i].name);
55        if (name == kEmailFieldName) {
56          user_name = UTF16ToASCII(form.fields[i].value);
57        } else if (name == kDomainFieldName) {
58          domain = UTF16ToASCII(form.fields[i].value);
59        }
60      }
61      if (!user_name.empty()) {
62        // We don't have password here because all password fields were
63        // stripped. Overriding TabContents::PasswordFormsFound also makes no
64        // sense because password value is always empty for account create page.
65        delegate_->OnUserCreated(user_name + "@" + domain, "");
66      }
67    }
68  }
69
70  AccountCreationViewDelegate* delegate_;
71
72  DISALLOW_COPY_AND_ASSIGN(AccountCreationTabContents);
73};
74
75///////////////////////////////////////////////////////////////////////////////
76// AccountCreationDomView, public:
77
78AccountCreationDomView::AccountCreationDomView() : delegate_(NULL) {
79}
80
81AccountCreationDomView::~AccountCreationDomView() {
82}
83
84void AccountCreationDomView::SetAccountCreationViewDelegate(
85    AccountCreationViewDelegate* delegate) {
86  delegate_ = delegate;
87}
88
89///////////////////////////////////////////////////////////////////////////////
90// AccountCreationDomView, DOMView implementation:
91
92TabContents* AccountCreationDomView::CreateTabContents(Profile* profile,
93                                                       SiteInstance* instance) {
94  return new AccountCreationTabContents(profile,
95                                        instance,
96                                        delegate_,
97                                        page_delegate_);
98}
99
100///////////////////////////////////////////////////////////////////////////////
101// AccountCreationView, public:
102
103AccountCreationView::AccountCreationView()
104    : dom_view_(new AccountCreationDomView()) {
105}
106
107AccountCreationView::~AccountCreationView() {
108}
109
110void AccountCreationView::SetAccountCreationViewDelegate(
111    AccountCreationViewDelegate* delegate) {
112  dom_view_->SetAccountCreationViewDelegate(delegate);
113}
114
115}  // namespace chromeos
116