Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4285)

Unified Diff: chrome/browser/extensions/extension_save_page_api.cc

Issue 8530003: Delete the temporary file when generating MHTML with the extension API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: One more sync Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 2a934b2b73924af140285b7f9d857d5dc6f3d111..fea54e7c22ae5014e0bb46e2d457a395f9dd1d63 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,18 @@ 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.";
+static SavePageAsMHTMLFunction::TestDelegate* 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,11 +49,30 @@ 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
- // at this point. It must be done before we can take that API
- // out of experimental.
bool success = file_util::CreateTemporaryFile(&mhtml_path_);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated,
@@ -59,16 +85,20 @@ 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));
+
+ 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 +154,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 || !render_view_host()) {
+ ReturnFailure(kTabClosedError);
+ return;
+ }
+
int child_id = render_view_host()->process()->GetID();
ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(
child_id, mhtml_path_);
@@ -135,5 +171,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();
}

Powered by Google App Engine
This is Rietveld 408576698