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.
4
5#include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop.h"
9#include "chrome/app/chrome_command_ids.h"
10#include "chrome/browser/chrome_notification_types.h"
11#include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
12#include "content/public/browser/notification_service.h"
13#include "content/public/test/test_utils.h"
14
15ContextMenuNotificationObserver::ContextMenuNotificationObserver(
16    int command_to_execute)
17    : command_to_execute_(command_to_execute) {
18  registrar_.Add(this,
19                 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
20                 content::NotificationService::AllSources());
21}
22
23ContextMenuNotificationObserver::~ContextMenuNotificationObserver() {
24}
25
26void ContextMenuNotificationObserver::Observe(
27    int type,
28    const content::NotificationSource& source,
29    const content::NotificationDetails& details) {
30  switch (type) {
31    case chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN: {
32      RenderViewContextMenu* context_menu =
33          content::Source<RenderViewContextMenu>(source).ptr();
34      base::MessageLoop::current()->PostTask(
35          FROM_HERE,
36          base::Bind(&ContextMenuNotificationObserver::ExecuteCommand,
37                     base::Unretained(this),
38                     context_menu));
39      break;
40    }
41
42    default:
43      NOTREACHED();
44  }
45}
46
47void ContextMenuNotificationObserver::ExecuteCommand(
48    RenderViewContextMenu* context_menu) {
49  context_menu->ExecuteCommand(command_to_execute_, 0);
50  context_menu->Cancel();
51}
52
53SaveLinkAsContextMenuObserver::SaveLinkAsContextMenuObserver(
54    const content::NotificationSource& source)
55    : ContextMenuNotificationObserver(IDC_CONTENT_CONTEXT_SAVELINKAS),
56      menu_visible_(false) {
57}
58
59SaveLinkAsContextMenuObserver::~SaveLinkAsContextMenuObserver() {
60}
61
62void SaveLinkAsContextMenuObserver::Observe(
63    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::MessageLoop::current()->PostTask(
72          FROM_HERE,
73          base::Bind(&SaveLinkAsContextMenuObserver::Cancel,
74                     base::Unretained(this),
75                     context_menu));
76      break;
77    }
78
79    default:
80      NOTREACHED();
81  }
82}
83
84void SaveLinkAsContextMenuObserver::WaitForMenu() {
85  content::WindowedNotificationObserver menu_observer(
86      chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN,
87      content::NotificationService::AllSources());
88  if (!menu_visible_)
89    menu_observer.Wait();
90  menu_visible_ = false;
91}
92
93base::string16 SaveLinkAsContextMenuObserver::GetSuggestedFilename() {
94  return params_.suggested_filename;
95}
96
97void SaveLinkAsContextMenuObserver::Cancel(
98    RenderViewContextMenu* context_menu) {
99  params_ = context_menu->params();
100  context_menu->Cancel();
101}
102