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

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

Powered by Google App Engine
This is Rietveld 408576698