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 "chrome/browser/infobars/infobar_extension_api.h"
6
7#include "base/strings/string_number_conversions.h"
8#include "base/strings/string_util.h"
9#include "base/values.h"
10#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
11#include "chrome/browser/extensions/extension_host.h"
12#include "chrome/browser/extensions/extension_infobar_delegate.h"
13#include "chrome/browser/extensions/extension_tab_util.h"
14#include "chrome/browser/extensions/window_controller.h"
15#include "chrome/browser/infobars/confirm_infobar_delegate.h"
16#include "chrome/browser/infobars/infobar_service.h"
17#include "chrome/browser/ui/browser.h"
18#include "chrome/common/extensions/extension.h"
19#include "chrome/common/url_constants.h"
20#include "content/public/browser/web_contents.h"
21#include "extensions/common/error_utils.h"
22#include "grit/generated_resources.h"
23
24using extensions::Extension;
25
26namespace {
27
28const char kHtmlPath[] = "path";
29const char kTabId[] = "tabId";
30const char kHeight[] = "height";
31
32}  // namespace
33
34bool InfobarsShowFunction::RunImpl() {
35  DictionaryValue* args;
36  EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args));
37
38  int tab_id;
39  EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kTabId, &tab_id));
40
41  std::string html_path;
42  EXTENSION_FUNCTION_VALIDATE(args->GetString(kHtmlPath, &html_path));
43
44  int height = 0;
45  if (args->HasKey(kHeight))
46    EXTENSION_FUNCTION_VALIDATE(args->GetInteger(kHeight, &height));
47
48  const Extension* extension = GetExtension();
49  GURL url = extension->GetResourceURL(extension->url(), html_path);
50
51  Browser* browser = NULL;
52  content::WebContents* web_contents = NULL;
53  if (!ExtensionTabUtil::GetTabById(
54      tab_id,
55      profile(),
56      include_incognito(),
57      &browser,
58      NULL,
59      &web_contents,
60      NULL)) {
61    error_ = extensions::ErrorUtils::FormatErrorMessage(
62        extensions::tabs_constants::kTabNotFoundError,
63        base::IntToString(tab_id));
64    return false;
65  }
66
67  ExtensionInfoBarDelegate::Create(
68      InfoBarService::FromWebContents(web_contents), browser, GetExtension(),
69      url, height);
70
71  // TODO(finnur): Return the actual DOMWindow object. Bug 26463.
72  DCHECK(browser->extension_window_controller());
73  SetResult(browser->extension_window_controller()->CreateWindowValue());
74
75  return true;
76}
77