Index: chrome/browser/extensions/extension_save_page_api.cc |
diff --git a/chrome/browser/extensions/extension_save_page_api.cc b/chrome/browser/extensions/extension_save_page_api.cc |
index 7fb8416d1b14e8ad26b2ce0d26515b14a98b9579..12b2d683e41efb639a331f131d69fbfcab435d60 100644 |
--- a/chrome/browser/extensions/extension_save_page_api.cc |
+++ b/chrome/browser/extensions/extension_save_page_api.cc |
@@ -8,6 +8,7 @@ |
#include "chrome/browser/browser_process.h" |
#include "chrome/browser/extensions/extension_tab_util.h" |
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
+#include "chrome/common/extensions/extension_messages.h" |
#include "content/browser/child_process_security_policy.h" |
#include "content/browser/renderer_host/render_view_host.h" |
#include "content/browser/tab_contents/tab_contents.h" |
@@ -26,12 +27,19 @@ const char* const kSizeRetrievalError = |
const char* const kTemporaryFileError = "Failed to create a temporary file."; |
const char* const kTabClosedError = "Cannot find the tab for thie request."; |
+SavePageAsMHTMLFunction::TestDelegate* SavePageAsMHTMLFunction::test_delegate_ = |
+ NULL; |
+ |
SavePageAsMHTMLFunction::SavePageAsMHTMLFunction() : tab_id_(0) { |
} |
SavePageAsMHTMLFunction::~SavePageAsMHTMLFunction() { |
} |
+void SavePageAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
+ test_delegate_ = delegate; |
+} |
+ |
bool SavePageAsMHTMLFunction::RunImpl() { |
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); |
@@ -42,6 +50,28 @@ bool SavePageAsMHTMLFunction::RunImpl() { |
return true; |
} |
+bool SavePageAsMHTMLFunction::OnMessageReceivedFromRenderView( |
+ const IPC::Message& message) { |
+ if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
+ return false; |
+ |
+ int message_request_id; |
+ void* iter = NULL; |
+ if (!message.ReadInt(&iter, &message_request_id)) { |
+ NOTREACHED() << "malformed extension message"; |
+ return true; |
+ } |
+ |
+ if (message_request_id != request_id()) |
+ return false; |
+ |
+ // The extension process has processed the response and has created a |
+ // reference to the blob, it is safe for us to go away. |
+ Release(); // Balanced in Run() |
+ |
+ return true; |
+} |
+ |
void SavePageAsMHTMLFunction::CreateTemporaryFile() { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
// TODO(jcivelli): http://crbug.com/97489 we don't clean-up the temporary file |
@@ -59,16 +89,21 @@ void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) { |
return; |
} |
- Browser* browser = NULL; |
- TabContentsWrapper* tab_contents_wrapper = NULL; |
+ if (test_delegate_) |
+ test_delegate_->OnTemporaryFileCreated(mhtml_path_); |
- if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), |
- &browser, NULL, &tab_contents_wrapper, NULL)) { |
+ // Sets a DeletableFileReference so the temporary file gets deleted once it is |
+ // no longer used. |
+ mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
+ |
+ |
Mihai Parparita -not on Chrome
2011/11/19 01:25:21
Extra newline.
Jay Civelli
2011/11/21 02:12:48
Done.
|
+ TabContents* tab_contents = GetTabContents(); |
+ if (!tab_contents) { |
ReturnFailure(kTabClosedError); |
return; |
} |
- TabContents* tab_contents = tab_contents_wrapper->tab_contents(); |
registrar_.Add( |
this, content::NOTIFICATION_MHTML_GENERATED, |
content::Source<RenderViewHost>(tab_contents->render_view_host())); |
@@ -124,6 +159,12 @@ void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { |
void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) { |
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ TabContents* tab_contents = GetTabContents(); |
+ if (!tab_contents) { |
+ ReturnFailure(kTabClosedError); |
+ return; |
+ } |
+ |
int child_id = render_view_host()->process()->id(); |
ChildProcessSecurityPolicy::GetInstance()->GrantReadFile( |
child_id, mhtml_path_); |
@@ -135,5 +176,18 @@ void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) { |
SendResponse(true); |
- Release(); // Balanced in Run() |
+ // Note that we'll wait for a response ack message received in |
+ // OnMessageReceivedFromRenderView before we call Release() (to prevent the |
+ // blob file from being deleted). |
+} |
+ |
+TabContents* SavePageAsMHTMLFunction::GetTabContents() { |
+ Browser* browser = NULL; |
+ TabContentsWrapper* tab_contents_wrapper = NULL; |
+ |
+ if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), |
+ &browser, NULL, &tab_contents_wrapper, NULL)) { |
+ return NULL; |
+ } |
+ return tab_contents_wrapper->tab_contents(); |
} |