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