summaryrefslogtreecommitdiff
path: root/chromium/extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.cc
blob: ce68bdb589fe9d549331f58096c8f787c88f0963 (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
// Copyright 2015 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 "extensions/browser/guest_view/mime_handler_view/test_mime_handler_view_guest.h"

#include "base/time/time.h"
#include "content/public/test/test_utils.h"

using guest_view::GuestViewBase;

namespace extensions {

TestMimeHandlerViewGuest::TestMimeHandlerViewGuest(
    content::WebContents* owner_web_contents)
    : MimeHandlerViewGuest(owner_web_contents),
      weak_ptr_factory_(this) {}

TestMimeHandlerViewGuest::~TestMimeHandlerViewGuest() {}

// static
GuestViewBase* TestMimeHandlerViewGuest::Create(
    content::WebContents* owner_web_contents) {
  return new TestMimeHandlerViewGuest(owner_web_contents);
}

// static
void TestMimeHandlerViewGuest::DelayNextCreateWebContents(int delay) {
  TestMimeHandlerViewGuest::delay_ = delay;
}

void TestMimeHandlerViewGuest::WaitForGuestAttached() {
  if (attached())
    return;
  created_message_loop_runner_ = new content::MessageLoopRunner;
  created_message_loop_runner_->Run();
}

void TestMimeHandlerViewGuest::CreateWebContents(
    const base::DictionaryValue& create_params,
    const WebContentsCreatedCallback& callback) {
  // Delay the creation of the guest's WebContents if |delay_| is set.
  if (delay_) {
    auto delta = base::TimeDelta::FromMilliseconds(
        delay_);
    scoped_ptr<base::DictionaryValue> params(create_params.DeepCopy());
    content::BrowserThread::PostDelayedTask(
        content::BrowserThread::UI,
        FROM_HERE,
        base::Bind(&TestMimeHandlerViewGuest::CallBaseCreateWebContents,
                   weak_ptr_factory_.GetWeakPtr(),
                   base::Passed(&params),
                   callback),
        delta);

    // Reset the delay for the next creation.
    delay_ = 0;
    return;
  }

  MimeHandlerViewGuest::CreateWebContents(create_params, callback);
}

void TestMimeHandlerViewGuest::DidAttachToEmbedder() {
  MimeHandlerViewGuest::DidAttachToEmbedder();
  if (created_message_loop_runner_.get())
    created_message_loop_runner_->Quit();
}

void TestMimeHandlerViewGuest::CallBaseCreateWebContents(
    scoped_ptr<base::DictionaryValue> create_params,
    const WebContentsCreatedCallback& callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  MimeHandlerViewGuest::CreateWebContents(*create_params.get(), callback);
}

// static
int TestMimeHandlerViewGuest::delay_ = 0;

}  // namespace extensions