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