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

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: Addressed sky's comments. 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
21 // Error messages. 22 // Error messages.
22 const char* const kFileTooBigError = "The MHTML file generated is too big."; 23 const char* const kFileTooBigError = "The MHTML file generated is too big.";
23 const char* const kMHTMLGenerationFailedError = "Failed to generate MHTML."; 24 const char* const kMHTMLGenerationFailedError = "Failed to generate MHTML.";
24 const char* const kSizeRetrievalError = 25 const char* const kSizeRetrievalError =
25 "Failed to retrieve size of generated MHTML."; 26 "Failed to retrieve size of generated MHTML.";
26 const char* const kTemporaryFileError = "Failed to create a temporary file."; 27 const char* const kTemporaryFileError = "Failed to create a temporary file.";
27 const char* const kTabClosedError = "Cannot find the tab for thie request."; 28 const char* const kTabClosedError = "Cannot find the tab for thie request.";
28 29
30 SavePageAsMHTMLFunction::TestDelegate* SavePageAsMHTMLFunction::test_delegate_ =
31 NULL;
32
29 SavePageAsMHTMLFunction::SavePageAsMHTMLFunction() : tab_id_(0) { 33 SavePageAsMHTMLFunction::SavePageAsMHTMLFunction() : tab_id_(0) {
30 } 34 }
31 35
32 SavePageAsMHTMLFunction::~SavePageAsMHTMLFunction() { 36 SavePageAsMHTMLFunction::~SavePageAsMHTMLFunction() {
33 } 37 }
34 38
39 void SavePageAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) {
40 test_delegate_ = delegate;
41 }
42
35 bool SavePageAsMHTMLFunction::RunImpl() { 43 bool SavePageAsMHTMLFunction::RunImpl() {
36 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_)); 44 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id_));
37 45
38 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() 46 AddRef(); // Balanced in ReturnFailure/ReturnSuccess()
39 47
40 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 48 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
41 NewRunnableMethod(this, &SavePageAsMHTMLFunction::CreateTemporaryFile)); 49 NewRunnableMethod(this, &SavePageAsMHTMLFunction::CreateTemporaryFile));
42 return true; 50 return true;
43 } 51 }
44 52
53 bool SavePageAsMHTMLFunction::OnMessageReceivedFromRenderView(
54 const IPC::Message& message) {
55 if (message.type() != ExtensionHostMsg_ResponseAck::ID)
56 return false;
57
58 int message_request_id;
59 void* iter = NULL;
60 if (!message.ReadInt(&iter, &message_request_id)) {
61 NOTREACHED() << "malformed extension message";
62 return true;
63 }
64
65 if (message_request_id != request_id())
66 return false;
67
68 // The extension process has processed the response and has created a
69 // reference to the blob, it is safe for us to go away.
70 Release(); // Balanced in Run()
71
72 return true;
73 }
74
45 void SavePageAsMHTMLFunction::CreateTemporaryFile() { 75 void SavePageAsMHTMLFunction::CreateTemporaryFile() {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
47 // TODO(jcivelli): http://crbug.com/97489 we don't clean-up the temporary file 77 // 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 78 // at this point. It must be done before we can take that API
49 // out of experimental. 79 // out of experimental.
50 bool success = file_util::CreateTemporaryFile(&mhtml_path_); 80 bool success = file_util::CreateTemporaryFile(&mhtml_path_);
51 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 81 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
52 NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated, 82 NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated,
53 success)); 83 success));
54 } 84 }
55 85
56 void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) { 86 void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) {
57 if (!success) { 87 if (!success) {
58 ReturnFailure(kTemporaryFileError); 88 ReturnFailure(kTemporaryFileError);
59 return; 89 return;
60 } 90 }
61 91
62 Browser* browser = NULL; 92 if (test_delegate_)
63 TabContentsWrapper* tab_contents_wrapper = NULL; 93 test_delegate_->OnTemporaryFileCreated(mhtml_path_);
64 94
65 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), 95 // Sets a DeletableFileReference so the temporary file gets deleted once it is
66 &browser, NULL, &tab_contents_wrapper, NULL)) { 96 // no longer used.
97 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_,
98 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
99
100
Mihai Parparita -not on Chrome 2011/11/19 01:25:21 Extra newline.
Jay Civelli 2011/11/21 02:12:48 Done.
101 TabContents* tab_contents = GetTabContents();
102 if (!tab_contents) {
67 ReturnFailure(kTabClosedError); 103 ReturnFailure(kTabClosedError);
68 return; 104 return;
69 } 105 }
70 106
71 TabContents* tab_contents = tab_contents_wrapper->tab_contents();
72 registrar_.Add( 107 registrar_.Add(
73 this, content::NOTIFICATION_MHTML_GENERATED, 108 this, content::NOTIFICATION_MHTML_GENERATED,
74 content::Source<RenderViewHost>(tab_contents->render_view_host())); 109 content::Source<RenderViewHost>(tab_contents->render_view_host()));
75 // TODO(jcivelli): we should listen for navigation in the tab, tab closed, 110 // TODO(jcivelli): we should listen for navigation in the tab, tab closed,
76 // renderer died. 111 // renderer died.
77 g_browser_process->mhtml_generation_manager()->GenerateMHTML( 112 g_browser_process->mhtml_generation_manager()->GenerateMHTML(
78 tab_contents, mhtml_path_); 113 tab_contents, mhtml_path_);
79 } 114 }
80 115
81 void SavePageAsMHTMLFunction::Observe( 116 void SavePageAsMHTMLFunction::Observe(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 error_ = error; 152 error_ = error;
118 153
119 SendResponse(false); 154 SendResponse(false);
120 155
121 Release(); // Balanced in Run() 156 Release(); // Balanced in Run()
122 } 157 }
123 158
124 void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) { 159 void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) {
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 161
162 TabContents* tab_contents = GetTabContents();
163 if (!tab_contents) {
164 ReturnFailure(kTabClosedError);
165 return;
166 }
167
127 int child_id = render_view_host()->process()->id(); 168 int child_id = render_view_host()->process()->id();
128 ChildProcessSecurityPolicy::GetInstance()->GrantReadFile( 169 ChildProcessSecurityPolicy::GetInstance()->GrantReadFile(
129 child_id, mhtml_path_); 170 child_id, mhtml_path_);
130 171
131 DictionaryValue* dict = new DictionaryValue(); 172 DictionaryValue* dict = new DictionaryValue();
132 result_.reset(dict); 173 result_.reset(dict);
133 dict->SetString("mhtmlFilePath", mhtml_path_.value()); 174 dict->SetString("mhtmlFilePath", mhtml_path_.value());
134 dict->SetInteger("mhtmlFileLength", file_size); 175 dict->SetInteger("mhtmlFileLength", file_size);
135 176
136 SendResponse(true); 177 SendResponse(true);
137 178
138 Release(); // Balanced in Run() 179 // Note that we'll wait for a response ack message received in
180 // OnMessageReceivedFromRenderView before we call Release() (to prevent the
181 // blob file from being deleted).
139 } 182 }
183
184 TabContents* SavePageAsMHTMLFunction::GetTabContents() {
185 Browser* browser = NULL;
186 TabContentsWrapper* tab_contents_wrapper = NULL;
187
188 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(),
189 &browser, NULL, &tab_contents_wrapper, NULL)) {
190 return NULL;
191 }
192 return tab_contents_wrapper->tab_contents();
193 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698