summaryrefslogtreecommitdiffstats
path: root/chrome/browser/guestview/adview/adview_guest.cc
blob: 44acbd9eeeee7782144d5c040415f1e90ae61255 (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
// Copyright 2013 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 "chrome/browser/guestview/adview/adview_guest.h"

#include "base/strings/string_util.h"
#include "chrome/browser/guestview/adview/adview_constants.h"
#include "chrome/browser/guestview/guestview_constants.h"
#include "content/public/browser/web_contents.h"
#include "net/base/net_errors.h"

using content::WebContents;

AdViewGuest::AdViewGuest(WebContents* guest_web_contents,
                         const std::string& extension_id)
    : GuestView(guest_web_contents, extension_id),
      WebContentsObserver(guest_web_contents) {
}

// static
AdViewGuest* AdViewGuest::From(int embedder_process_id,
                               int guest_instance_id) {
  GuestView* guest = GuestView::From(embedder_process_id, guest_instance_id);
  if (!guest)
    return NULL;
  return guest->AsAdView();
}

GuestView::Type AdViewGuest::GetViewType() const {
  return GuestView::ADVIEW;
}

WebViewGuest* AdViewGuest::AsWebView() {
  return NULL;
}

AdViewGuest* AdViewGuest::AsAdView() {
  return this;
}

AdViewGuest::~AdViewGuest() {
}

void AdViewGuest::DidCommitProvisionalLoadForFrame(
    int64 frame_id,
    const string16& frame_unique_name,
    bool is_main_frame,
    const GURL& url,
    content::PageTransition transition_type,
    content::RenderViewHost* render_view_host) {
  scoped_ptr<DictionaryValue> args(new DictionaryValue());
  args->SetString(guestview::kUrl, url.spec());
  args->SetBoolean(guestview::kIsTopLevel, is_main_frame);
  DispatchEvent(new GuestView::Event(adview::kEventLoadCommit, args.Pass()));
}

void AdViewGuest::DidFailProvisionalLoad(
    int64 frame_id,
    const string16& frame_unique_name,
    bool is_main_frame,
    const GURL& validated_url,
    int error_code,
    const string16& error_description,
    content::RenderViewHost* render_view_host) {
  // Translate the |error_code| into an error string.
  std::string error_type;
  RemoveChars(net::ErrorToString(error_code), "net::", &error_type);

  scoped_ptr<DictionaryValue> args(new DictionaryValue());
  args->SetBoolean(guestview::kIsTopLevel, is_main_frame);
  args->SetString(guestview::kUrl, validated_url.spec());
  args->SetString(guestview::kReason, error_type);
  DispatchEvent(new GuestView::Event(adview::kEventLoadAbort, args.Pass()));
}