| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2006-2008 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/dom_ui/dom_ui_contents.h" |
| 6 |
| 7 #include "chrome/browser/dom_ui/dom_ui.h" |
| 8 #include "chrome/browser/dom_ui/history_ui.h" |
| 9 #include "chrome/browser/navigation_entry.h" |
| 10 #include "chrome/browser/render_view_host.h" |
| 11 #include "chrome/common/resource_bundle.h" |
| 12 |
| 13 // The scheme used for DOMUIContentses |
| 14 // TODO(glen): Merge this with the scheme in chrome_url_data_manager |
| 15 static const char kURLScheme[] = "chrome"; |
| 16 |
| 17 // The path used in internal URLs to thumbnail data. |
| 18 static const char kThumbnailPath[] = "thumb"; |
| 19 |
| 20 // The path used in internal URLs to favicon data. |
| 21 static const char kFavIconPath[] = "favicon"; |
| 22 |
| 23 /////////////////////////////////////////////////////////////////////////////// |
| 24 // FavIconSource |
| 25 |
| 26 FavIconSource::FavIconSource(Profile* profile) |
| 27 : DataSource(kFavIconPath, MessageLoop::current()), profile_(profile) {} |
| 28 |
| 29 void FavIconSource::StartDataRequest(const std::string& path, int request_id) { |
| 30 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 31 if (hs) { |
| 32 HistoryService::Handle handle; |
| 33 if (path.size() > 8 && path.substr(0, 8) == "iconurl/") { |
| 34 handle = hs->GetFavIcon( |
| 35 GURL(path.substr(8)), |
| 36 &cancelable_consumer_, |
| 37 NewCallback(this, &FavIconSource::OnFavIconDataAvailable)); |
| 38 } else { |
| 39 handle = hs->GetFavIconForURL( |
| 40 GURL(path), |
| 41 &cancelable_consumer_, |
| 42 NewCallback(this, &FavIconSource::OnFavIconDataAvailable)); |
| 43 } |
| 44 // Attach the ChromeURLDataManager request ID to the history request. |
| 45 cancelable_consumer_.SetClientData(hs, handle, request_id); |
| 46 } else { |
| 47 SendResponse(request_id, NULL); |
| 48 } |
| 49 } |
| 50 |
| 51 void FavIconSource::OnFavIconDataAvailable( |
| 52 HistoryService::Handle request_handle, |
| 53 bool know_favicon, |
| 54 scoped_refptr<RefCountedBytes> data, |
| 55 bool expired, |
| 56 GURL icon_url) { |
| 57 HistoryService* hs = |
| 58 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 59 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); |
| 60 |
| 61 if (know_favicon && data.get() && !data->data.empty()) { |
| 62 // Forward the data along to the networking system. |
| 63 SendResponse(request_id, data); |
| 64 } else { |
| 65 if (!default_favicon_.get()) { |
| 66 default_favicon_ = new RefCountedBytes; |
| 67 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( |
| 68 IDR_DEFAULT_FAVICON, &default_favicon_->data); |
| 69 } |
| 70 |
| 71 SendResponse(request_id, default_favicon_); |
| 72 } |
| 73 } |
| 74 |
| 75 /////////////////////////////////////////////////////////////////////////////// |
| 76 // ThumbnailSource |
| 77 |
| 78 ThumbnailSource::ThumbnailSource(Profile* profile) |
| 79 : DataSource(kThumbnailPath, MessageLoop::current()), profile_(profile) {} |
| 80 |
| 81 void ThumbnailSource::StartDataRequest(const std::string& path, |
| 82 int request_id) { |
| 83 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 84 if (hs) { |
| 85 HistoryService::Handle handle = hs->GetPageThumbnail( |
| 86 GURL(path), |
| 87 &cancelable_consumer_, |
| 88 NewCallback(this, &ThumbnailSource::OnThumbnailDataAvailable)); |
| 89 // Attach the ChromeURLDataManager request ID to the history request. |
| 90 cancelable_consumer_.SetClientData(hs, handle, request_id); |
| 91 } else { |
| 92 // Tell the caller that no thumbnail is available. |
| 93 SendResponse(request_id, NULL); |
| 94 } |
| 95 } |
| 96 |
| 97 void ThumbnailSource::OnThumbnailDataAvailable( |
| 98 HistoryService::Handle request_handle, |
| 99 scoped_refptr<RefCountedBytes> data) { |
| 100 HistoryService* hs = |
| 101 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 102 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); |
| 103 // Forward the data along to the networking system. |
| 104 if (data.get() && !data->data.empty()) { |
| 105 SendResponse(request_id, data); |
| 106 } else { |
| 107 if (!default_thumbnail_.get()) { |
| 108 default_thumbnail_ = new RefCountedBytes; |
| 109 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( |
| 110 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); |
| 111 } |
| 112 |
| 113 SendResponse(request_id, default_thumbnail_); |
| 114 } |
| 115 } |
| 116 |
| 117 /////////////////////////////////////////////////////////////////////////////// |
| 118 // DOMUIContents |
| 119 |
| 120 // This is the top-level URL handler for chrome: URLs, and exposed in |
| 121 // our header file. The individual DOMUIs provide a chrome: |
| 122 // HTML source at the same host/path. |
| 123 bool DOMUIContentsCanHandleURL(GURL* url, |
| 124 TabContentsType* result_type) { |
| 125 if (!url->SchemeIs(kURLScheme)) |
| 126 return false; |
| 127 |
| 128 // TODO: remove once the debugger is using DOMContentsUI |
| 129 if (url->host().compare("debugger") == 0) |
| 130 return false; |
| 131 |
| 132 *result_type = TAB_CONTENTS_DOM_UI; |
| 133 return true; |
| 134 } |
| 135 |
| 136 DOMUIContents::DOMUIContents(Profile* profile, |
| 137 SiteInstance* instance, |
| 138 RenderViewHostFactory* render_view_factory) |
| 139 : WebContents(profile, |
| 140 instance, |
| 141 render_view_factory, |
| 142 MSG_ROUTING_NONE, |
| 143 NULL), |
| 144 current_ui_(NULL) { |
| 145 set_type(TAB_CONTENTS_DOM_UI); |
| 146 } |
| 147 |
| 148 DOMUIContents::~DOMUIContents() { |
| 149 if (current_ui_) |
| 150 delete current_ui_; |
| 151 } |
| 152 |
| 153 bool DOMUIContents::CreateRenderViewForRenderManager( |
| 154 RenderViewHost* render_view_host) { |
| 155 // Be sure to enable DOM UI bindings on the RenderViewHost before |
| 156 // CreateRenderView is called. Since a cross-site transition may be |
| 157 // involved, this may or may not be the same RenderViewHost that we had when |
| 158 // we were created. |
| 159 render_view_host->AllowDOMUIBindings(); |
| 160 return WebContents::CreateRenderViewForRenderManager(render_view_host); |
| 161 } |
| 162 |
| 163 WebPreferences DOMUIContents::GetWebkitPrefs() { |
| 164 // Get the users preferences then force image loading to always be on. |
| 165 WebPreferences web_prefs = WebContents::GetWebkitPrefs(); |
| 166 web_prefs.loads_images_automatically = true; |
| 167 web_prefs.javascript_enabled = true; |
| 168 |
| 169 return web_prefs; |
| 170 } |
| 171 |
| 172 bool DOMUIContents::NavigateToPendingEntry(bool reload) { |
| 173 if (current_ui_) { |
| 174 // Shut down our existing DOMUI. |
| 175 delete current_ui_; |
| 176 current_ui_ = NULL; |
| 177 } |
| 178 |
| 179 // Set up a new DOMUI. |
| 180 NavigationEntry* pending_entry = controller()->GetPendingEntry(); |
| 181 current_ui_ = GetDOMUIForURL(pending_entry->url()); |
| 182 if (current_ui_) |
| 183 current_ui_->Init(); |
| 184 else |
| 185 return false; |
| 186 |
| 187 // Let WebContents do whatever it's meant to do. |
| 188 return WebContents::NavigateToPendingEntry(reload); |
| 189 } |
| 190 |
| 191 DOMUI* DOMUIContents::GetDOMUIForURL(const GURL &url) { |
| 192 if (url.host() == HistoryUI::GetBaseURL().host()) |
| 193 return new HistoryUI(this); |
| 194 |
| 195 return NULL; |
| 196 } |
| 197 |
| 198 void DOMUIContents::ProcessDOMUIMessage(const std::string& message, |
| 199 const std::string& content) { |
| 200 DCHECK(current_ui_); |
| 201 current_ui_->ProcessDOMUIMessage(message, content); |
| 202 } |
| 203 |
| 204 // static |
| 205 const std::string DOMUIContents::GetScheme() { |
| 206 return kURLScheme; |
| 207 } |
| OLD | NEW |