| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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/tab_contents/match_preview.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 11 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
| 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/common/page_transition_types.h" |
| 17 #include "chrome/common/renderer_preferences.h" |
| 18 #include "ipc/ipc_message.h" |
| 19 |
| 20 class MatchPreview::TabContentsDelegateImpl : public TabContentsDelegate { |
| 21 public: |
| 22 explicit TabContentsDelegateImpl(MatchPreview* match_preview) |
| 23 : match_preview_(match_preview) { |
| 24 } |
| 25 |
| 26 virtual void OpenURLFromTab(TabContents* source, |
| 27 const GURL& url, const GURL& referrer, |
| 28 WindowOpenDisposition disposition, |
| 29 PageTransition::Type transition) {} |
| 30 virtual void NavigationStateChanged(const TabContents* source, |
| 31 unsigned changed_flags) {} |
| 32 virtual void AddNewContents(TabContents* source, |
| 33 TabContents* new_contents, |
| 34 WindowOpenDisposition disposition, |
| 35 const gfx::Rect& initial_pos, |
| 36 bool user_gesture) {} |
| 37 virtual void ActivateContents(TabContents* contents) { |
| 38 match_preview_->CommitCurrentPreview(); |
| 39 } |
| 40 virtual void LoadingStateChanged(TabContents* source) {} |
| 41 virtual void CloseContents(TabContents* source) {} |
| 42 virtual void MoveContents(TabContents* source, const gfx::Rect& pos) {} |
| 43 virtual void DetachContents(TabContents* source) {} |
| 44 virtual bool IsPopup(const TabContents* source) const { |
| 45 return false; |
| 46 } |
| 47 virtual TabContents* GetConstrainingContents(TabContents* source) { |
| 48 return NULL; |
| 49 } |
| 50 virtual void ToolbarSizeChanged(TabContents* source, bool is_animating) {} |
| 51 virtual void URLStarredChanged(TabContents* source, bool starred) {} |
| 52 virtual void UpdateTargetURL(TabContents* source, const GURL& url) {} |
| 53 virtual void ContentsMouseEvent( |
| 54 TabContents* source, const gfx::Point& location, bool motion) {} |
| 55 virtual void ContentsZoomChange(bool zoom_in) {} |
| 56 virtual void OnContentSettingsChange(TabContents* source) {} |
| 57 virtual bool IsApplication() const { return false; } |
| 58 virtual void ConvertContentsToApplication(TabContents* source) {} |
| 59 virtual bool CanReloadContents(TabContents* source) const { return true; } |
| 60 virtual gfx::Rect GetRootWindowResizerRect() const { |
| 61 return match_preview_->host_->delegate() ? |
| 62 match_preview_->host_->delegate()->GetRootWindowResizerRect() : |
| 63 gfx::Rect(); |
| 64 } |
| 65 virtual void ShowHtmlDialog(HtmlDialogUIDelegate* delegate, |
| 66 gfx::NativeWindow parent_window) {} |
| 67 virtual void BeforeUnloadFired(TabContents* tab, |
| 68 bool proceed, |
| 69 bool* proceed_to_fire_unload) {} |
| 70 virtual void ForwardMessageToExternalHost(const std::string& message, |
| 71 const std::string& origin, |
| 72 const std::string& target) {} |
| 73 virtual bool IsExternalTabContainer() const { return false; } |
| 74 virtual void SetFocusToLocationBar(bool select_all) {} |
| 75 virtual void RenderWidgetShowing() {} |
| 76 virtual ExtensionFunctionDispatcher* CreateExtensionFunctionDispatcher( |
| 77 RenderViewHost* render_view_host, |
| 78 const std::string& extension_id) { |
| 79 return NULL; |
| 80 } |
| 81 virtual bool TakeFocus(bool reverse) { return false; } |
| 82 virtual void SetTabContentBlocked(TabContents* contents, bool blocked) {} |
| 83 virtual void TabContentsFocused(TabContents* tab_content) { |
| 84 match_preview_->CommitCurrentPreview(); |
| 85 } |
| 86 virtual int GetExtraRenderViewHeight() const { return 0; } |
| 87 virtual bool CanDownload(int request_id) { return false; } |
| 88 virtual void OnStartDownload(DownloadItem* download, TabContents* tab) {} |
| 89 virtual bool HandleContextMenu(const ContextMenuParams& params) { |
| 90 return false; |
| 91 } |
| 92 virtual bool ExecuteContextMenuCommand(int command) { |
| 93 return false; |
| 94 } |
| 95 virtual void ConfirmAddSearchProvider(const TemplateURL* template_url, |
| 96 Profile* profile) {} |
| 97 virtual void ShowPageInfo(Profile* profile, |
| 98 const GURL& url, |
| 99 const NavigationEntry::SSLStatus& ssl, |
| 100 bool show_history) {} |
| 101 virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, |
| 102 bool* is_keyboard_shortcut) { |
| 103 return false; |
| 104 } |
| 105 virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) {} |
| 106 virtual void ShowRepostFormWarningDialog(TabContents* tab_contents) {} |
| 107 virtual void ShowContentSettingsWindow(ContentSettingsType content_type) {} |
| 108 virtual void ShowCollectedCookiesDialog(TabContents* tab_contents) {} |
| 109 virtual bool OnGoToEntryOffset(int offset) { return false; } |
| 110 virtual bool ShouldAddNavigationsToHistory() const { return false; } |
| 111 virtual void OnDidGetApplicationInfo(TabContents* tab_contents, |
| 112 int32 page_id) {} |
| 113 virtual Browser* GetBrowser() { return NULL; } |
| 114 virtual gfx::NativeWindow GetFrameNativeWindow() { |
| 115 return match_preview_->host_->delegate() ? |
| 116 match_preview_->host_->delegate()->GetFrameNativeWindow() : NULL; |
| 117 } |
| 118 virtual void TabContentsCreated(TabContents* new_contents) {} |
| 119 virtual bool infobars_enabled() { return false; } |
| 120 virtual bool ShouldEnablePreferredSizeNotifications() { return false; } |
| 121 virtual void UpdatePreferredSize(const gfx::Size& pref_size) {} |
| 122 virtual void ContentTypeChanged(TabContents* source) {} |
| 123 |
| 124 private: |
| 125 MatchPreview* match_preview_; |
| 126 |
| 127 DISALLOW_COPY_AND_ASSIGN(TabContentsDelegateImpl); |
| 128 }; |
| 129 |
| 130 MatchPreview::MatchPreview(TabContents* host) : host_(host) { |
| 131 delegate_.reset(new TabContentsDelegateImpl(this)); |
| 132 } |
| 133 |
| 134 MatchPreview::~MatchPreview() { |
| 135 // Delete the TabContents before the delegate as the TabContents holds a |
| 136 // reference to the delegate. |
| 137 preview_contents_.reset(NULL); |
| 138 } |
| 139 |
| 140 // static |
| 141 bool MatchPreview::IsEnabled() { |
| 142 static bool enabled = false; |
| 143 static bool checked = false; |
| 144 if (!checked) { |
| 145 checked = true; |
| 146 enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| 147 switches::kEnableMatchPreview); |
| 148 } |
| 149 return enabled; |
| 150 } |
| 151 |
| 152 void MatchPreview::Update(const GURL& url) { |
| 153 if (url_ == url) |
| 154 return; |
| 155 |
| 156 url_ = url; |
| 157 |
| 158 if (url_.is_empty() || !url_.is_valid()) { |
| 159 DestroyPreviewContents(); |
| 160 return; |
| 161 } |
| 162 |
| 163 if (!preview_contents_.get()) { |
| 164 preview_contents_.reset( |
| 165 new TabContents(host_->profile(), NULL, MSG_ROUTING_NONE, NULL)); |
| 166 preview_contents_->set_delegate(delegate_.get()); |
| 167 NotificationService::current()->Notify( |
| 168 NotificationType::MATCH_PREVIEW_TAB_CONTENTS_CREATED, |
| 169 Source<TabContents>(host_), |
| 170 NotificationService::NoDetails()); |
| 171 } |
| 172 |
| 173 // TODO: figure out transition type. |
| 174 preview_contents_->controller().LoadURL(url, GURL(), |
| 175 PageTransition::GENERATED); |
| 176 } |
| 177 |
| 178 void MatchPreview::DestroyPreviewContents() { |
| 179 url_ = GURL(); |
| 180 preview_contents_.reset(NULL); |
| 181 } |
| 182 |
| 183 void MatchPreview::CommitCurrentPreview() { |
| 184 DCHECK(preview_contents_.get()); |
| 185 if (host_->delegate()) |
| 186 host_->delegate()->CommitMatchPreview(host_); |
| 187 } |
| 188 |
| 189 TabContents* MatchPreview::ReleasePreviewContents() { |
| 190 url_ = GURL(); |
| 191 return preview_contents_.release(); |
| 192 } |
| OLD | NEW |