1// Copyright (c) 2012 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/renderer/renderer_webcolorchooser_impl.h"
6
7#include "content/common/frame_messages.h"
8
9namespace content {
10
11static int GenerateColorChooserIdentifier() {
12  static int next = 0;
13  return ++next;
14}
15
16RendererWebColorChooserImpl::RendererWebColorChooserImpl(
17    RenderFrame* render_frame,
18    blink::WebColorChooserClient* client)
19    : RenderFrameObserver(render_frame),
20      identifier_(GenerateColorChooserIdentifier()),
21      client_(client) {
22}
23
24RendererWebColorChooserImpl::~RendererWebColorChooserImpl() {
25}
26
27bool RendererWebColorChooserImpl::OnMessageReceived(
28    const IPC::Message& message) {
29  bool handled = true;
30  IPC_BEGIN_MESSAGE_MAP(RendererWebColorChooserImpl, message)
31    IPC_MESSAGE_HANDLER(FrameMsg_DidChooseColorResponse,
32                        OnDidChooseColorResponse)
33    IPC_MESSAGE_HANDLER(FrameMsg_DidEndColorChooser, OnDidEndColorChooser)
34    IPC_MESSAGE_UNHANDLED(handled = false)
35  IPC_END_MESSAGE_MAP()
36  return handled;
37}
38
39void RendererWebColorChooserImpl::setSelectedColor(blink::WebColor color) {
40  Send(new FrameHostMsg_SetSelectedColorInColorChooser(
41      routing_id(), identifier_, static_cast<SkColor>(color)));
42}
43
44void RendererWebColorChooserImpl::endChooser() {
45  Send(new FrameHostMsg_EndColorChooser(routing_id(), identifier_));
46}
47
48void RendererWebColorChooserImpl::Open(
49      SkColor initial_color,
50      const std::vector<content::ColorSuggestion>& suggestions) {
51  Send(new FrameHostMsg_OpenColorChooser(routing_id(),
52                                         identifier_,
53                                         initial_color,
54                                         suggestions));
55}
56
57void RendererWebColorChooserImpl::OnDidChooseColorResponse(int color_chooser_id,
58                                                           SkColor color) {
59  DCHECK(identifier_ == color_chooser_id);
60
61  client_->didChooseColor(static_cast<blink::WebColor>(color));
62}
63
64void RendererWebColorChooserImpl::OnDidEndColorChooser(int color_chooser_id) {
65  if (identifier_ != color_chooser_id)
66    return;
67  client_->didEndChooser();
68}
69
70}  // namespace content
71