| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/api/page_capture/page_capture_api.h" | 5 #include "chrome/browser/extensions/api/page_capture/page_capture_api.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profiles_state.h" | |
| 18 #include "content/public/browser/child_process_security_policy.h" | 15 #include "content/public/browser/child_process_security_policy.h" |
| 19 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
| 21 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
| 22 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 23 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 24 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/common/mhtml_generation_params.h" | 22 #include "content/public/common/mhtml_generation_params.h" |
| 26 #include "extensions/common/extension_messages.h" | 23 #include "extensions/common/extension_messages.h" |
| 27 | 24 |
| 28 #if defined(OS_CHROMEOS) | |
| 29 #include "chrome/browser/chromeos/extensions/public_session_permission_helper.h" | |
| 30 #endif | |
| 31 | |
| 32 using content::BrowserThread; | 25 using content::BrowserThread; |
| 33 using content::ChildProcessSecurityPolicy; | 26 using content::ChildProcessSecurityPolicy; |
| 34 using content::WebContents; | 27 using content::WebContents; |
| 35 using extensions::PageCaptureSaveAsMHTMLFunction; | 28 using extensions::PageCaptureSaveAsMHTMLFunction; |
| 36 using storage::ShareableFileReference; | 29 using storage::ShareableFileReference; |
| 37 | 30 |
| 38 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; | 31 namespace SaveAsMHTML = extensions::api::page_capture::SaveAsMHTML; |
| 39 | 32 |
| 40 namespace { | 33 namespace { |
| 41 | 34 |
| 42 const char kFileTooBigError[] = "The MHTML file generated is too big."; | 35 const char kFileTooBigError[] = "The MHTML file generated is too big."; |
| 43 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; | 36 const char kMHTMLGenerationFailedError[] = "Failed to generate MHTML."; |
| 44 const char kTemporaryFileError[] = "Failed to create a temporary file."; | 37 const char kTemporaryFileError[] = "Failed to create a temporary file."; |
| 45 const char kTabClosedError[] = "Cannot find the tab for this request."; | 38 const char kTabClosedError[] = "Cannot find the tab for this request."; |
| 46 #if defined(OS_CHROMEOS) | |
| 47 const char kUserDenied[] = "User denied request."; | |
| 48 #endif | |
| 49 | 39 |
| 50 void ClearFileReferenceOnIOThread( | 40 void ClearFileReferenceOnIOThread( |
| 51 scoped_refptr<storage::ShareableFileReference>) {} | 41 scoped_refptr<storage::ShareableFileReference>) {} |
| 52 | 42 |
| 53 } // namespace | 43 } // namespace |
| 54 | 44 |
| 55 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; | 45 static PageCaptureSaveAsMHTMLFunction::TestDelegate* test_delegate_ = NULL; |
| 56 | 46 |
| 57 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { | 47 PageCaptureSaveAsMHTMLFunction::PageCaptureSaveAsMHTMLFunction() { |
| 58 } | 48 } |
| 59 | 49 |
| 60 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { | 50 PageCaptureSaveAsMHTMLFunction::~PageCaptureSaveAsMHTMLFunction() { |
| 61 if (mhtml_file_.get()) { | 51 if (mhtml_file_.get()) { |
| 62 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
| 63 BrowserThread::IO, FROM_HERE, | 53 BrowserThread::IO, FROM_HERE, |
| 64 base::Bind(&ClearFileReferenceOnIOThread, base::Passed(&mhtml_file_))); | 54 base::Bind(&ClearFileReferenceOnIOThread, base::Passed(&mhtml_file_))); |
| 65 } | 55 } |
| 66 } | 56 } |
| 67 | 57 |
| 68 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { | 58 void PageCaptureSaveAsMHTMLFunction::SetTestDelegate(TestDelegate* delegate) { |
| 69 test_delegate_ = delegate; | 59 test_delegate_ = delegate; |
| 70 } | 60 } |
| 71 | 61 |
| 72 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { | 62 bool PageCaptureSaveAsMHTMLFunction::RunAsync() { |
| 73 params_ = SaveAsMHTML::Params::Create(*args_); | 63 params_ = SaveAsMHTML::Params::Create(*args_); |
| 74 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 64 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 75 | 65 |
| 76 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() | 66 AddRef(); // Balanced in ReturnFailure/ReturnSuccess() |
| 77 | 67 |
| 78 // In Public Sessions, extensions (and apps) are force-installed by admin | |
| 79 // policy so the user does not get a chance to review the permissions for | |
| 80 // these extensions. This is not acceptable from a security/privacy | |
| 81 // standpoint, so when an extension uses the PageCapture API for the first | |
| 82 // time, we show the user a dialog where they can choose whether to allow the | |
| 83 // extension access to the API. | |
| 84 #if defined(OS_CHROMEOS) | |
| 85 if (profiles::IsPublicSession()) { | |
| 86 WebContents* web_contents = GetWebContents(); | |
| 87 if (!web_contents) { | |
| 88 ReturnFailure(kTabClosedError); | |
| 89 return true; | |
| 90 } | |
| 91 // This Unretained is safe because this object is Released() in | |
| 92 // OnMessageReceived which gets called at some point after callback is run. | |
| 93 auto callback = | |
| 94 base::Bind(&PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest, | |
| 95 base::Unretained(this)); | |
| 96 permission_helper::HandlePermissionRequest( | |
| 97 *extension(), {APIPermission::kPageCapture}, web_contents, callback, | |
| 98 permission_helper::PromptFactory()); | |
| 99 return true; | |
| 100 } | |
| 101 #endif | |
| 102 | |
| 103 BrowserThread::PostTask( | 68 BrowserThread::PostTask( |
| 104 BrowserThread::FILE, FROM_HERE, | 69 BrowserThread::FILE, FROM_HERE, |
| 105 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | 70 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); |
| 106 return true; | 71 return true; |
| 107 } | 72 } |
| 108 | 73 |
| 109 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( | 74 bool PageCaptureSaveAsMHTMLFunction::OnMessageReceived( |
| 110 const IPC::Message& message) { | 75 const IPC::Message& message) { |
| 111 if (message.type() != ExtensionHostMsg_ResponseAck::ID) | 76 if (message.type() != ExtensionHostMsg_ResponseAck::ID) |
| 112 return false; | 77 return false; |
| 113 | 78 |
| 114 int message_request_id; | 79 int message_request_id; |
| 115 base::PickleIterator iter(message); | 80 base::PickleIterator iter(message); |
| 116 if (!iter.ReadInt(&message_request_id)) { | 81 if (!iter.ReadInt(&message_request_id)) { |
| 117 NOTREACHED() << "malformed extension message"; | 82 NOTREACHED() << "malformed extension message"; |
| 118 return true; | 83 return true; |
| 119 } | 84 } |
| 120 | 85 |
| 121 if (message_request_id != request_id()) | 86 if (message_request_id != request_id()) |
| 122 return false; | 87 return false; |
| 123 | 88 |
| 124 // The extension process has processed the response and has created a | 89 // The extension process has processed the response and has created a |
| 125 // reference to the blob, it is safe for us to go away. | 90 // reference to the blob, it is safe for us to go away. |
| 126 Release(); // Balanced in Run() | 91 Release(); // Balanced in Run() |
| 127 | 92 |
| 128 return true; | 93 return true; |
| 129 } | 94 } |
| 130 | 95 |
| 131 #if defined(OS_CHROMEOS) | |
| 132 void PageCaptureSaveAsMHTMLFunction::ResolvePermissionRequest( | |
| 133 const PermissionIDSet& allowed_permissions) { | |
| 134 if (allowed_permissions.ContainsID(APIPermission::kPageCapture)) { | |
| 135 BrowserThread::PostTask( | |
| 136 BrowserThread::FILE, FROM_HERE, | |
| 137 base::Bind(&PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile, this)); | |
| 138 } else { | |
| 139 ReturnFailure(kUserDenied); | |
| 140 } | |
| 141 } | |
| 142 #endif | |
| 143 | |
| 144 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { | 96 void PageCaptureSaveAsMHTMLFunction::CreateTemporaryFile() { |
| 145 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 97 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 146 bool success = base::CreateTemporaryFile(&mhtml_path_); | 98 bool success = base::CreateTemporaryFile(&mhtml_path_); |
| 147 BrowserThread::PostTask( | 99 BrowserThread::PostTask( |
| 148 BrowserThread::IO, FROM_HERE, | 100 BrowserThread::IO, FROM_HERE, |
| 149 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, | 101 base::Bind(&PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated, this, |
| 150 success)); | 102 success)); |
| 151 } | 103 } |
| 152 | 104 |
| 153 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { | 105 void PageCaptureSaveAsMHTMLFunction::TemporaryFileCreated(bool success) { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 GetProfile(), | 195 GetProfile(), |
| 244 include_incognito(), | 196 include_incognito(), |
| 245 &browser, | 197 &browser, |
| 246 NULL, | 198 NULL, |
| 247 &web_contents, | 199 &web_contents, |
| 248 NULL)) { | 200 NULL)) { |
| 249 return NULL; | 201 return NULL; |
| 250 } | 202 } |
| 251 return web_contents; | 203 return web_contents; |
| 252 } | 204 } |
| OLD | NEW |