Java Code Examples for org.chromium.base.ThreadUtils#runOnUiThreadBlocking()

The following examples show how to use org.chromium.base.ThreadUtils#runOnUiThreadBlocking() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChromeBrowserProvider.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether Chrome is sufficiently initialized to handle a call to the
 * ChromeBrowserProvider.
 */
private boolean canHandleContentProviderApiCall() {
    if (isInUiThread()) return false;
    ensureUriMatcherInitialized();
    if (mNativeChromeBrowserProvider != 0) return true;
    synchronized (mLoadNativeLock) {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            @SuppressFBWarnings("DM_EXIT")
            public void run() {
                if (mNativeChromeBrowserProvider != 0) return;
                try {
                    ChromeBrowserInitializer.getInstance(getContext())
                            .handleSynchronousStartup();
                } catch (ProcessInitException e) {
                    // Chrome browser runs in the background, so exit silently; but do exit,
                    // since otherwise the next attempt to use Chrome will find a broken JNI.
                    System.exit(-1);
                }
                ensureNativeSideInitialized();
            }
        });
    }
    return true;
}
 
Example 2
Source File: ChromiumSyncAdapter.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void startBrowserProcess(
        final BrowserStartupController.StartupCallback callback,
        final SyncResult syncResult, Semaphore semaphore) {
    try {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                initCommandLine();
                if (mAsyncStartup) {
                    BrowserStartupController.get(mApplication)
                            .startBrowserProcessesAsync(callback);
                } else {
                    startBrowserProcessesSync(callback);
                }
            }
        });
    } catch (RuntimeException e) {
        // It is still unknown why we ever experience this. See http://crbug.com/180044.
        Log.w(TAG, "Got exception when trying to request a sync. Informing Android system.", e);
        // Using numIoExceptions so Android will treat this as a soft error.
        syncResult.stats.numIoExceptions++;
        semaphore.release();
    }
}
 
Example 3
Source File: ChromeBrowserProvider.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Make sure chrome is running. This method mustn't run on UI thread.
 *
 * @return Whether the native chrome process is running successfully once this has returned.
 */
private boolean ensureNativeChromeLoaded() {
    ensureUriMatcherInitialized();

    synchronized(mLoadNativeLock) {
        if (mNativeChromeBrowserProvider != 0) return true;

        final AtomicBoolean retVal = new AtomicBoolean(true);
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                retVal.set(ensureNativeChromeLoadedOnUIThread());
            }
        });
        return retVal.get();
    }
}
 
Example 4
Source File: ChromiumSyncAdapter.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private void startBrowserProcess(
        final BrowserStartupController.StartupCallback callback,
        final SyncResult syncResult, Semaphore semaphore) {
    try {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                initCommandLine();
                if (mAsyncStartup) {
                    BrowserStartupController.get(mApplication)
                            .startBrowserProcessesAsync(callback);
                } else {
                    startBrowserProcessesSync(callback);
                }
            }
        });
    } catch (RuntimeException e) {
        // It is still unknown why we ever experience this. See http://crbug.com/180044.
        Log.w(TAG, "Got exception when trying to request a sync. Informing Android system.", e);
        // Using numIoExceptions so Android will treat this as a soft error.
        syncResult.stats.numIoExceptions++;
        semaphore.release();
    }
}
 
Example 5
Source File: BackgroundTaskJobService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public void taskFinished(final boolean needsReschedule) {
    // Need to remove the current task from the currently running tasks. All other access
    // happens on the main thread, so do this removal also on the main thread.
    // To ensure that a new job is not immediately scheduled in between removing the task
    // from being a current task and before calling jobFinished, leading to us finishing
    // something with the same ID, call
    // {@link JobService#jobFinished(JobParameters, boolean} also on the main thread.
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            if (!isCurrentBackgroundTaskForJobId()) {
                Log.e(TAG, "Tried finishing non-current BackgroundTask.");
                return;
            }

            mJobService.mCurrentTasks.remove(mParams.getJobId());
            mJobService.jobFinished(mParams, needsReschedule);
        }
    });
}
 
Example 6
Source File: ChromeBrowserProvider.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether Chrome is sufficiently initialized to handle a call to the
 * ChromeBrowserProvider.
 */
private boolean canHandleContentProviderApiCall() {
    if (isInUiThread()) return false;
    ensureUriMatcherInitialized();
    if (mNativeChromeBrowserProvider != 0) return true;
    synchronized (mLoadNativeLock) {
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            @SuppressFBWarnings("DM_EXIT")
            public void run() {
                if (mNativeChromeBrowserProvider != 0) return;
                try {
                    ChromeBrowserInitializer.getInstance(getContext())
                            .handleSynchronousStartup();
                } catch (ProcessInitException e) {
                    // Chrome browser runs in the background, so exit silently; but do exit,
                    // since otherwise the next attempt to use Chrome will find a broken JNI.
                    System.exit(-1);
                }
                ensureNativeSideInitialized();
            }
        });
    }
    return true;
}
 
