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 <string>
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/profiles/profile_manager.h"
11#include "chrome/browser/ui/browser_finder.h"
12#include "chrome/browser/ui/tabs/tab_strip_model.h"
13#include "content/public/browser/web_contents.h"
14#include "url/gurl.h"
15
16namespace {
17
18GURL GetTargetTabUrl(int session_id, int index) {
19  Browser* browser = chrome::FindBrowserWithID(session_id);
20  // Sanity checks.
21  if (!browser || index >= browser->tab_strip_model()->count())
22    return GURL();
23
24  if (index >= 0) {
25    content::WebContents* target_tab =
26        browser->tab_strip_model()->GetWebContentsAt(index);
27    if (target_tab)
28      return target_tab->GetURL();
29  }
30
31  return GURL();
32}
33
34}  // namespace
35
36namespace chrome {
37
38extern const char kAppLauncherCategoryTag[] = "AppLauncher";
39
40void ShowFeedbackPage(Browser* browser,
41                      const std::string& description_template,
42                      const std::string& category_tag) {
43  GURL page_url;
44  if (browser) {
45    page_url = GetTargetTabUrl(browser->session_id().id(),
46                               browser->tab_strip_model()->active_index());
47  }
48
49  Profile* profile = NULL;
50  if (browser) {
51    profile = browser->profile();
52  } else {
53    profile = ProfileManager::GetLastUsedProfileAllowedByPolicy();
54  }
55  if (!profile) {
56    LOG(ERROR) << "Cannot invoke feedback: No profile found!";
57    return;
58  }
59
60  // We do not want to launch on an OTR profile.
61  profile = profile->GetOriginalProfile();
62  DCHECK(profile);
63
64  extensions::FeedbackPrivateAPI* api =
65      extensions::FeedbackPrivateAPI::GetFactoryInstance()->Get(profile);
66
67  api->RequestFeedback(description_template,
68                       category_tag,
69                       page_url);
70}
71
72}  // namespace chrome
73