summaryrefslogtreecommitdiff
path: root/chromium/components/physical_web/webui/physical_web_base_message_handler.cc
blob: 17b089b4ff260a00d0347d24f1816e713f75202a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/physical_web/webui/physical_web_base_message_handler.h"

#include <memory>
#include <utility>

#include "base/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "components/physical_web/data_source/physical_web_data_source.h"
#include "components/physical_web/webui/physical_web_ui_constants.h"

namespace physical_web_ui {

PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler()
    : data_source_(nullptr) {}

PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() {
  if (data_source_)
    data_source_->UnregisterListener(this);
}

void PhysicalWebBaseMessageHandler::OnFound(const GURL& url) {
  PushNearbyURLs();
}

void PhysicalWebBaseMessageHandler::OnLost(const GURL& url) {
  // do nothing
}

void PhysicalWebBaseMessageHandler::OnDistanceChanged(
    const GURL& url,
    double distance_estimate) {
  // do nothing
}

void PhysicalWebBaseMessageHandler::RegisterMessages() {
  RegisterMessageCallback(
      kPhysicalWebPageLoaded,
      base::BindRepeating(
          &PhysicalWebBaseMessageHandler::HandlePhysicalWebPageLoaded,
          base::Unretained(this)));

  RegisterMessageCallback(
      kPhysicalWebItemClicked,
      base::BindRepeating(
          &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
          base::Unretained(this)));

  data_source_ = GetPhysicalWebDataSource();
  if (data_source_)
    data_source_->RegisterListener(this, physical_web::OPPORTUNISTIC);
}

void PhysicalWebBaseMessageHandler::PushNearbyURLs() {
  base::DictionaryValue results;

  std::unique_ptr<physical_web::MetadataList> metadata_list =
      GetPhysicalWebDataSource()->GetMetadataList();

  // Append new metadata at the end of the list.
  for (const auto& metadata_list_item : *metadata_list) {
    const std::string& group_id = metadata_list_item.group_id;
    if (metadata_map_.find(group_id) == metadata_map_.end()) {
      ordered_group_ids_.push_back(group_id);
      metadata_map_.insert(std::make_pair(group_id, metadata_list_item));
    }
  }

  auto metadata = std::make_unique<base::ListValue>();
  int index = 0;
  for (const auto& group_id : ordered_group_ids_) {
    auto metadata_list_item = metadata_map_[group_id];
    auto metadata_item = std::make_unique<base::DictionaryValue>();
    metadata_item->SetString(physical_web_ui::kResolvedUrl,
                             metadata_list_item.resolved_url.spec());
    metadata_item->SetString(physical_web_ui::kPageInfoIcon,
                             metadata_list_item.icon_url.spec());
    metadata_item->SetString(physical_web_ui::kPageInfoTitle,
                             metadata_list_item.title);
    metadata_item->SetString(physical_web_ui::kPageInfoDescription,
                             metadata_list_item.description);
    // Add the item index so when an item is selected, the index can be recorded
    // in a UMA histogram.
    metadata_item->SetInteger(physical_web_ui::kIndex, index);
    metadata->Append(std::move(metadata_item));
    ++index;
  }

  results.Set(physical_web_ui::kMetadata, std::move(metadata));

  // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will
  // create a list view with an item for each URL.
  CallJavaScriptFunction(physical_web_ui::kPushNearbyUrls, results);
}

void PhysicalWebBaseMessageHandler::HandlePhysicalWebPageLoaded(
    const base::ListValue* args) {
  PushNearbyURLs();
  UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay",
                             (int)ordered_group_ids_.size(), 50);
}

void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
    const base::ListValue* args) {
  int index = 0;
  if (!args->GetInteger(0, &index)) {
    DLOG(ERROR) << "Invalid selection index";
    return;
  }

  // Record the index of the selected item.
  UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
                             50);

  // Count the number of selections.
  base::RecordAction(
      base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
}

}  //  namespace physical_web_ui