| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_save_page_api.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 12 #include "chrome/common/extensions/extension_messages.h" | |
| 13 #include "content/browser/child_process_security_policy.h" | |
| 14 #include "content/browser/renderer_host/render_view_host.h" | |
| 15 #include "content/browser/tab_contents/tab_contents.h" | |
| 16 #include "content/browser/download/mhtml_generation_manager.h" | |
| 17 #include "content/public/browser/notification_details.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "content/public/browser/notification_types.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 // Error messages. | |
| 24 const char* const kFileTooBigError = "The MHTML file generated is too big."; | |
| 25 const char* const kMHTMLGenerationFailedError = "Failed to generate MHTML."; | |
| 26 const char* const kSizeRetrievalError = | |
| 27 "Failed to retrieve size of generated MHTML."; | |
| 28 const char* const kTemporaryFileError = "Failed to create a temporary file."; | |
| 29 const char* const kTabClosedError = "Cannot find the tab for thie request."; | |
| 30 | |
| 31 static SavePageAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | |
| 32 | |
| 33 SavePageAsMHTMLFunction::SavePageAsMHTMLFunction() : tab_id_(0) { | |
| 34 } | |
| 35 | |
| 36 SavePageAsMHTMLFunction::~SavePageAsMHTMLFunction() { | |
| 37 } | |
| 38 | |
| 39 void SavePageAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | |
| 40 test_delegate_ = delegate; | |
| 41 } | |
| 42 | |
| 43 bool SavePageAsMHTMLFunction::RunImpl() { | |
| 44 DictionaryValue* args; | |
| 45 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | |
| 46 | |
| 47 if (!args->HasKey("tabId")) | |
| 48 return false; | |
| 49 | |
| 50 EXTENSION_FUNCTION_VALIDATE(args->GetInteger("tabId", &tab_id_)); | |
| 51 | |
| 52 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | |
| 53 | |
| 54 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 55 NewRunnableMethod(this, &SavePageAsMHTMLFunction::CreateTemporaryFile)); | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 bool SavePageAsMHTMLFunction::OnMessageReceivedFromRenderView( | |
| 60 const IPC::Message& message) { | |
| 61 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | |
| 62 return false; | |
| 63 | |
| 64 int message_request_id; | |
| 65 void* iter = NULL; | |
| 66 if (!message.ReadInt(&iter, &message_request_id)) { | |
| 67 NOTREACHED() << "malformed extension message"; | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 if (message_request_id != request_id()) | |
| 72 return false; | |
| 73 | |
| 74 // The extension process has processed the response and has created a | |
| 75 // reference to the blob, it is safe for us to go away. | |
| 76 Release(); // Balanced in Run() | |
| 77 | |
| 78 return true; | |
| 79 } | |
| 80 | |
| 81 void SavePageAsMHTMLFunction::CreateTemporaryFile() { | |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 83 bool success = file_util::CreateTemporaryFile(&mhtml_path_); | |
| 84 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 85 NewRunnableMethod(this, &SavePageAsMHTMLFunction::TemporaryFileCreated, | |
| 86 success)); | |
| 87 } | |
| 88 | |
| 89 void SavePageAsMHTMLFunction::TemporaryFileCreated(bool success) { | |
| 90 if (!success) { | |
| 91 ReturnFailure(kTemporaryFileError); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 if (test_delegate_) | |
| 96 test_delegate_->OnTemporaryFileCreated(mhtml_path_); | |
| 97 | |
| 98 // Sets a DeletableFileReference so the temporary file gets deleted once it is | |
| 99 // no longer used. | |
| 100 mhtml_file_ = webkit_blob::DeletableFileReference::GetOrCreate(mhtml_path_, | |
| 101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | |
| 102 | |
| 103 TabContents* tab_contents = GetTabContents(); | |
| 104 if (!tab_contents) { | |
| 105 ReturnFailure(kTabClosedError); | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 MHTMLGenerationManager::GenerateMHTMLCallback callback = | |
| 110 base::Bind(&SavePageAsMHTMLFunction::MHTMLGenerated, this); | |
| 111 | |
| 112 g_browser_process->mhtml_generation_manager()->GenerateMHTML( | |
| 113 tab_contents, mhtml_path_, callback); | |
| 114 } | |
| 115 | |
| 116 void SavePageAsMHTMLFunction::MHTMLGenerated(const FilePath& file_path, | |
| 117 int64 mhtml_file_size) { | |
| 118 DCHECK(mhtml_path_ == file_path); | |
| 119 if (mhtml_file_size <= 0) { | |
| 120 ReturnFailure(kMHTMLGenerationFailedError); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 if (mhtml_file_size > std::numeric_limits<int>::max()) { | |
| 125 ReturnFailure(kFileTooBigError); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 ReturnSuccess(mhtml_file_size); | |
| 130 } | |
| 131 | |
| 132 void SavePageAsMHTMLFunction::ReturnFailure(const std::string& error) { | |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 134 | |
| 135 error_ = error; | |
| 136 | |
| 137 SendResponse(false); | |
| 138 | |
| 139 Release(); // Balanced in Run() | |
| 140 } | |
| 141 | |
| 142 void SavePageAsMHTMLFunction::ReturnSuccess(int64 file_size) { | |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 144 | |
| 145 TabContents* tab_contents = GetTabContents(); | |
| 146 if (!tab_contents || !render_view_host()) { | |
| 147 ReturnFailure(kTabClosedError); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 int child_id = render_view_host()->process()->GetID(); | |
| 152 ChildProcessSecurityPolicy::GetInstance()->GrantReadFile( | |
| 153 child_id, mhtml_path_); | |
| 154 | |
| 155 DictionaryValue* dict = new DictionaryValue(); | |
| 156 result_.reset(dict); | |
| 157 dict->SetString("mhtmlFilePath", mhtml_path_.value()); | |
| 158 dict->SetInteger("mhtmlFileLength", file_size); | |
| 159 | |
| 160 SendResponse(true); | |
| 161 | |
| 162 // Note that we'll wait for a response ack message received in | |
| 163 // OnMessageReceivedFromRenderView before we call Release() (to prevent the | |
| 164 // blob file from being deleted). | |
| 165 } | |
| 166 | |
| 167 TabContents* SavePageAsMHTMLFunction::GetTabContents() { | |
| 168 Browser* browser = NULL; | |
| 169 TabContentsWrapper* tab_contents_wrapper = NULL; | |
| 170 | |
| 171 if (!ExtensionTabUtil::GetTabById(tab_id_, profile(), include_incognito(), | |
| 172 &browser, NULL, &tab_contents_wrapper, NULL)) { | |
| 173 return NULL; | |
| 174 } | |
| 175 return tab_contents_wrapper->tab_contents(); | |
| 176 } | |
| OLD | NEW |