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 "ash/shelf/shelf_menu_model_adapter.h"
6 
7 #include "ash/public/cpp/app_menu_constants.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "ui/base/models/simple_menu_model.h"
10 #include "ui/views/view.h"
11 
12 namespace ash {
13 
ShelfMenuModelAdapter(const std::string & app_id,std::unique_ptr<ui::SimpleMenuModel> model,views::View * menu_owner,ui::MenuSourceType source_type,base::OnceClosure on_menu_closed_callback,bool is_tablet_mode,bool for_application_menu_items)14 ShelfMenuModelAdapter::ShelfMenuModelAdapter(
15     const std::string& app_id,
16     std::unique_ptr<ui::SimpleMenuModel> model,
17     views::View* menu_owner,
18     ui::MenuSourceType source_type,
19     base::OnceClosure on_menu_closed_callback,
20     bool is_tablet_mode,
21     bool for_application_menu_items)
22     : AppMenuModelAdapter(app_id,
23                           std::move(model),
24                           menu_owner->GetWidget(),
25                           source_type,
26                           std::move(on_menu_closed_callback),
27                           is_tablet_mode),
28       menu_owner_(menu_owner),
29       for_application_menu_items_(for_application_menu_items) {}
30 
31 ShelfMenuModelAdapter::~ShelfMenuModelAdapter() = default;
32 
GetCommandIdForHistograms(int command_id)33 int ShelfMenuModelAdapter::GetCommandIdForHistograms(int command_id) {
34   if (!for_application_menu_items_)
35     return command_id;
36 
37   // Command IDs of shelf app menu items must be in the range
38   // [APP_MENU_ITEM_ID_FIRST : APP_MENU_ITEM_ID_LAST] to avoid conflicts with
39   // other IDs when reporting UMA.
40   command_id += APP_MENU_ITEM_ID_FIRST;
41   DCHECK_LE(command_id, APP_MENU_ITEM_ID_LAST);
42   return command_id;
43 }
44 
RecordHistogramOnMenuClosed()45 void ShelfMenuModelAdapter::RecordHistogramOnMenuClosed() {
46   base::TimeDelta user_journey_time = base::TimeTicks::Now() - menu_open_time();
47   // If the menu is for a ShelfButton.
48   if (!app_id().empty()) {
49     UMA_HISTOGRAM_TIMES("Apps.ContextMenuUserJourneyTime.ShelfButton",
50                         user_journey_time);
51     UMA_HISTOGRAM_ENUMERATION("Apps.ContextMenuShowSource.ShelfButton",
52                               source_type(), ui::MENU_SOURCE_TYPE_LAST);
53     if (is_tablet_mode()) {
54       UMA_HISTOGRAM_TIMES(
55           "Apps.ContextMenuUserJourneyTime.ShelfButton.TabletMode",
56           user_journey_time);
57       UMA_HISTOGRAM_ENUMERATION(
58           "Apps.ContextMenuShowSource.ShelfButton.TabletMode", source_type(),
59           ui::MenuSourceType::MENU_SOURCE_TYPE_LAST);
60     } else {
61       UMA_HISTOGRAM_TIMES(
62           "Apps.ContextMenuUserJourneyTime.ShelfButton.ClamshellMode",
63           user_journey_time);
64       UMA_HISTOGRAM_ENUMERATION(
65           "Apps.ContextMenuShowSource.ShelfButton.ClamshellMode", source_type(),
66           ui::MenuSourceType::MENU_SOURCE_TYPE_LAST);
67     }
68     return;
69   }
70 
71   UMA_HISTOGRAM_TIMES("Apps.ContextMenuUserJourneyTime.Shelf",
72                       user_journey_time);
73   UMA_HISTOGRAM_ENUMERATION("Apps.ContextMenuShowSource.Shelf", source_type(),
74                             ui::MENU_SOURCE_TYPE_LAST);
75   if (is_tablet_mode()) {
76     UMA_HISTOGRAM_TIMES("Apps.ContextMenuUserJourneyTime.Shelf.TabletMode",
77                         user_journey_time);
78     UMA_HISTOGRAM_ENUMERATION("Apps.ContextMenuShowSource.Shelf.TabletMode",
79                               source_type(),
80                               ui::MenuSourceType::MENU_SOURCE_TYPE_LAST);
81   } else {
82     UMA_HISTOGRAM_TIMES("Apps.ContextMenuUserJourneyTime.Shelf.ClamshellMode",
83                         user_journey_time);
84     UMA_HISTOGRAM_ENUMERATION("Apps.ContextMenuShowSource.Shelf.ClamshellMode",
85                               source_type(),
86                               ui::MenuSourceType::MENU_SOURCE_TYPE_LAST);
87   }
88 }
89 
IsShowingMenuForView(const views::View & view) const90 bool ShelfMenuModelAdapter::IsShowingMenuForView(
91     const views::View& view) const {
92   return IsShowingMenu() && menu_owner_ == &view;
93 }
94 
95 }  // namespace ash
96