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.
4
5#include "content/shell/renderer/test_runner/mock_web_push_client.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9#include "third_party/WebKit/public/platform/WebPushError.h"
10#include "third_party/WebKit/public/platform/WebPushRegistration.h"
11#include "third_party/WebKit/public/platform/WebString.h"
12
13using blink::WebString;
14
15namespace content {
16
17MockWebPushClient::MockWebPushClient()
18    : error_message_(
19          "Registration failed (default mock client error message)") {
20}
21
22MockWebPushClient::~MockWebPushClient() {}
23
24void MockWebPushClient::SetMockSuccessValues(
25    const std::string& end_point, const std::string& registration_id) {
26  end_point_ = end_point;
27  registration_id_ = registration_id;
28  error_message_ = "";
29}
30
31void MockWebPushClient::SetMockErrorValues(const std::string& message) {
32  end_point_ = "";
33  registration_id_ = "";
34  error_message_ = message;
35}
36
37void MockWebPushClient::registerPushMessaging(
38    const WebString& sender_id,
39    blink::WebPushRegistrationCallbacks* callbacks) {
40  registerPushMessaging(sender_id, callbacks, NULL);
41}
42
43void MockWebPushClient::registerPushMessaging(
44    const WebString& sender_id,
45    blink::WebPushRegistrationCallbacks* callbacks,
46    blink::WebServiceWorkerProvider* service_worker_provider) {
47  DCHECK(callbacks);
48
49  if (!error_message_.empty()) {
50    scoped_ptr<blink::WebPushError> error(
51        new blink::WebPushError(blink::WebPushError::ErrorTypeAbort,
52                                WebString::fromUTF8(error_message_)));
53    callbacks->onError(error.release());
54  } else {
55    DCHECK(!end_point_.empty() && !registration_id_.empty());
56
57    scoped_ptr<blink::WebPushRegistration> registration(
58        new blink::WebPushRegistration(WebString::fromUTF8(end_point_),
59                                       WebString::fromUTF8(registration_id_)));
60    callbacks->onSuccess(registration.release());
61  }
62
63  delete callbacks;
64}
65
66}  // namespace content
67