Example 7
Source File: ChromeBrowserProvider.java    From 365browser with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreate() {
    // Work around for broken Android versions that break the Android contract and initialize
    // ContentProviders on non-UI threads.  crbug.com/705442
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            ContentApplication.initCommandLine(getContext());

            BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
                    .addStartupCompletedObserver(
                            new BrowserStartupController.StartupCallback() {
                                @Override
                                public void onSuccess(boolean alreadyStarted) {
                                    ensureNativeSideInitialized();
                                }

                                @Override
                                public void onFailure() {
                                }
                            });
        }
    });

    return true;
}
 
Example 8
Source File: IncognitoNotificationService.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate across the running activities and for any running tabbed mode activities close their
 * incognito tabs.
 *
 * @see TabWindowManager#getIndexForWindow(Activity)
 */
private void closeIncognitoTabsInRunningTabbedActivities() {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            List<WeakReference<Activity>> runningActivities =
                    ApplicationStatus.getRunningActivities();
            for (int i = 0; i < runningActivities.size(); i++) {
                Activity activity = runningActivities.get(i).get();
                if (activity == null) continue;
                if (!(activity instanceof ChromeTabbedActivity)) continue;

                ChromeTabbedActivity tabbedActivity = (ChromeTabbedActivity) activity;
                if (tabbedActivity.isActivityDestroyed()) continue;

                tabbedActivity.getTabModelSelector().getModel(true).closeAllTabs(
                        false, false);
            }
        }
    });
}
 
Example 9
Source File: IncognitoNotificationService.java    From delion with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate across the running activities and for any running tabbed mode activities close their
 * incognito tabs.
 *
 * @see TabWindowManager#getIndexForWindow(Activity)
 */
private void closeIncognitoTabsInRunningTabbedActivities() {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            List<WeakReference<Activity>> runningActivities =
                    ApplicationStatus.getRunningActivities();
            for (int i = 0; i < runningActivities.size(); i++) {
                Activity activity = runningActivities.get(i).get();
                if (activity == null) continue;
                if (!(activity instanceof ChromeTabbedActivity)) continue;

                ChromeTabbedActivity tabbedActivity = (ChromeTabbedActivity) activity;
                if (tabbedActivity.isActivityDestroyed()) continue;

                tabbedActivity.getTabModelSelector().getModel(true).closeAllTabs(
                        false, false);
            }
        }
    });
}
 
Example 10
Source File: IncognitoNotificationService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Iterate across the running activities and for any running tabbed mode activities close their
 * incognito tabs.
 *
 * @see TabWindowManager#getIndexForWindow(Activity)
 */
private void closeIncognitoTabsInRunningTabbedActivities() {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            List<WeakReference<Activity>> runningActivities =
                    ApplicationStatus.getRunningActivities();
            for (int i = 0; i < runningActivities.size(); i++) {
                Activity activity = runningActivities.get(i).get();
                if (activity == null) continue;
                if (!(activity instanceof ChromeTabbedActivity)) continue;

                ChromeTabbedActivity tabbedActivity = (ChromeTabbedActivity) activity;
                if (tabbedActivity.isActivityDestroyed()) continue;

                // Close the Chrome Home bottom sheet if it is open over an incognito tab.
                if (tabbedActivity.getBottomSheet() != null
                        && tabbedActivity.getBottomSheet().isSheetOpen()
                        && tabbedActivity.getTabModelSelector().isIncognitoSelected()) {
                    // Skip animating to ensure to sheet is closed immediately. If the animation
                    // is run, the incognito profile will be in use until the end of the
                    // animation.
                    tabbedActivity.getBottomSheet().setSheetState(
                            BottomSheet.SHEET_STATE_PEEK, false);
                }

                tabbedActivity.getTabModelSelector().getModel(true).closeAllTabs(
                        false, false);
            }
        }
    });
}
 
Example 11
Source File: SupervisedUserContentProvider.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
void setFilterForTesting() {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            try {
                nativeSetFilterForTesting(getSupervisedUserContentProvider());
            } catch (ProcessInitException e) {
                // There is no way of returning anything sensible here, so ignore the error and
                // do nothing.
            }
        }
    });
}
 
Example 12
Source File: AwBrowserProcess.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Starts the chromium browser process running within this process. Creates threads
 * and performs other per-app resource allocations; must not be called from zygote.
 * Note: it is up to the caller to ensure this is only called once.
 * @param context The Android application context
 */
