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.
5 #include "chrome/renderer/net/net_error_page_controller.h"
7 #include "base/strings/string_piece.h"
8 #include "chrome/renderer/net/net_error_helper.h"
9 #include "content/public/renderer/render_frame.h"
10 #include "gin/handle.h"
11 #include "gin/object_template_builder.h"
12 #include "third_party/WebKit/public/web/WebKit.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
15 gin::WrapperInfo
NetErrorPageController::kWrapperInfo
= {
16 gin::kEmbedderNativeGin
};
19 void NetErrorPageController::Install(content::RenderFrame
* render_frame
) {
20 v8::Isolate
* isolate
= blink::mainThreadIsolate();
21 v8::HandleScope
handle_scope(isolate
);
22 v8::Local
<v8::Context
> context
=
23 render_frame
->GetWebFrame()->mainWorldScriptContext();
24 if (context
.IsEmpty())
27 v8::Context::Scope
context_scope(context
);
29 gin::Handle
<NetErrorPageController
> controller
= gin::CreateHandle(
30 isolate
, new NetErrorPageController(render_frame
));
31 if (controller
.IsEmpty())
34 v8::Local
<v8::Object
> global
= context
->Global();
35 global
->Set(gin::StringToV8(isolate
, "errorPageController"),
39 bool NetErrorPageController::ShowSavedCopyButtonClick() {
40 return ButtonClick(error_page::NetErrorHelperCore::SHOW_SAVED_COPY_BUTTON
);
43 bool NetErrorPageController::ReloadButtonClick() {
44 return ButtonClick(error_page::NetErrorHelperCore::RELOAD_BUTTON
);
47 bool NetErrorPageController::DetailsButtonClick() {
48 return ButtonClick(error_page::NetErrorHelperCore::MORE_BUTTON
);
51 bool NetErrorPageController::TrackEasterEgg() {
52 return ButtonClick(error_page::NetErrorHelperCore::EASTER_EGG
);
55 bool NetErrorPageController::TrackCachedCopyButtonClick(bool is_default_label
) {
56 return is_default_label
?
57 ButtonClick(error_page::NetErrorHelperCore::SHOW_CACHED_PAGE_BUTTON
) :
58 ButtonClick(error_page::NetErrorHelperCore::SHOW_CACHED_COPY_BUTTON
);
61 bool NetErrorPageController::TrackClick(const gin::Arguments
& args
) {
65 if (args
.PeekNext().IsEmpty() || !args
.PeekNext()->IsInt32())
68 NetErrorHelper
* net_error_helper
=
69 content::RenderFrameObserverTracker
<NetErrorHelper
>::Get(render_frame());
70 DCHECK(net_error_helper
);
71 net_error_helper
->TrackClick(args
.PeekNext()->Int32Value());
75 bool NetErrorPageController::ButtonClick(
76 error_page::NetErrorHelperCore::Button button
) {
80 NetErrorHelper
* net_error_helper
=
81 content::RenderFrameObserverTracker
<NetErrorHelper
>::Get(render_frame());
82 DCHECK(net_error_helper
);
84 net_error_helper
->ButtonPressed(button
);
89 NetErrorPageController::NetErrorPageController(
90 content::RenderFrame
* render_frame
) : RenderFrameObserver(render_frame
) {}
92 NetErrorPageController::~NetErrorPageController() {}
94 gin::ObjectTemplateBuilder
NetErrorPageController::GetObjectTemplateBuilder(
95 v8::Isolate
* isolate
) {
96 return gin::Wrappable
<NetErrorPageController
>::GetObjectTemplateBuilder(
98 .SetMethod("showSavedCopyButtonClick",
99 &NetErrorPageController::ShowSavedCopyButtonClick
)
100 .SetMethod("reloadButtonClick",
101 &NetErrorPageController::ReloadButtonClick
)
102 .SetMethod("detailsButtonClick",
103 &NetErrorPageController::DetailsButtonClick
)
104 .SetMethod("trackClick",
105 &NetErrorPageController::TrackClick
)
106 .SetMethod("trackEasterEgg",
107 &NetErrorPageController::TrackEasterEgg
)
108 .SetMethod("trackCachedCopyButtonClick",
109 &NetErrorPageController::TrackCachedCopyButtonClick
);
112 void NetErrorPageController::OnDestruct() {}