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

Side by Side 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: Clean-up 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h" 7 #include "base/file_util.h"
8 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/extension_tab_util.h" 9 #include "chrome/browser/extensions/extension_tab_util.h"
10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
11 #include "chrome/common/extensions/extension_messages.h"
11 #include "content/browser/child_process_security_policy.h" 12 #include "content/browser/child_process_security_policy.h"
12 #include "content/browser/renderer_host/render_view_host.h" 13 #include "content/browser/renderer_host/render_view_host.h"
13 #include "content/browser/tab_contents/tab_contents.h" 14 #include "content/browser/tab_contents/tab_contents.h"
14 #include "content/browser/download/mhtml_generation_manager.h" 15 #include "content/browser/download/mhtml_generation_manager.h"
15 #include "content/public/browser/notification_details.h" 16 #include "content/public/browser/notification_details.h"
16 #include "content/public/browser/notification_source.h" 17 #include "content/public/browser/notification_source.h"
17 #include "content/public/browser/notification_types.h" 18 #include "content/public/browser/notification_types.h"
18 19
19 using content::BrowserThread; 20 using content::BrowserThread;
20 21
(...skipping 14 matching lines...) Expand all
35 bool SavePageAsMHTMLFunction::RunImpl() { 36 bool SavePageAsMHTMLFunction::RunImpl() {
36 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); 37 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_));
37 38
38 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() 39 AddRef(); // Balanced in ReturnFailure/ReturnSuccess()
39 40
40 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 41 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
41 NewRunnableMethod(this, &SavePageAsMHTMLFunction::CreateTemporaryFile)); 42 NewRunnableMethod(this, &SavePageAsMHTMLFunction::CreateTemporaryFile));
42 return true; 43 return true;
43 } 44 }
44 45
46 bool SavePageAsMHTMLFunction::OnMessageReceivedFromRenderView(
47 const IPC::Message& message) {
48 if (message.type() != ExtensionHostMsg_ResponseAck::ID)
49 return false;
50
51 int message_request_id;
52 void* iter = NULL;
53 if (!message.ReadInt(&iter, &message_request_id)) {
54 NOTREACHED() << "malformed extension message";
55 return true;
56 }
57
58 if (message_request_id != request_id())
59 return false;
60
61 // The extension process has processed the response and has created a
62 // reference to the blob, it is safe for us to go away.
63 Release(); // Balanced in Run()
64
65 return true;
66 }
67
45 void SavePageAsMHTMLFunction::CreateTemporaryFile() { 68 void SavePageAsMHTMLFunction::CreateTemporaryFile() {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
47 // TODO(jcivelli): http://crbug.com/97489 we don't clean-up the temporary file 70 // TODO(jcivelli): http://crbug.com/97489 we don't clean-up the temporary file
48 // at this point. It must be done before we can take that API 71 // at this point. It must be done before we can take that API
49 // out of experimental. 72 // out of experimental.
50 bool success = file_util::CreateTemporaryFile(&mhtml_path_); 73 bool success = file_util::CreateTemporaryFile(&mhtml_path_);
51 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 74 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
52 NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated, 75 NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated,
53 success)); 76 success));
54 } 77 }
55 78
56 void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) { 79 void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) {
57 if (!success) { 80 if (!success) {
58 ReturnFailure(kTemporaryFileError); 81 ReturnFailure(kTemporaryFileError);
59 return; 82 return;
60 } 83 }
61 84
62 Browser* browser = NULL; 85 // Sets a DeletableFileReference so the temporary file gets deleted once it is
63 TabContentsWrapper* tab_contents_wrapper = NULL; 86 // no longer used.
87 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_,
88 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
64 89
65 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), 90
66 &browser, NULL, &tab_contents_wrapper, NULL)) { 91 TabContents* tab_contents = GetTabContents();
92 if (!tab_contents) {
67 ReturnFailure(kTabClosedError); 93 ReturnFailure(kTabClosedError);
68 return; 94 return;
69 } 95 }
70 96
71 TabContents* tab_contents = tab_contents_wrapper->tab_contents();
72 registrar_.Add( 97 registrar_.Add(
73 this, content::NOTIFICATION_MHTML_GENERATED, 98 this, content::NOTIFICATION_MHTML_GENERATED,
74 content::Source<RenderViewHost>(tab_contents->render_view_host())); 99 content::Source<RenderViewHost>(tab_contents->render_view_host()));
75 // TODO(jcivelli): we should listen for navigation in the tab, tab closed, 100 // TODO(jcivelli): we should listen for navigation in the tab, tab closed,
76 // renderer died. 101 // renderer died.
77 g_browser_process->mhtml_generation_manager()->GenerateMHTML( 102 g_browser_process->mhtml_generation_manager()->GenerateMHTML(
78 tab_contents, mhtml_path_); 103 tab_contents, mhtml_path_);
79 } 104 }
80 105
81 void SavePageAsMHTMLFunction::Observe( 106 void SavePageAsMHTMLFunction::Observe(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 error_ = error; 142 error_ = error;
118 143
119 SendResponse(false); 144 SendResponse(false);
120 145
121 Release(); // Balanced in Run() 146 Release(); // Balanced in Run()
122 } 147 }
123 148
124 void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) { 149 void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 151
152 TabContents* tab_contents = GetTabContents();
153 if (!tab_contents) {
154 ReturnFailure(kTabClosedError);
155 return;
156 }
157
127 int child_id = render_view_host()->process()->id(); 158 int child_id = render_view_host()->process()->id();
128 ChildProcessSecurityPolicy::GetInstance()->GrantReadFile( 159 ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(
129 child_id, mhtml_path_); 160 child_id, mhtml_path_);
130 161
131 DictionaryValue* dict = new DictionaryValue(); 162 DictionaryValue* dict = new DictionaryValue();
132 result_.reset(dict); 163 result_.reset(dict);
133 dict->SetString("mhtmlFilePath", mhtml_path_.value()); 164 dict->SetString("mhtmlFilePath", mhtml_path_.value());
134 dict->SetInteger("mhtmlFileLength", file_size); 165 dict->SetInteger("mhtmlFileLength", file_size);
135 166
136 SendResponse(true); 167 SendResponse(true);
137 168
138 Release(); // Balanced in Run() 169 // Note that we'll wait for a response ack message before we call Release()
asargent_no_longer_on_chrome 2011/11/15 18:56:31 nit: might be worth mentioning that where this hap
Jay Civelli 2011/11/18 23:14:32 Done.
170 // (to prevent the blob file from being deleted).
139 } 171 }
172
173 TabContents* SavePageAsMHTMLFunction::GetTabContents() {
174 Browser* browser = NULL;
Mihai Parparita -not on Chrome 2011/11/15 18:51:42 Body of this function is indented one too many tim
Jay Civelli 2011/11/18 23:14:32 Done.
175 TabContentsWrapper* tab_contents_wrapper = NULL;
176
177 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(),
178 &browser, NULL, &tab_contents_wrapper, NULL)) {
179 return NULL;
180 }
181 return tab_contents_wrapper->tab_contents();
182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698