| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_save_page_api.h" | 5 #include "chrome/browser/extensions/extension_save_page_api.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_tab_util.h" | 10 #include "chrome/browser/extensions/extension_tab_util.h" |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 11 #include "chrome/common/extensions/extension_messages.h" | 12 #include "chrome/common/extensions/extension_messages.h" |
| 12 #include "content/browser/child_process_security_policy.h" | 13 #include "content/browser/child_process_security_policy.h" |
| 13 #include "content/browser/renderer_host/render_view_host.h" | 14 #include "content/browser/renderer_host/render_view_host.h" |
| 14 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
| 15 #include "content/browser/download/mhtml_generation_manager.h" | 16 #include "content/browser/download/mhtml_generation_manager.h" |
| 16 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 // no longer used. | 93 // no longer used. |
| 93 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, | 94 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, |
| 94 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 95 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 95 | 96 |
| 96 TabContents* tab_contents = GetTabContents(); | 97 TabContents* tab_contents = GetTabContents(); |
| 97 if (!tab_contents) { | 98 if (!tab_contents) { |
| 98 ReturnFailure(kTabClosedError); | 99 ReturnFailure(kTabClosedError); |
| 99 return; | 100 return; |
| 100 } | 101 } |
| 101 | 102 |
| 102 registrar_.Add( | 103 MHTMLGenerationManager::GenerateMHTMLCallback callback = |
| 103 this, content::NOTIFICATION_MHTML_GENERATED, | 104 base::Bind(&SavePageAsMHTMLFunction::MHTMLGenerated, this); |
| 104 content::Source<RenderViewHost>(tab_contents->render_view_host())); | 105 |
| 105 // TODO(jcivelli): we should listen for navigation in the tab, tab closed, | |
| 106 // renderer died. | |
| 107 g_browser_process->mhtml_generation_manager()->GenerateMHTML( | 106 g_browser_process->mhtml_generation_manager()->GenerateMHTML( |
| 108 tab_contents, mhtml_path_); | 107 tab_contents, mhtml_path_, callback); |
| 109 } | 108 } |
| 110 | 109 |
| 111 void SavePageAsMHTMLFunction::Observe( | 110 void SavePageAsMHTMLFunction::MHTMLGenerated(const FilePath& file_path, |
| 112 int type, | 111 int64 mhtml_file_size) { |
| 113 const content::NotificationSource& source, | 112 DCHECK(mhtml_path_ == file_path); |
| 114 const content::NotificationDetails& details) { | 113 if (mhtml_file_size <= 0) { |
| 115 DCHECK(type == content::NOTIFICATION_MHTML_GENERATED); | |
| 116 | |
| 117 const MHTMLGenerationManager::NotificationDetails* save_details = | |
| 118 content::Details<MHTMLGenerationManager::NotificationDetails>(details). | |
| 119 ptr(); | |
| 120 | |
| 121 if (mhtml_path_ != save_details->file_path) { | |
| 122 // This could happen if there are concurrent MHTML generations going on for | |
| 123 // the same tab. | |
| 124 LOG(WARNING) << "Received a notification that MHTML was generated but for a" | |
| 125 " different file."; | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 registrar_.RemoveAll(); | |
| 130 | |
| 131 if (save_details->file_size <= 0) { | |
| 132 ReturnFailure(kMHTMLGenerationFailedError); | 114 ReturnFailure(kMHTMLGenerationFailedError); |
| 133 return; | 115 return; |
| 134 } | 116 } |
| 135 | 117 |
| 136 if (save_details->file_size > std::numeric_limits<int>::max()) { | 118 if (mhtml_file_size > std::numeric_limits<int>::max()) { |
| 137 ReturnFailure(kFileTooBigError); | 119 ReturnFailure(kFileTooBigError); |
| 138 return; | 120 return; |
| 139 } | 121 } |
| 140 | 122 |
| 141 ReturnSuccess(save_details->file_size); | 123 ReturnSuccess(mhtml_file_size); |
| 142 } | 124 } |
| 143 | 125 |
| 144 void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { | 126 void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { |
| 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 146 | 128 |
| 147 error_ = error; | 129 error_ = error; |
| 148 | 130 |
| 149 SendResponse(false); | 131 SendResponse(false); |
| 150 | 132 |
| 151 Release(); // Balanced in Run() | 133 Release(); // Balanced in Run() |
| (...skipping 27 matching lines...) Expand all Loading... |
| 179 TabContents* SavePageAsMHTMLFunction::GetTabContents() { | 161 TabContents* SavePageAsMHTMLFunction::GetTabContents() { |
| 180 Browser* browser = NULL; | 162 Browser* browser = NULL; |
| 181 TabContentsWrapper* tab_contents_wrapper = NULL; | 163 TabContentsWrapper* tab_contents_wrapper = NULL; |
| 182 | 164 |
| 183 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), | 165 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), |
| 184 &browser, NULL, &tab_contents_wrapper, NULL)) { | 166 &browser, NULL, &tab_contents_wrapper, NULL)) { |
| 185 return NULL; | 167 return NULL; |
| 186 } | 168 } |
| 187 return tab_contents_wrapper->tab_contents(); | 169 return tab_contents_wrapper->tab_contents(); |
| 188 } | 170 } |
| OLD | NEW |