public static void start(final Context context) {
    // We must post to the UI thread to cover the case that the user
    // has invoked Chromium startup by using the (thread-safe)
    // CookieManager rather than creating a WebView.
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            if( !BrowserStartupController.get(context).startBrowserProcessesSync(
                        BrowserStartupController.MAX_RENDERERS_SINGLE_PROCESS)) {
                throw new RuntimeException("Cannot initialize WebView");
            }
        }
    });
}
 
Example 13
Source File: BackgroundTaskGcmTaskService.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void taskFinished(final boolean needsReschedule) {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            mWaiter.onWaitDone(needsReschedule);
        }
    });
}
 
Example 14
Source File: ChromeBrowserProvider.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void finalize() throws Throwable {
    try {
        // Tests might try to destroy this in the wrong thread.
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                ensureNativeChromeDestroyedOnUIThread();
            }
        });
    } finally {
        super.finalize();
    }
}
 
Example 15
Source File: AwBrowserProcess.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Starts the chromium browser process running within this process. Creates threads
 * and performs other per-app resource allocations; must not be called from zygote.
 * Note: it is up to the caller to ensure this is only called once.
 * @param context The Android application context
 */
public static void start(final Context context) {
    // We must post to the UI thread to cover the case that the user
    // has invoked Chromium startup by using the (thread-safe)
    // CookieManager rather than creating a WebView.
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            if( !BrowserStartupController.get(context).startBrowserProcessesSync(
                        BrowserStartupController.MAX_RENDERERS_SINGLE_PROCESS)) {
                throw new RuntimeException("Cannot initialize WebView");
            }
        }
    });
}
 
Example 16
Source File: SupervisedUserContentProvider.java    From delion with Apache License 2.0 5 votes vote down vote up
void setFilterForTesting() {
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            try {
                nativeSetFilterForTesting(getSupervisedUserContentProvider());
            } catch (ProcessInitException e) {
                // There is no way of returning anything sensible here, so ignore the error and
                // do nothing.
            }
        }
    });
}
 
Example 17
Source File: ChromeBrowserProvider.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void finalize() throws Throwable {
    try {
        // Tests might try to destroy this in the wrong thread.
        ThreadUtils.runOnUiThreadBlocking(new Runnable() {
            @Override
            public void run() {
                ensureNativeChromeDestroyedOnUIThread();
            }
        });
    } finally {
        super.finalize();
    }
}
 
Example 18
Source File: BackgroundTaskGcmTaskService.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public int onRunTask(TaskParams params) {
    final BackgroundTask backgroundTask =
            BackgroundTaskSchedulerGcmNetworkManager.getBackgroundTaskFromTaskParams(params);
    if (backgroundTask == null) {
        Log.w(TAG, "Failed to start task. Could not instantiate class.");
        return GcmNetworkManager.RESULT_FAILURE;
    }

    final TaskParameters taskParams =
            BackgroundTaskSchedulerGcmNetworkManager.getTaskParametersFromTaskParams(params);
    final Waiter waiter = new Waiter(Waiter.MAX_TIMEOUT_SECONDS);

    final AtomicBoolean taskNeedsBackgroundProcessing = new AtomicBoolean();
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            taskNeedsBackgroundProcessing.set(
                    backgroundTask.onStartTask(ContextUtils.getApplicationContext(), taskParams,
                            new TaskFinishedCallbackGcmTaskService(waiter)));
        }
    });

    if (!taskNeedsBackgroundProcessing.get()) return GcmNetworkManager.RESULT_SUCCESS;

    waiter.startWaiting();

    if (waiter.isRescheduleNeeded()) return GcmNetworkManager.RESULT_RESCHEDULE;
    if (!waiter.hasTaskTimedOut()) return GcmNetworkManager.RESULT_SUCCESS;

    final AtomicBoolean taskNeedsRescheduling = new AtomicBoolean();
    ThreadUtils.runOnUiThreadBlocking(new Runnable() {
        @Override
        public void run() {
            taskNeedsRescheduling.set(backgroundTask.onStopTask(
                    ContextUtils.getApplicationContext(), taskParams));
        }
    });

    if (taskNeedsRescheduling.get()) return GcmNetworkManager.RESULT_RESCHEDULE;

    return GcmNetworkManager.RESULT_SUCCESS;
}
 
Example 19
Source File: AwSettings.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void maybeRunOnUiThreadBlocking(Runnable r) {
    if (mHandler != null) {
        ThreadUtils.runOnUiThreadBlocking(r);
    }
}
 
Example 20
Source File: AwSettings.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
void maybeRunOnUiThreadBlocking(Runnable r) {
    if (mHandler != null) {
        ThreadUtils.runOnUiThreadBlocking(r);
    }
}