1 // Copyright 2015 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/banners/app_banner_client.h"
7 #include "chrome/common/render_messages.h"
8 #include "ipc/ipc_message.h"
9 #include "third_party/WebKit/public/platform/WebString.h"
11 using blink::WebString
;
13 AppBannerClient::AppBannerClient(content::RenderFrame
* render_frame
)
14 : content::RenderFrameObserver(render_frame
) {
17 AppBannerClient::~AppBannerClient() {
20 void AppBannerClient::OnDestruct() {
21 // The RenderFrameImpl destructor calls RenderFrameObserver::OnDestruct, which
22 // deletes this object. However, RenderFrameImpl also holds a scoped pointer
23 // to AppBannerClient. Override OnDestruct to be a no-op to stop a double call
24 // to the destructor and a renderer crash.
27 bool AppBannerClient::OnMessageReceived(const IPC::Message
& message
) {
29 IPC_BEGIN_MESSAGE_MAP(AppBannerClient
, message
)
30 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerAccepted
, OnBannerAccepted
);
31 IPC_MESSAGE_HANDLER(ChromeViewMsg_AppBannerDismissed
, OnBannerDismissed
);
32 IPC_MESSAGE_UNHANDLED(handled
= false)
37 void AppBannerClient::registerBannerCallbacks(
39 blink::WebAppBannerCallbacks
* callbacks
) {
40 banner_callbacks_
.AddWithID(callbacks
, request_id
);
43 void AppBannerClient::showAppBanner(int request_id
) {
44 Send(new ChromeViewHostMsg_RequestShowAppBanner(routing_id(), request_id
));
47 void AppBannerClient::ResolveEvent(
49 const std::string
& platform
,
50 const blink::WebAppBannerPromptResult::Outcome
& outcome
) {
51 blink::WebAppBannerCallbacks
* callbacks
=
52 banner_callbacks_
.Lookup(request_id
);
56 scoped_ptr
<blink::WebAppBannerPromptResult
> result(
57 new blink::WebAppBannerPromptResult(
58 blink::WebString::fromUTF8(platform
),
60 callbacks
->onSuccess(result
.release());
61 banner_callbacks_
.Remove(request_id
);
64 void AppBannerClient::OnBannerAccepted(int request_id
,
65 const std::string
& platform
) {
66 ResolveEvent(request_id
, platform
,
67 blink::WebAppBannerPromptResult::Outcome::Accepted
);
70 void AppBannerClient::OnBannerDismissed(int request_id
) {
71 ResolveEvent(request_id
, "",
72 blink::WebAppBannerPromptResult::Outcome::Dismissed
);