Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / renderer_context_menu / render_view_context_menu_browsertest_util.cc
blobcacade0511acd56b9f994a898aeeff39800c167b
1 // Copyright 2014 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.
5 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/test_utils.h"
17 ContextMenuNotificationObserver::ContextMenuNotificationObserver(
18 int command_to_execute)
19 : command_to_execute_(command_to_execute) {
20 registrar_.Add(this,
21 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
22 content::NotificationService::AllSources());
25 ContextMenuNotificationObserver::~ContextMenuNotificationObserver() {
28 void ContextMenuNotificationObserver::Observe(
29 int type,
30 const content::NotificationSource& source,
31 const content::NotificationDetails& details) {
32 switch (type) {
33 case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN: {
34 RenderViewContextMenu* context_menu =
35 content::Source<RenderViewContextMenu>(source).ptr();
36 base::ThreadTaskRunnerHandle::Get()->PostTask(
37 FROM_HERE,
38 base::Bind(&ContextMenuNotificationObserver::ExecuteCommand,
39 base::Unretained(this), context_menu));
40 break;
43 default:
44 NOTREACHED();
48 void ContextMenuNotificationObserver::ExecuteCommand(
49 RenderViewContextMenu* context_menu) {
50 context_menu->ExecuteCommand(command_to_execute_, 0);
51 context_menu->Cancel();
54 ContextMenuWaiter::ContextMenuWaiter(const content::NotificationSource& source)
55 : menu_visible_(false) {
56 registrar_.Add(this, chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
57 content::NotificationService::AllSources());
60 ContextMenuWaiter::~ContextMenuWaiter() {
63 void ContextMenuWaiter::Observe(int type,
64 const content::NotificationSource& source,
65 const content::NotificationDetails& details) {
66 switch (type) {
67 case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN: {
68 menu_visible_ = true;
69 RenderViewContextMenu* context_menu =
70 content::Source<RenderViewContextMenu>(source).ptr();
71 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE, base::Bind(&ContextMenuWaiter::Cancel,
73 base::Unretained(this), context_menu));
74 break;
77 default:
78 NOTREACHED();
82 void ContextMenuWaiter::WaitForMenuOpenAndClose() {
83 content::WindowedNotificationObserver menu_observer(
84 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
85 content::NotificationService::AllSources());
86 if (!menu_visible_)
87 menu_observer.Wait();
89 content::RunAllPendingInMessageLoop();
90 menu_visible_ = false;
93 content::ContextMenuParams& ContextMenuWaiter::params() {
94 return params_;
97 void ContextMenuWaiter::Cancel(RenderViewContextMenu* context_menu) {
98 params_ = context_menu->params();
99 context_menu->Cancel();