Home | History | Annotate | Download | only in adview
      1 // Copyright 2013 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/guestview/adview/adview_guest.h"
      6 
      7 #include "base/strings/string_util.h"
      8 #include "chrome/browser/guestview/adview/adview_constants.h"
      9 #include "chrome/browser/guestview/guestview_constants.h"
     10 #include "content/public/browser/web_contents.h"
     11 #include "net/base/net_errors.h"
     12 
     13 using content::WebContents;
     14 
     15 AdViewGuest::AdViewGuest(WebContents* guest_web_contents)
     16     : GuestView(guest_web_contents),
     17       WebContentsObserver(guest_web_contents) {
     18 }
     19 
     20 // static
     21 AdViewGuest* AdViewGuest::From(int embedder_process_id,
     22                                int guest_instance_id) {
     23   GuestView* guest = GuestView::From(embedder_process_id, guest_instance_id);
     24   if (!guest)
     25     return NULL;
     26   return guest->AsAdView();
     27 }
     28 
     29 GuestView::Type AdViewGuest::GetViewType() const {
     30   return GuestView::ADVIEW;
     31 }
     32 
     33 WebViewGuest* AdViewGuest::AsWebView() {
     34   return NULL;
     35 }
     36 
     37 AdViewGuest* AdViewGuest::AsAdView() {
     38   return this;
     39 }
     40 
     41 AdViewGuest::~AdViewGuest() {
     42 }
     43 
     44 void AdViewGuest::DidCommitProvisionalLoadForFrame(
     45     int64 frame_id,
     46     bool is_main_frame,
     47     const GURL& url,
     48     content::PageTransition transition_type,
     49     content::RenderViewHost* render_view_host) {
     50   scoped_ptr<DictionaryValue> args(new DictionaryValue());
     51   args->SetString(guestview::kUrl, url.spec());
     52   args->SetBoolean(guestview::kIsTopLevel, is_main_frame);
     53   DispatchEvent(new GuestView::Event(adview::kEventLoadCommit, args.Pass()));
     54 }
     55 
     56 void AdViewGuest::DidFailProvisionalLoad(
     57     int64 frame_id,
     58     bool is_main_frame,
     59     const GURL& validated_url,
     60     int error_code,
     61     const string16& error_description,
     62     content::RenderViewHost* render_view_host) {
     63   // Translate the |error_code| into an error string.
     64   std::string error_type;
     65   RemoveChars(net::ErrorToString(error_code), "net::", &error_type);
     66 
     67   scoped_ptr<DictionaryValue> args(new DictionaryValue());
     68   args->SetBoolean(guestview::kIsTopLevel, is_main_frame);
     69   args->SetString(guestview::kUrl, validated_url.spec());
     70   args->SetString(guestview::kReason, error_type);
     71   DispatchEvent(new GuestView::Event(adview::kEventLoadAbort, args.Pass()));
     72 }
     73