1// Copyright (c) 2011 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/ui/webui/html_dialog_tab_contents_delegate.h"
6
7#include "chrome/browser/profiles/profile.h"
8#include "chrome/browser/tabs/tab_strip_model.h"
9#include "chrome/browser/ui/browser.h"
10#include "chrome/browser/ui/browser_navigator.h"
11#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
12#include "content/browser/tab_contents/tab_contents.h"
13
14// Incognito profiles are not long-lived, so we always want to store a
15// non-incognito profile.
16//
17// TODO(akalin): Should we make it so that we have a default incognito
18// profile that's long-lived?  Of course, we'd still have to clear it out
19// when all incognito browsers close.
20HtmlDialogTabContentsDelegate::HtmlDialogTabContentsDelegate(Profile* profile)
21    : profile_(profile->GetOriginalProfile()) {}
22
23HtmlDialogTabContentsDelegate::~HtmlDialogTabContentsDelegate() {}
24
25Profile* HtmlDialogTabContentsDelegate::profile() const { return profile_; }
26
27void HtmlDialogTabContentsDelegate::Detach() {
28  profile_ = NULL;
29}
30
31void HtmlDialogTabContentsDelegate::OpenURLFromTab(
32    TabContents* source, const GURL& url, const GURL& referrer,
33    WindowOpenDisposition disposition, PageTransition::Type transition) {
34  if (profile_) {
35    // Specify a NULL browser for navigation. This will cause Navigate()
36    // to find a browser matching params.profile or create a new one.
37    Browser* browser = NULL;
38    browser::NavigateParams params(browser, url, transition);
39    params.profile = profile_;
40    params.referrer = referrer;
41    if (source && source->is_crashed() && disposition == CURRENT_TAB &&
42        transition == PageTransition::LINK)
43      params.disposition = NEW_FOREGROUND_TAB;
44    else
45      params.disposition = disposition;
46    params.window_action = browser::NavigateParams::SHOW_WINDOW;
47    browser::Navigate(&params);
48  }
49}
50
51void HtmlDialogTabContentsDelegate::NavigationStateChanged(
52    const TabContents* source, unsigned changed_flags) {
53  // We shouldn't receive any NavigationStateChanged except the first
54  // one, which we ignore because we're a dialog box.
55}
56
57void HtmlDialogTabContentsDelegate::AddNewContents(
58    TabContents* source, TabContents* new_contents,
59    WindowOpenDisposition disposition, const gfx::Rect& initial_pos,
60    bool user_gesture) {
61  if (profile_) {
62    // Specify a NULL browser for navigation. This will cause Navigate()
63    // to find a browser matching params.profile or create a new one.
64    Browser* browser = NULL;
65
66    TabContentsWrapper* wrapper = new TabContentsWrapper(new_contents);
67    browser::NavigateParams params(browser, wrapper);
68    params.profile = profile_;
69    // TODO(pinkerton): no way to get a wrapper for this.
70    // params.source_contents = source;
71    params.disposition = disposition;
72    params.window_bounds = initial_pos;
73    params.window_action = browser::NavigateParams::SHOW_WINDOW;
74    browser::Navigate(&params);
75  }
76}
77
78void HtmlDialogTabContentsDelegate::ActivateContents(TabContents* contents) {
79  // We don't do anything here because there's only one TabContents in
80  // this frame and we don't have a TabStripModel.
81}
82
83void HtmlDialogTabContentsDelegate::DeactivateContents(TabContents* contents) {
84  // We don't care about this notification (called when a user gesture triggers
85  // a call to window.blur()).
86}
87
88void HtmlDialogTabContentsDelegate::LoadingStateChanged(TabContents* source) {
89  // We don't care about this notification.
90}
91
92void HtmlDialogTabContentsDelegate::CloseContents(TabContents* source) {
93  // We receive this message but don't handle it because we really do the
94  // cleanup somewhere else (namely, HtmlDialogUIDelegate::OnDialogClosed()).
95}
96
97bool HtmlDialogTabContentsDelegate::IsPopup(const TabContents* source) const {
98  // This needs to return true so that we are allowed to be resized by our
99  // contents.
100  return true;
101}
102
103void HtmlDialogTabContentsDelegate::UpdateTargetURL(TabContents* source,
104                                                    const GURL& url) {
105  // Ignored.
106}
107
108bool HtmlDialogTabContentsDelegate::ShouldAddNavigationToHistory(
109    const history::HistoryAddPageArgs& add_page_args,
110    NavigationType::Type navigation_type) {
111  return false;
112}
113