10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
20529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Use of this source code is governed by a BSD-style license that can be
30529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// found in the LICENSE file.
40529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/renderer/net/net_error_page_controller.h"
60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/strings/string_piece.h"
80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/renderer/net/net_error_helper.h"
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/renderer/render_frame.h"
100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "gin/handle.h"
110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "gin/object_template_builder.h"
120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "third_party/WebKit/public/web/WebKit.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "third_party/WebKit/public/web/WebLocalFrame.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
150529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochgin::WrapperInfo NetErrorPageController::kWrapperInfo = {
160529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    gin::kEmbedderNativeGin};
170529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// static
190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid NetErrorPageController::Install(content::RenderFrame* render_frame) {
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Isolate* isolate = blink::mainThreadIsolate();
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::HandleScope handle_scope(isolate);
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Context> context =
230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      render_frame->GetWebFrame()->mainWorldScriptContext();
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (context.IsEmpty())
250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return;
260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Context::Scope context_scope(context);
280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  gin::Handle<NetErrorPageController> controller = gin::CreateHandle(
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      isolate, new NetErrorPageController(render_frame));
315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  if (controller.IsEmpty())
325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    return;
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  v8::Handle<v8::Object> global = context->Global();
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  global->Set(gin::StringToV8(isolate, "errorPageController"),
360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch              controller.ToV8());
370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool NetErrorPageController::LoadStaleButtonClick() {
400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!render_frame())
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NetErrorHelper* net_error_helper =
440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      content::RenderFrameObserverTracker<NetErrorHelper>::Get(render_frame());
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DCHECK(net_error_helper);
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  net_error_helper->LoadStaleButtonPressed();
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return true;
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool NetErrorPageController::ReloadButtonClick() {
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!render_frame())
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NetErrorHelper* net_error_helper =
560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      content::RenderFrameObserverTracker<NetErrorHelper>::Get(render_frame());
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DCHECK(net_error_helper);
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  net_error_helper->ReloadButtonPressed();
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return true;
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool NetErrorPageController::DetailsButtonClick() {
640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  if (!render_frame())
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NetErrorHelper* net_error_helper =
680529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      content::RenderFrameObserverTracker<NetErrorHelper>::Get(render_frame());
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  DCHECK(net_error_helper);
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  net_error_helper->MoreButtonPressed();
710529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
720529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return true;
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
75010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool NetErrorPageController::TrackClick(const gin::Arguments& args) {
76010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!render_frame())
77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return false;
78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!args.PeekNext()->IsInt32())
80010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return false;
81010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
82010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  NetErrorHelper* net_error_helper =
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      content::RenderFrameObserverTracker<NetErrorHelper>::Get(render_frame());
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(net_error_helper);
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  net_error_helper->TrackClick(args.PeekNext()->Int32Value());
86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  return true;
87010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
88010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
890529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochNetErrorPageController::NetErrorPageController(
900529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    content::RenderFrame* render_frame) : RenderFrameObserver(render_frame) {}
910529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
920529e5d033099cbfc42635f6f6183833b09dff6eBen MurdochNetErrorPageController::~NetErrorPageController() {}
930529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
940529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochgin::ObjectTemplateBuilder NetErrorPageController::GetObjectTemplateBuilder(
950529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    v8::Isolate* isolate) {
960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return gin::Wrappable<NetErrorPageController>::GetObjectTemplateBuilder(
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch             isolate)
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      .SetMethod("loadStaleButtonClick",
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 &NetErrorPageController::LoadStaleButtonClick)
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      .SetMethod("reloadButtonClick",
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                 &NetErrorPageController::ReloadButtonClick)
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      .SetMethod("detailsButtonClick",
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 &NetErrorPageController::DetailsButtonClick)
104010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      .SetMethod("trackClick",
105010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                 &NetErrorPageController::TrackClick);
1060529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1070529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid NetErrorPageController::OnDestruct() {}
109