1 // Copyright 2018 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 "components/autofill_assistant/browser/actions/show_details_action.h"
6 
7 #include <memory>
8 #include <utility>
9 #include <vector>
10 
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "components/autofill_assistant/browser/actions/action_delegate.h"
14 #include "components/autofill_assistant/browser/details.h"
15 #include "components/strings/grit/components_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
17 
18 namespace autofill_assistant {
19 
ShowDetailsAction(ActionDelegate * delegate,const ActionProto & proto)20 ShowDetailsAction::ShowDetailsAction(ActionDelegate* delegate,
21                                      const ActionProto& proto)
22     : Action(delegate, proto) {
23   DCHECK(proto_.has_show_details());
24 }
25 
~ShowDetailsAction()26 ShowDetailsAction::~ShowDetailsAction() {}
27 
InternalProcessAction(ProcessActionCallback callback)28 void ShowDetailsAction::InternalProcessAction(ProcessActionCallback callback) {
29   std::unique_ptr<Details> details = nullptr;
30   bool details_valid = true;
31 
32   switch (proto_.show_details().data_to_show_case()) {
33     case ShowDetailsProto::DataToShowCase::kDetails:
34       details = std::make_unique<Details>();
35       details_valid =
36           Details::UpdateFromProto(proto_.show_details(), details.get());
37       break;
38     case ShowDetailsProto::DataToShowCase::kContactDetails:
39       details = std::make_unique<Details>();
40       details_valid = Details::UpdateFromContactDetails(
41           proto_.show_details(), delegate_->GetUserData(), details.get());
42       break;
43     case ShowDetailsProto::DataToShowCase::kShippingAddress:
44       details = std::make_unique<Details>();
45       details_valid = Details::UpdateFromShippingAddress(
46           proto_.show_details(), delegate_->GetUserData(), details.get());
47       break;
48     case ShowDetailsProto::DataToShowCase::kCreditCard:
49       details = std::make_unique<Details>();
50       details_valid = Details::UpdateFromSelectedCreditCard(
51           proto_.show_details(), delegate_->GetUserData(), details.get());
52       break;
53     case ShowDetailsProto::DataToShowCase::DATA_TO_SHOW_NOT_SET:
54       // Clear Details. Calling SetDetails with nullptr clears the details.
55       break;
56   }
57 
58   if (!details_valid) {
59     VLOG(1) << "Failed to fill the details";
60     UpdateProcessedAction(INVALID_ACTION);
61   } else {
62     delegate_->SetDetails(std::move(details));
63     UpdateProcessedAction(ACTION_APPLIED);
64   }
65 
66   std::move(callback).Run(std::move(processed_action_proto_));
67 }
68 }  // namespace autofill_assistant
69