summaryrefslogtreecommitdiffstats
path: root/chrome/browser/dom_ui/html_dialog_contents.cc
blob: 0b4a7a96034a7ffe73c72bded94eda8303afc7cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/dom_ui/html_dialog_contents.h"

#include "chrome/browser/render_view_host.h"

const char kGearsScheme[] = "gears";

HtmlDialogContents::HtmlDialogContents(Profile* profile,
                                       SiteInstance* instance,
                                       RenderViewHostFactory* rvf)
    : DOMUIHost(profile, instance, rvf),
      delegate_(NULL) {
  set_type(TAB_CONTENTS_HTML_DIALOG);
}

HtmlDialogContents::~HtmlDialogContents() {
}

void HtmlDialogContents::Init(HtmlDialogContentsDelegate* delegate) {
  delegate_ = delegate;

  std::string dialog_args;
  if (delegate_)
    dialog_args = delegate_->GetDialogArgs();

  DCHECK(render_view_host());
  render_view_host()->SetDOMUIProperty("dialogArguments", dialog_args);
}

////////////////////////////////////////////////////////////////////////////////
// DOMUIHost implementation:

void HtmlDialogContents::AttachMessageHandlers() {
  // Hook up the javascript function calls, also known as chrome.send("foo")
  // calls in the HTML, to the actual C++ functions.
  RegisterMessageCallback("DialogClose",
      NewCallback(this, &HtmlDialogContents::OnDialogClosed));
}

// static
bool HtmlDialogContents::IsHtmlDialogUrl(const GURL& url) {
  return url.SchemeIs(kGearsScheme);
}

////////////////////////////////////////////////////////////////////////////////
// Private:

// Helper function to read the JSON string from the Value parameter.
std::string GetJsonResponse(const Value* content) {
  if (!content || !content->IsType(Value::TYPE_LIST))  {
    NOTREACHED();
    return "";
  }
  const ListValue* args = static_cast<const ListValue*>(content);
  if (args->GetSize() != 1) {
    NOTREACHED();
    return "";
  }

  std::string result;
  Value* value = NULL;
  if (!args->Get(0, &value) || !value->GetAsString(&result)) {
    NOTREACHED();
    return "";
  }

  return result;
}

void HtmlDialogContents::OnDialogClosed(const Value* content) {
  if (delegate_)
    delegate_->OnDialogClosed(GetJsonResponse(content));
}